index

  • Hello,

    does somebody know query for result about index where procedure or query use this index ?

  • Do you mean checking if an index is used?

    How to get index usage information in SQL Server[/url]

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • tony28 (5/5/2014)


    Hello,

    does somebody know query for result about index where procedure or query use this index ?

    You can check that by query plan

    Below query give you all non clustered index usage of tables..

    SELECT OBJECT_NAME(ddius.[object_id]) AS [Table Name] ,

    i.name AS [Index Name] ,

    i.index_id ,

    user_updates AS [Total Writes] ,

    user_seeks + user_scans + user_lookups AS [Total Reads] ,

    user_updates - ( user_seeks + user_scans + user_lookups )

    AS [Difference]

    FROM sys.dm_db_index_usage_stats AS ddius WITH ( NOLOCK )

    INNER JOIN sys.indexes AS i WITH ( NOLOCK )

    ON ddius.[object_id] = i.[object_id]

    AND i.index_id = ddius.index_id

    WHERE OBJECTPROPERTY(ddius.[object_id], 'IsUserTable') = 1

    AND ddius.database_id = DB_ID()

    -- AND user_updates > ( user_seeks + user_scans + user_lookups )

    AND i.index_id > 1

    ORDER BY [Difference] DESC ,

    [Total Writes] DESC ,

    [Total Reads] ASC ;

    and this one will give you particular table's index

    SELECT s.name, D.*

    FROM sys.dm_db_index_usage_stats D

    LEFT OUTER JOIN sys.indexes S

    ON D.object_id = S.object_Id AND D.index_id = S.index_id

    WHERE database_id = DB_ID('DBname') AND d.object_id = OBJECT_ID('tablename');

  • try these two, searches plan cache for your index name

    WITH XMLNAMESPACES (DEFAULT 'http://schemas.microsoft.com/sqlserver/2004/07/showplan')

    SELECT

    DB_NAME(E.dbid) AS [DBName],

    object_name(E.objectid, dbid) AS [ObjectName],

    P.cacheobjtype AS [CacheObjType],

    P.objtype AS [ObjType],

    E.query_plan.query('count(//RelOp[@LogicalOp = ''Index Scan'' or @LogicalOp = ''Clustered Index Scan'']/*/Object[@Index=''[MyIndex]''])') AS [ScanCount],

    E.query_plan.query('count(//RelOp[@LogicalOp = ''Index Seek'' or @LogicalOp = ''Clustered Index Seek'']/*/Object[@Index=''[MyIndex]''])') AS [SeekCount],

    E.query_plan.query('count(//Update/Object[@Index=''[MyIndex]''])') AS [UpdateCount],

    P.refcounts AS [RefCounts],

    P.usecounts AS [UseCounts],

    E.query_plan AS [QueryPlan]

    FROM sys.dm_exec_cached_plans P

    CROSS APPLY sys.dm_exec_query_plan(P.plan_handle) E

    WHERE

    E.dbid = DB_ID('XPLAN') AND

    E.query_plan.exist('//*[@Index=''[your index name]'']') = 1

    -- OR (BETTER)

    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

    DECLARE @IndexName AS NVARCHAR(128);

    set @IndexName = your index name';

    -- Make sure the name passed is appropriately quoted

    IF (LEFT(@IndexName, 1) <> '[' AND RIGHT(@IndexName, 1) <> ']') SET @IndexName = QUOTENAME(@IndexName);

    --Handle the case where the left or right was quoted manually but not the opposite side

    IF LEFT(@IndexName, 1) <> '[' SET @IndexName = '['+@IndexName;

    IF RIGHT(@IndexName, 1) <> ']' SET @IndexName = @IndexName + ']';

    -- Dig into the plan cache and find all plans using this index

    ;WITH XMLNAMESPACES

    (DEFAULT 'http://schemas.microsoft.com/sqlserver/2004/07/showplan')

    SELECT

    stmt.value('(@StatementText)[1]', 'varchar(max)') AS SQL_Text,

    obj.value('(@Database)[1]', 'varchar(128)') AS DatabaseName,

    obj.value('(@Schema)[1]', 'varchar(128)') AS SchemaName,

    obj.value('(@Table)[1]', 'varchar(128)') AS TableName,

    obj.value('(@Index)[1]', 'varchar(128)') AS IndexName,

    obj.value('(@IndexKind)[1]', 'varchar(128)') AS IndexKind,

    cp.plan_handle,

    query_plan

    FROM sys.dm_exec_cached_plans AS cp

    CROSS APPLY sys.dm_exec_query_plan(plan_handle) AS qp

    CROSS APPLY query_plan.nodes('/ShowPlanXML/BatchSequence/Batch/Statements/StmtSimple') AS batch(stmt)

    CROSS APPLY stmt.nodes('.//IndexScan/Object[@Index=sql:variable("@IndexName")]') AS idx(obj)

    OPTION(MAXDOP 1, RECOMPILE);

    ---------------------------------------------------------------------

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply