SQL SERVER – Identify Most Resource Intensive Queries – SQL in Sixty Seconds #029 – Video

There are a few questions I often get asked. I wonder how interesting is that in our daily life all of us have to often need the same kind of information at the same time. Here is the example of the similar questions:

  • How many user created tables are there in the database?
  • How many non clustered indexes each of the tables in the database have?
  • Is table Heap or has clustered index on it?
  • How many rows each of the tables is contained in the database?

I finally wrote down a very quick script (in less than sixty seconds when I originally wrote it) which can answer above questions. I also created a very quick video to explain the results and how to execute the script.

Here is the complete complete script which I have used in the SQL in Sixty Seconds Video. Thanks Harsh for important Tip in the comment.

SELECT [schema_name] = s.name, table_name = o.name,
MAX(i1.type_desc) ClusteredIndexorHeap,
COUNT(i.TYPE) NoOfNonClusteredIndex, p.rows
FROM sys.indexes i
RIGHT JOIN sys.objects o ON i.[object_id] = o.[object_id]
INNER JOIN sys.schemas s ON o.[schema_id] = s.[schema_id]
LEFT JOIN sys.partitions p ON p.OBJECT_ID = o.OBJECT_ID AND p.index_id IN (0,1)
LEFT JOIN sys.indexes i1 ON i.OBJECT_ID = i1.OBJECT_ID AND i1.TYPE IN (0,1)
WHERE o.TYPE IN ('U')
GROUP BY s.name, o.name, p.rows
ORDER BY schema_name, table_name

Above script have small modification as suggested by Harsh in comment.

Related Tips in SQL in Sixty Seconds:

What would you like to see in the next SQL in Sixty Seconds video?

Reference: Pinal Dave (http://blog.sqlauthority.com)

About these ads

11 thoughts on “SQL SERVER – Identify Most Resource Intensive Queries – SQL in Sixty Seconds #029 – Video

  1. This script returns only details of tables which do not have any non-clustered indexes.

    Script should be:

    SELECT [schema_name] = s.name, table_name = o.name,
    MAX(i1.type_desc) ClusteredIndexorHeap,
    COUNT(i.TYPE) NoOfNonClusteredIndex, p.rows
    FROM sys.indexes i
    RIGHT JOIN sys.objects o ON i.[object_id] = o.[object_id] AND i.TYPE=2
    INNER JOIN sys.schemas s ON o.[schema_id] = s.[schema_id]
    LEFT JOIN sys.partitions p ON p.OBJECT_ID = o.OBJECT_ID AND p.index_id IN (0,1)
    LEFT JOIN sys.indexes i1 ON o.OBJECT_ID = i1.OBJECT_ID AND i1.TYPE IN (0,1)
    WHERE o.TYPE IN (‘U’)
    GROUP BY s.name, o.name, p.rows
    ORDER BY schema_name, table_name

  2. Hi sir , can you please help me on below requirement

    I had two servers

    server 1 ——-SSIS Package is present
    server 2—– EXe location

    I had SSIS Package in server 1 , at one step am calling the exe present in server 2 and passing arguments and working directory to server 2

    when i execute the package the package should execute in Server 1, but when it trigger to exe the exe should execute in server 2 not in server 1

    can any one help me on these i can use dotnet also

    thanks in advance

  3. The count of nonclustered index is actually returning total count of index type which includes Heap, clustered, non clustered.

    SELECT
    [schema_name] = s.name
    ,table_name = o.name
    ,MAX(i1.type_desc) ClusteredIndexorHeap
    ,MAX(COALESCE(i2.NCIC,0)) NoOfNonClusteredIndex
    ,p.rows
    FROM sys.indexes i
    RIGHT JOIN sys.objects o
    ON i.[object_id] = o.[object_id]
    INNER JOIN sys.schemas s
    ON o.[schema_id] = s.[schema_id]
    LEFT JOIN sys.partitions p
    ON p.OBJECT_ID = o.OBJECT_ID AND p.index_id IN (0,1)
    LEFT JOIN sys.indexes i1
    ON i.OBJECT_ID = i1.OBJECT_ID AND i1.TYPE IN (0,1)
    LEFT JOIN (
    SELECT object_id,COUNT(Index_id) NCIC
    FROM sys.indexes
    where type = 2
    GROUP BY object_id) I2
    ON i.OBJECT_ID = i2.OBJECT_ID
    WHERE o.TYPE IN (‘U’)
    GROUP BY s.name, o.name, p.rows
    ORDER BY schema_name, table_name

    Now the column of NoOfNonClusteredIndexes will return count of non clustered indexes only.

    The query may be further optimized.

    • Yes, In modified script, type=2 portion is missing from right join , this change will be enough to get count of non-clustered indexes

  4. Pingback: SQL SERVER – Weekly Series – Memory Lane – #050 | Journey to SQL Authority with Pinal Dave

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s