MSSQL Search Blocked Sessions
Query for blocked sessions.
SELECT session_id, status, blocking_session_id FROM sys.dm_exec_requests WHERE blocking_session_id > 0
Close a blocking session.
KILL {blocking_session_id}
All posts containing MSSQL-related scripts and patterns.
Query for blocked sessions.
SELECT session_id, status, blocking_session_id FROM sys.dm_exec_requests WHERE blocking_session_id > 0
Close a blocking session.
KILL {blocking_session_id}
Display index physical stats.
SELECT * FROM sys.DM_DB_INDEX_PHYSICAL_STATS ( DB_ID('database_name'), Object_ID('schema.tablename'), NULL, -- Index ID NULL, -- Partition Number 'DETAILED' -- Mode: [NULL|DEFAULT|LIMITED|SAMPLED|DETAILED] )
fragmentation <10% | geen actie nodig |
fragmentation 10-30% | reorganize index (ALTER INDEX index_name ON schema.table_name REORGANIZE) |
fragmentation >30% | rebuild indexes (ALTER INDEX… REBUILD of CREATE INDEX met DROP_EXISTING optie) |
Instruction written in the Dutch language…
M.b.v. onderstaande T-SQL statements maak je een indexed view. Bij een indexed view worden de gegevens als aparte objecten opgeslagen in de database. Dit kan in bepaalde omstandigheden performance winst opleveren. Er zijn echter ook een aantal restricties. Bij een update van een record waar een dergelijke view van afhankelijk is wordt ook de view bijgewerkt. Dit kan dus ook performance verlies opleveren. SCHEMABINDING zorgt ervoor dat onderliggende tabellen niet kunnen worden aangepast zonder eerst de VIEW te verwijderen.
SET QUOTED_IDENTIFIER ON GO CREATE VIEW [dbo].[Indexed_Viewname] WITH SCHEMABINDING AS SELECT x2.column1 as [column1a], column1, x3.column2 FROM dbo.table1 x1 JOIN dbo.table2 x2 ON x1.column3 = x2.column5 JOIN dbo.table3 x3 ON x2.column4 = x3.column1 WHERE column2 = 'X'
Met onderstaande T-SQL statement maak je een clustered index.
CREATE UNIQUE CLUSTERED INDEX Cl_Idx_Viewname ON dbo.Indexed_Viewname(column1)
Instruction written in the Dutch language…
CREATE PARTITION FUNCTION pfTablename (datatype) AS RANGE LEFT FOR VALUES (Getdate() - 365)
CREATE PARTITION SCHEME pfTablename AS PARTITION pfTablename TO (Filegroup1, Filegroup2, Filegroup3)
Example to create a unique constraint on a column.
ALTER TABLE tablename ADD CONSTRAINT CK_Columnname UNIQUE (Columnname)
INNER JOIN: Only records with joing fields in both tables.
LEFT OUTER JOIN: All records from table 1 and only matching fields from table 2 that match on join field.
RIGHT OUTER JOIN: All records from table 2 and only matchin records from table 2 that match on join field
Below an example…
SELECT DISTINCT [Database].[dbo].[Table1].[Field1] AS [Table1-Field1] ,[Database].[dbo].[Table1].[Field2] AS [Table1-Field2] ,[Database].[dbo].[Table2].[Field3] AS [Table2-Field3] FROM [Database].[dbo].[Table1] INNER JOIN [Database].[dbo].[Table2] ON [Database].[dbo].[Table2].[Field0] = [Database].[dbo].[Table1].[Field0] WHERE [Database].[dbo].[Table1].[Field1] [Database].[dbo].[Table2].[Field3] ORDER BY [Database].[dbo].[Table1].[Field1],[Database].[dbo].[Table1].[Field1];