Saturday 28 April 2012

Last Queries Executed Recently on Server.

If the Database users want to get the queries executed on the server before sometime, then he can get it by executing the below command. While it returns only those queries which are existing in the Cache.

SELECT           DEQS.last_execution_time   AS   [Time]
                     , DEST.TEXT    AS    [Query]
FROM           sys.dm_exec_query_stats   AS   DEQS
CROSS APPLY sys.dm_exec_sql_text(DEQS.sql_handle)   AS   DEST
ORDER BY    DEQS.last_execution_time DESC 

you will the below output screen : 


 












If you like to see execution plan as well then write the blow command:

SELECT      DEQS.last_execution_time AS [Time]
                ,DEST.TEXT AS [Query]
                ,DECP.QUERY_PLAN
FROM      sys.dm_exec_query_stats AS DEQS
CROSS APPLY sys.dm_exec_sql_text(DEQS.sql_handle) AS DEST
CROSS APPLY sys.dm_exec_query_plan(DEQS.plan_handle) AS DECP
ORDER BY DEQS.last_execution_time DESC












In the last column of above output is the XML formatted Execution Plan of the query.

Friday 20 April 2012

Index Rebuild vs Index Reorganize


         To improve the performance of SQL access queries, Indexing the table(s) is the foremost option. But after some time, due to Insert/Update/Delete operations those indexes get Fragmented, which in cause decline in performance. To avoid such performance decline; those indexes need to be Reorganized or Rebuilt.

Index Rebuild : This process drops the existing Index and Recreates a new index with same name.

Syntax:
ALTER INDEX ALL ON Production.TrnBSETrades REBUILD

Index Reorganize : This process does not drop the existing Index but physically reorganizes the leaf nodes of the index.

Syntax:
ALTER INDEX ALL ON Production.TrnBSETrades REORGANIZE

To get the details of the index fragmentations status, execute following DBCC Command :

DBCC SHOWCONTIG  ('Production.dbo.TrnBSETrades','NCLIX_TrnBSETrades_UserID_ClientCode_SaudaDate')
As a result, you will get the below output :

DBCC SHOWCONTIG scanning 'TrnBSETrades' table...
Table: 'TrnBSETrades' (548275418); index ID: 10, database ID: 25
LEAF level scan performed.
- Pages Scanned................................: 3120
- Extents Scanned..............................: 421
- Extent Switches..............................: 3212
- Avg. Pages per Extent........................: 9.5
- Scan Density [Best Count:Actual Count].......: 13.21% [458:3253]
- Logical Scan Fragmentation ..................: 97.12%
- Extent Scan Fragmentation ...................: 25.78%
- Avg. Bytes Free per Page.....................: 3845.9
- Avg. Page Density (full).....................: 58.79%
DBCC execution completed. If DBCC printed error messages, contact your system administrator.



You can also execute the the above command without mentioning the Index name as:
DBCC SHOWCONTIG ('Production.dbo.TrnBSETrades')

Also you can get the fragmentation using below query:

Recommendation: Index should be rebuild when index fragmentation is great than 40%. Index should be reorganized when index fragmentation is between 10% to 40%. Index rebuilding process uses more CPU and it locks the database resources. SQL Server development version and Enterprise version has option ONLINE, which can be turned on when Index is rebuilt. ONLINE option will keep index available during the rebuilding.

Tuesday 17 April 2012

DBCC RESEED Table Identity Value – Reset Table Identity

DBCC CHECKIDENT can reseed (reset) the identity value of the table. For example, YourTable has 50 rows with 50
as last identity. If we want next record to have identity as 85 we need to run following T SQL script in Query Analyzer.
DBCC CHECKIDENT (yourtable, reseed, 84)
If table has to start with an identity of 1 with the next insert then table should be reseeded with the identity to 0.
If identity seed is set below values that currently are in table, it will violate the uniqueness constraint
as soon as the values start to duplicate and will generate error.

Row Counts for all the Tables in a DataBase

(1)
SELECT CAST(OBJECT_NAME(id) AS VARCHAR(40)) AS 'Table',
       ROWS AS 'Rows'
FROM sys.sysindexes
WHERE OBJECT_NAME(id) NOT LIKE 'sys%' AND indid < 2
ORDER BY 'Table';
------ OR -------

(2)
SELECT o.name,   ddps.row_count 
FROM sys.indexes AS i  
  INNER JOIN sys.objects AS o
  ON i.OBJECT_ID = o.OBJECT_ID  
  INNER JOIN sys.dm_db_partition_stats AS ddps
  ON i.OBJECT_ID = ddps.OBJECT_ID  
  AND i.index_id = ddps.index_id 
WHERE i.index_id < 2 
  AND o.is_ms_shipped = 0
ORDER BY  ddps.row_count