Search This Blog

Showing posts with label sql optmization. Show all posts
Showing posts with label sql optmization. Show all posts

Friday, 13 March 2015

#TIP: Query to find the last sql server restart using DMVs

Following script will help us to find the last sql server restart datetime.

SELECT sqlserver_start_time FROM sys.dm_os_sys_info

Friday, 16 January 2015

When was last tempdb cleared

Following query will help us to find when was tempdb cleared? Means when was last usage stats cleared? This will show how many days are passed when usage stats are cleared.

SELECT  DATEDIFF(DAYsd.crdateGETDATE()) AS days_historyFROM    sys.sysdatabases sdWHERE   sd.[name] 'tempdb' ;


Wednesday, 7 January 2015

Find Heap Tables in your sql database

Tip #4 Find Heap Tables in your sql database.

I am posting some of the most useful queries which help a dba/ developer to optimize database performance. Please reference to different Tips in other blogs as well.

Following query will help in determining the heap Tables in Sql database. Some people may not be aware of: What are Heap Tables?

Heaps (Tables without Clustered Indexes): A heap is a table without a clustered index. One or more non clustered indexes can be created on tables which can be stored as a heap. Data which is stored in heap, is without specifying any order.

Run the following query in your sql server query window:

SELECT T.NAME 'Heaptable'
FROM sys.indexes I
INNER JOIN sys.tables T
 ON I.object_id = T.object_id
WHERE I.type = 0
 AND T.type = 'U'

Calculate lines of code in Sql Stored Procedure

Tip #3 Find the lines of code in Sql Stored Procedure.


I am posting some of the most useful queries which help a dba/ developer to optimize database performance. Please reference to different Tips in other blogs as well.


Following query will help in determining the lines of code present in the stored procedure. This query will exclude spaces and comments while calculating the lines of codes.. Run the following query in your sql server query window:


SELECT t.sp_name
 ,sum(t.lines_of_code) - 1 AS lines_ofcode
 ,t.type_desc
FROM (
 SELECT o.NAME AS sp_name
  ,(len(c.TEXT) - len(replace(c.TEXT, CHAR(10), ''))) AS lines_of_code
  ,CASE 
   WHEN o.xtype = 'P'
    THEN 'Stored Procedure'
   WHEN o.xtype IN (
     'FN'
     ,'IF'
     ,'TF'
     )
    THEN 'Function'
   END AS type_desc
 FROM sysobjects o
 INNER JOIN syscomments c
  ON c.id = o.id
 WHERE o.xtype IN (
   'P'
   ,'FN'
   ,'IF'
   ,'TF'
   )
  AND o.category = 0
  AND o.NAME NOT IN (
   'fn_diagramobjects'
   ,'sp_alterdiagram'
   ,'sp_creatediagram'
   ,'sp_dropdiagram'
   ,'sp_helpdiagramdefinition'
   ,'sp_helpdiagrams'
   ,'sp_renamediagram'
   ,'sp_upgraddiagrams'
   ,'sysdiagrams'
   )
 ) t
GROUP BY t.sp_name
 ,t.type_desc
ORDER BY 1

Find Memory Usage (RAM) by Sql query

Tip #2 Memory Usage  (RAM) by Sql processes.

I am posting some of the most useful queries which help a dba/ developer to optimize database performance. Please reference to different Tips in other blogs as well.Following query will help in determine Memory usage in percentage by sql processes. Run the following query in your sql server query window:


SELECT (((m.total_physical_memory_kb - m.available_physical_memory_kb) / convert(FLOAT, m.total_physical_memory_kb)) * 100) AS memoryusage FROM sys.dm_os_sys_memory m GO

Check CPU usage by Sql query

Tip #1 Check CPU usage by Sql query:


I am posting some of the most useful queries which help a dba/ developer to optimize database performance. Please reference to different Tips in other blogs as well.Following query will help in determine CPU consumption in percentage by sql processes. Run the following query in your sql server query window:


SELECT TOP 1 100 - x.System_Idle AS CPU_USAGE
FROM (
 SELECT y.record.value('(./Record/@id)[1]', 'int') AS record_id, y.record.value('(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS System_Idle
 FROM (
  SELECT CONVERT(XML, record) AS record
  FROM sys.dm_os_ring_buffers
  WHERE ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR' AND record LIKE '%<SystemHealth>%'
  ) AS y
 ) AS x
ORDER BY x.record_id DESC
GO