0

我正在尝试修改以下脚本以仅指向与特定视图关联vw_foo的索引。这可能吗?

SELECT name AS index_name,
STATS_DATE(OBJECT_ID, index_id) AS StatsUpdated
FROM sys.indexes

编辑

当我说“关联”时,我指的是用于创建视图的基础表上的索引

4

2 回答 2

1

现有的答案可能正朝着检查名称的方向发展,sys.objects但从未这样做。但无论如何都没有必要这样做,因为该OBJECT_ID()功能可以让您object_id以一种干净的方式获得:

SELECT name AS index_name,
STATS_DATE(OBJECT_ID, index_id) AS StatsUpdated
FROM sys.indexes
WHERE object_id = OBJECT_ID('vw_foo')
于 2013-04-15T09:34:15.903 回答
1

可能对你有帮助——

SELECT 
      SCHEMA_NAME(o.[schema_id]) + '.' + o.name
    , s.name
    , statistics_update_date = STATS_DATE(o.[object_id], stats_id) 
FROM sys.objects o
JOIN sys.stats s ON o.[object_id] = s.[object_id]
WHERE o.[type] = 'V' AND o.name = 'vw_foo'

DECLARE @table_name SYSNAME
SELECT @table_name = 'dbo.vw_foo'

已编辑

当我说“关联”时,我指的是视图使用的表上的索引

此查询返回使用指定表的视图列表 + 显示有关已使用索引的附加信息 -

SELECT 
      o.table_name
    , b.view_name
    , i.name
    , stast_updates = STATS_DATE(i.[object_id], i.index_id)
    , dm_ius.last_user_seek
    , dm_ius.last_user_scan
    , dm_ius.last_user_lookup
    , dm_ius.last_user_update
    , dm_ius.user_updates
    , dm_ius.user_lookups
    , dm_ius.user_scans
    , dm_ius.user_seeks
FROM (
    SELECT
          table_name = s.name + '.' + o.name
        , o.[object_id]
    FROM sys.objects o
    JOIN sys.schemas s ON o.[schema_id] = s.[schema_id]
    WHERE o.[type] = 'U'
        AND s.name + '.' + o.name = @table_name
) o
JOIN sys.indexes i ON o.[object_id] = i.[object_id]
                  AND i.[type] > 0
                  AND i.is_disabled = 0
                  AND i.is_hypothetical = 0
LEFT JOIN sys.dm_db_index_usage_stats dm_ius ON i.index_id = dm_ius.index_id AND dm_ius.[object_id] = i.[object_id]  
OUTER APPLY (
    SELECT 
          view_name = r.referencing_schema_name + '.' + r.referencing_entity_name
        , r.referencing_id
    FROM sys.dm_sql_referencing_entities (o.table_name, 'OBJECT') r
    JOIN sys.objects o2 ON r.referencing_id = o2.[object_id]
    WHERE o2.[type] = 'V'
) b
WHERE b.view_name IS NOT NULL
于 2013-04-15T09:29:45.687 回答