2

Using SQL Server 2012.

Using the system views stored for each database I wanted to keep a summary of index behavior for all indexes across the instance. The code below collects some stats and drops them in a table for later use. The snag is, I had to use a cursor to loop through the databases in the instance. Can anyone suggest a nicer way of doing this without having to use the cursor?

USE Master
GO

-- If the procedure already exists then update it. 
IF OBJECT_ID('sp_index_stats') IS NOT NULL 
    DROP PROCEDURE sp_index_stats
GO

CREATE PROCEDURE sp_index_stats AS
    DECLARE     @dbname     NVARCHAR(128) 
    DECLARE     @finalSQL   NVARCHAR(1024)

    -- Results of the query are put into a table in Demo_DB. 
    -- So empty it now for the new results. 

    TRUNCATE TABLE Demo_DB.dbo.index_stats

    -- Use a cursor to get a list of all the tables in the instance. 
    DECLARE database_cursor CURSOR LOCAL FOR
        SELECT  name
        FROM    Master.dbo.sysdatabases
        WHERE   name NOT IN ('master', 'model', 'msdb', 'tempdb')

    OPEN database_cursor

    FETCH NEXT FROM database_cursor INTO @dbname

    WHILE @@FETCH_STATUS = 0
    BEGIN
        -- Each database will contain the same structue, so I need
        -- to create a standard query just changing the database name.
        SET @finalSQL = 'INSERT INTO Demo_DB.dbo.index_stats '

        -- Sort out the select statement.
        SET @finalSQL = @finalSQL + 'SELECT sdb.name, st.name, si.name, si.type_desc, '
        SET @finalSQL = @finalSQL + 'si.is_primary_key, si.fill_factor, '
        SET @finalSQL = @finalSQL + 'sd.user_scans, sd.last_user_scan, '
        SET @finalSQL = @finalSQL + 'sd.user_updates, sd.last_user_update '

        -- The 'from' and 'joins' need to be adjusted slightly for each databse. 
        SET @finalSQL = @finalSQL + 'FROM ' + @dbname + '.sys.tables st '
        SET @finalSQL = @finalSQL + 'JOIN ' + @dbname + '.sys.indexes si ON (si.object_id = st.object_id) '
        SET @finalSQL = @finalSQL + 'JOIN ' + @dbname + '.sys.dm_db_index_usage_stats sd ON (si.object_id = sd.object_id) '
        SET @finalSQL = @finalSQL + 'JOIN ' + @dbname + '.sys.databases sdb ON (sdb.database_id = sd.database_id) ;'

        -- Ok, let's run that...
        EXEC (@finalSQL) 

        -- Get the next database in the instance..,
        FETCH NEXT FROM database_cursor INTO @dbname
    END
    CLOSE database_cursor
    DEALLOCATE database_cursor
GO

Regards,

JC.

6
  • 1
    The is an undocumented procedure named sp_MSforeachdb. Yet, it seems that fellow T-SQL programmers feel that it shouldn't be used: stackoverflow.com/questions/20131930/… An alternative to cursors is to use While as described here: stackoverflow.com/questions/61967/…
    – Ralph
    Oct 8, 2015 at 11:07
  • What is the problem with the cursor? Do you think another method would be faster? Why? The main cost of this procedure at runtime is going to be the individual pulls of all that stats/metadata from each database, not the scaffolding around generating those individual queries. Oct 8, 2015 at 13:50
  • @Ralph It most definitely should not be used. Also, why do you think a while loop is any different from a cursor? Oct 8, 2015 at 13:52
  • @AaronBertrand: I don't think that loops are better than cursors. Nor do I believe the opposite. But as far as I can tell the discussion on the web on this topic is somewhat controversial: some favor cursors, some favor loops with table variables and PK, and some say it depends on the case (server version, setup, volume, etc.). James asked for an alternative to a cursor and I made him aware of an alternative (without assessing it at the same time). I hope that's OK.
    – Ralph
    Oct 8, 2015 at 14:09
  • @Ralph it's just a common misconception that while loops are faster than cursors. Yes, it's an alternative, kind of like a red Ferrari is an alternative to a yellow Ferrari. Oct 8, 2015 at 14:13

1 Answer 1

0

What about concatenation?

Something like this (not reproduced the whole stored procedure, but hopefully you get the gist)

DECLARE @SQL VARCHAR(MAX)

SET @SQL = ''

SELECT @SQL = @SQL + 
'INSERT INTO Demo_DB.dbo.index_stats ' +
'SELECT sdb.name, st.name, si.name, si.type_desc, ' +
'si.is_primary_key, si.fill_factor, ' +
'sd.user_scans, sd.last_user_scan, ' +
'sd.user_updates, sd.last_user_update ' +
'FROM ' + name + '.sys.tables st ' +
'JOIN ' + name + '.sys.indexes si ON (si.object_id = st.object_id) ' +
'JOIN ' + name + '.sys.dm_db_index_usage_stats sd ON (si.object_id = sd.object_id) ' +
'JOIN ' + name + '.sys.databases sdb ON (sdb.database_id = sd.database_id) ;'  
FROM    Master.dbo.sysdatabases
WHERE   name NOT IN ('master', 'model', 'msdb', 'tempdb')

SELECT @SQL

(Obviously not EXECing anything at this point).

1
  • Thank you Fruitbat, that's exactly it. Aside from the debate over cursors and loops, when I wrote the code out I was sure there was a better SQL solution, but couldn't see it. Oct 9, 2015 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.