在SQL服务器中,用户查询和系统查询有什么区别?

In SQL Server, what is the difference between a user query and a system query?

sys.dm_db_missing_index_group_stats 的文档使用术语 "user queries" 和 "system queries",但并未定义这些术语的含义。例如:

基于术语在 System query to determine full (including inherited from AD ROLES) view of permissions in a database?, I assume that system queries are queries against system tables. Then user queries must be queries against user tables 的使用方式。

谁有这两个词的定义比较权威的出处?我问是因为我正在使用 sys.dm_db_missing_index_group_stats 的结果,并在决定是否应用索引时考虑如何正确权衡 avg_user_impact 与 avg_system_impact。

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

原因与 master 数据库中 "global" 代码所在位置的命名约定和搜索算法有关。 (sp_MyCode 对比 spMyCode)

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

我认为这不再是人们所说的术语。

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

I understand the system scans/lookups columns are to do with statistics updates & index maintenance. I asked Brent Ozar when he was presenting over in Germany about a month ago and he confirmed this for me.

I asked him about this with regard to identifying unused indexes on a system. He said that you only have to be concerned with User Lookups and User Scans, the system ones are really only system side operations. So an index that only has "system" activity has not been used by queries since the index usage statistics were last reset.

我通过 运行 在 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 服务器试图查找某种类型的它自己的元数据并且没有正确的内部索引来满足查询。