性能问题加入系统表& DB上有多个分区表的DMV?

时间:2017-07-07 15:10:38

标签: sql-server dmv

我在我的环境中的每个数据库上运行以下查询,每天两次,以进行跟踪。我有一个相对繁忙的服务器,其数据库的查询次数为95k(此DB包含许多约20个表,可以有多个分区,有些超过300个)。在我所有的其他服务器上它运行正常,但对于这个数据库我已经开始遇到问题,它曾经在几分钟内完成,但现在将运行超过18分钟。

如果我只是做一个COUNT,它会在大约1秒后回复93462。

SELECT i.name需要2秒(返回93462行)

SELECT p.rows至少需要18分钟

sp_whoisactive不显示WAIT_INFO。我尝试使用SQL Sentry Plan Explorer的WAIT STATS功能,但由于查询永远不会完成(我让它运行的最长时间是18分钟),我没有得到任何结果。

这是在SQL Server 2016 SP1-CU2,Enterprise,64位

非常感谢所有人的帮助!

SELECT distinct 'mydbname' AS database_name,
        SCHEMA_NAME(o.schema_id) AS table_schema,
        o.name AS table_name,
        i.name AS index_name,
        o.type_desc as type_desc_full,
        CASE i.type 
                WHEN 0 THEN 'HP' 
                WHEN 1 THEN 'C' 
                WHEN 2 THEN 'NC'
                WHEN 3 THEN 'XM'
                WHEN 4 THEN 'Sp'
                WHEN 5 THEN 'CS' --clustered columnstore, which doesn't exist (yet)
                WHEN 6 THEN 'cs' --nonclustered columnstore, available in 2012.
        ELSE 'UK' END AS type_desc_brief,
        MAX(a.type) as allocation_type,
        MAX(p.rows) AS rows_in_table,
        SUM(a.total_pages * 8/1024) AS Total_MB,
        avg(u.user_seeks) AS user_seeks,
        MAX(last_user_seek) AS last_user_seek,
        avg(u.user_lookups) AS user_lookups,
        MAX(last_user_lookup) AS last_user_lookup,
        avg(u.user_scans) AS  user_scans,
        MAX(last_user_scan) AS last_user_scan,
        avg(u.user_updates) AS  user_updates,
        MAX(last_user_update) AS last_user_update
FROM    sys.indexes i
JOIN    sys.objects o
        ON i.object_id = o.object_id
LEFT JOIN sys.dm_db_index_usage_stats u
        ON i.object_id = u.object_id
           AND i.index_id = u.index_id
           AND u.database_id = DB_ID()
inner JOIN sys.partitions as p
        on i.object_id = p.object_id
        and i.index_id = p.index_id
INNER JOIN sys.allocation_units AS a
        on (a.type = 2 AND p.partition_id = a.container_id)
        OR ((a.type = 1 OR a.type = 3) AND p.hobt_id = a.container_id)
--WHERE   o.type_desc NOT IN ('SYSTEM_TABLE', 'INTERNAL_TABLE','SQL_TABLE_VALUED_FUNCTION')           -- No system tables!
GROUP BY SCHEMA_NAME(o.schema_id),
        o.name,
        i.name,
        o.type_desc,
        CASE i.type 
                WHEN 0 THEN 'HP' 
                WHEN 1 THEN 'C' 
                WHEN 2 THEN 'NC'
                WHEN 3 THEN 'XM'
                WHEN 4 THEN 'Sp'
                WHEN 5 THEN 'CS' --clustered columnstore
                WHEN 6 THEN 'cs' --nonclustered columnstore, available in 2012.
        ELSE 'UK' END

1 个答案:

答案 0 :(得分:0)

由于访问了sys.partitions,我看到了阻塞。我相信sys.partitions.rows来自stats对象。可能是从很多很多单个对象中收集的,任何一个统计对象都可能是阻塞的位置。

sys.dm_session_wait_stats会消除光线吗?