【问题标题】:In SQL Server, what is the difference between a user query and a system query?在 SQL Server 中,用户查询和系统查询有什么区别?
【发布时间】:2015-08-12 23:56:07
【问题描述】:

sys.dm_db_missing_index_group_stats 的文档使用了“用户查询”和“系统查询”这两个术语,但并未定义它们的含义。例如:

  • user_seeks:“由用户查询引起的搜索次数,组中推荐的索引本可以用于。”
  • system_seeks:“由系统查询引起的搜索次数,例如自动统计查询,组中推荐的索引可以用于。”

根据System query to determine full (including inherited from AD ROLES) view of permissions in a database? 使用该术语的方式,我假设系统查询是针对system tables 的查询。那么用户查询必须是针对user tables的查询。

有人对这两个术语的定义有更权威的来源吗?我之所以问,是因为我正在使用 sys.dm_db_missing_index_group_stats 的结果,并考虑如何在决定是否应用索引时正确权衡 avg_user_impact 与 avg_system_impact。

【问题讨论】:

  • 我认为用户查询是用户在 sql server 上执行的查询,而系统查询是 sql server [在场景任务后面] 运行的查询。如果您曾经运行过跟踪,您将看到 sql server 必须做多少工作才能保持 sql server 实例运行,即使没有用户在服务器上执行任何查询。
  • 我还没有看到这个词是这样使用的。例如,sqlserverperformance.wordpress.com/2010/09/29/… 有一个名为“SQL Azure System Queries”的脚本,其中包含一组针对 sys.* 表的 SELECT。但 SQL Server 文档从未出现并实际定义了该术语。

标签: sql-server sql-server-2012 database-performance sqlperformance


【解决方案1】:

来自What causes system_scans in sys.dm_db_index_usage_stats to count up?问题的答案

我了解系统扫描/查找列与 统计更新和索引维护。我问过 Brent Ozar 什么时候 大约一个月前在德国展示,他证实了这一点 我。

我问过他关于在 系统。他说你只需要关心用户查找 和用户扫描,系统扫描实际上只是系统端 操作。所以一个只有“系统”活动的索引还没有被 自上次重置索引使用统计信息以来由查询使用。

我通过在AdventureWorks2012 上运行一些查询部分确认了这一点:

/* Get the statistics from Sales.SalesOrderHeader */
EXEC sp_helpstats 'Sales.SalesOrderHeader'

-- Results: 
/*
statistics_name             statistics_keys
-------------------------------------------
_WA_Sys_00000004_4B7734FF   DueDate
_WA_Sys_00000008_4B7734FF   SalesOrderNumber
_WA_Sys_0000000D_4B7734FF   TerritoryID
_WA_Sys_0000000E_4B7734FF   BillToAddressID
_WA_Sys_0000000F_4B7734FF   ShipToAddressID
_WA_Sys_00000010_4B7734FF   ShipMethodID
_WA_Sys_00000011_4B7734FF   CreditCardID
_WA_Sys_00000013_4B7734FF   CurrencyRateID
*/

/* Update the statistics for the SalesOrderNumber stats */
UPDATE STATISTICS Sales.SalesOrderHeader _WA_Sys_00000008_4B7734FF WITH FULLSCAN

/* Get the index usage stats for that index */
SELECT
    DB_NAME(iu.database_id),
    OBJECT_NAME(iu.object_id),
    i.name,
    iu.user_seeks,
    iu.user_scans,
    iu.system_seeks,
    iu.system_scans
FROM sys.dm_db_index_usage_stats iu
JOIN sys.indexes i 
    ON iu.object_id = i.object_id
    AND iu.index_id = i.index_id
WHERE i.name = 'AK_SalesOrderHeader_SalesOrderNumber'

/*
DatabaseName        TableName           IndexName                               
AdventureWorks2012  SalesOrderHeader    AK_SalesOrderHeader_SalesOrderNumber    

user_seeks  user_scans  system_seeks    system_scans
0           0           0               1
*/

/* Seek and scan the index */
SELECT SalesOrderNumber
FROM Sales.SalesOrderHeader
WHERE SalesOrderNumber = N'A'
UNION ALL 
SELECT TOP 100 SalesOrderNumber
FROM Sales.SalesOrderHeader WITH (INDEX(AK_SalesOrderHeader_SalesOrderNumber))

/* 
Running the index usage query from above returns the following: 

DatabaseName        TableName           IndexName                               
AdventureWorks2012  SalesOrderHeader    AK_SalesOrderHeader_SalesOrderNumber    

user_seeks  user_scans  system_seeks    system_scans
1           1           0               1
*/

/* Rebuild the index to remove all usage stats */
ALTER INDEX AK_SalesOrderHeader_SalesOrderNumber ON Sales.SalesOrderHeader REBUILD

因此用户在使用管理视图中搜索和扫描似乎是合适的,并且更新统计信息的系统扫描与上面给出的答案相匹配。

但回到最初的问题,我想说你不必担心sys.dm_db_missing_index_group_stats 上的系统搜索和系统扫描。似乎它们表明 SQL Server 正在尝试查找某种类型的自己的元数据,并且没有正确的内部索引来满足查询。

【讨论】:

    【解决方案2】:

    在 SQL 4.21 之前,我们总是将系统查询称为存储在主数据库中的过程,而用户查询则在用户数据库(不是主数据库)中。

    原因与主数据库中“全局”代码所在位置的命名约定和搜索算法有关。 (sp_MyCode 与 spMyCode)

    在 master 中也定义了扩展存储过程。

    我认为人们不再是这个意思了。

    【讨论】:

      猜你喜欢
      • 2020-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-05
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多