Dapper QueryMultiple 在数据库上多次显示所有查询 运行
Dapper QueryMultiple shows all queries run multiple times on DB
在 dapper 中使用 QueryMultiple 时,是否所有查询都在数据库上 运行 多次?
.Net核心代码示例:
var query1 = "select * from [User]";
var query2 = "select * from UserRole";
var multiQuery = $"{query1}; {query2}";
using (var multi = await _dbConnection.QueryMultipleAsync(multiQuery))
{
var userResponse = multi.Read<UserResponse>();
var userRoleResponse = multi.Read<UserRoleResponse>();
//DO SOMETHING
}
当我查看数据库上的查询 运行 时,我看到以下内容:
Time
Query
2021-11-12 17:17:47.673
select * from [User]; select * from UserRole
2021-11-12 17:17:47.673
select * from [User]; select * from UserRole
SQL 查询以查看数据库中的最新查询:
SELECT top 10 deqs.last_execution_time AS [Time], dest.text AS [Query]
FROM sys.dm_exec_query_stats AS deqs
CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
ORDER BY deqs.last_execution_time DESC
我期待看到以下内容:
Time
Query
2021-11-12 17:17:47.673
select * from [User]
2021-11-12 17:17:47.673
select * from UserRole
这是否意味着 Dapper 的 QueryMultiple 运行多次查询所有查询?
只是希望了解它是如何工作的,因为我们正在研究潜在的性能影响。
数据库是 Sql 服务器。
Does this mean that Dapper's QueryMultiple is running all queries multiple times?
不,您误读了 DMV。
sys.dm_exec_query_stats has one row per query, but sys.dm_exec_sql_text returns 整个批处理(或存储过程主体),而不是单个查询。因此,您必须使用 statement_start_offset
和 statement_end_offset
来提取单个查询。
这是来自 docs 的示例:
SELECT TOP 5 query_stats.query_hash AS "Query Hash",
SUM(query_stats.total_worker_time) / SUM(query_stats.execution_count) AS "Avg CPU Time",
MIN(query_stats.statement_text) AS "Statement Text"
FROM
(SELECT QS.*,
SUBSTRING(ST.text, (QS.statement_start_offset/2) + 1,
((CASE statement_end_offset
WHEN -1 THEN DATALENGTH(ST.text)
ELSE QS.statement_end_offset END
- QS.statement_start_offset)/2) + 1) AS statement_text
FROM sys.dm_exec_query_stats AS QS
CROSS APPLY sys.dm_exec_sql_text(QS.sql_handle) as ST) as query_stats
GROUP BY query_stats.query_hash
ORDER BY 2 DESC;
在 dapper 中使用 QueryMultiple 时,是否所有查询都在数据库上 运行 多次?
.Net核心代码示例:
var query1 = "select * from [User]";
var query2 = "select * from UserRole";
var multiQuery = $"{query1}; {query2}";
using (var multi = await _dbConnection.QueryMultipleAsync(multiQuery))
{
var userResponse = multi.Read<UserResponse>();
var userRoleResponse = multi.Read<UserRoleResponse>();
//DO SOMETHING
}
当我查看数据库上的查询 运行 时,我看到以下内容:
Time | Query |
---|---|
2021-11-12 17:17:47.673 | select * from [User]; select * from UserRole |
2021-11-12 17:17:47.673 | select * from [User]; select * from UserRole |
SQL 查询以查看数据库中的最新查询:
SELECT top 10 deqs.last_execution_time AS [Time], dest.text AS [Query]
FROM sys.dm_exec_query_stats AS deqs
CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
ORDER BY deqs.last_execution_time DESC
我期待看到以下内容:
Time | Query |
---|---|
2021-11-12 17:17:47.673 | select * from [User] |
2021-11-12 17:17:47.673 | select * from UserRole |
这是否意味着 Dapper 的 QueryMultiple 运行多次查询所有查询?
只是希望了解它是如何工作的,因为我们正在研究潜在的性能影响。
数据库是 Sql 服务器。
Does this mean that Dapper's QueryMultiple is running all queries multiple times?
不,您误读了 DMV。
sys.dm_exec_query_stats has one row per query, but sys.dm_exec_sql_text returns 整个批处理(或存储过程主体),而不是单个查询。因此,您必须使用 statement_start_offset
和 statement_end_offset
来提取单个查询。
这是来自 docs 的示例:
SELECT TOP 5 query_stats.query_hash AS "Query Hash",
SUM(query_stats.total_worker_time) / SUM(query_stats.execution_count) AS "Avg CPU Time",
MIN(query_stats.statement_text) AS "Statement Text"
FROM
(SELECT QS.*,
SUBSTRING(ST.text, (QS.statement_start_offset/2) + 1,
((CASE statement_end_offset
WHEN -1 THEN DATALENGTH(ST.text)
ELSE QS.statement_end_offset END
- QS.statement_start_offset)/2) + 1) AS statement_text
FROM sys.dm_exec_query_stats AS QS
CROSS APPLY sys.dm_exec_sql_text(QS.sql_handle) as ST) as query_stats
GROUP BY query_stats.query_hash
ORDER BY 2 DESC;