我有两个查询来过滤某些用户 ID,具体取决于问题及其答案。
设想
查询 A 是(原始版本):
SELECT userid
FROM mem..ProfileResult
WHERE ( ( QuestionID = 4
AND QuestionLabelID = 0
AND AnswerGroupID = 4
AND ResultValue = 1
)
OR ( QuestionID = 14
AND QuestionLabelID = 0
AND AnswerGroupID = 19
AND ResultValue = 3
)
OR ( QuestionID = 23
AND QuestionLabelID = 0
AND AnswerGroupID = 28
AND ( ResultValue & 16384 > 0 )
)
OR ( QuestionID = 17
AND QuestionLabelID = 0
AND AnswerGroupID = 22
AND ( ResultValue = 6
OR ResultValue = 19
OR ResultValue = 21
)
)
OR ( QuestionID = 50
AND QuestionLabelID = 0
AND AnswerGroupID = 51
AND ( ResultValue = 10
OR ResultValue = 41
)
)
)
GROUP BY userid
HAVING COUNT(*) = 5
我使用“set statistics time on”和“set statistic io on”来检查 CPU 时间和 io 性能。
结果是:
CPU time = 47206 ms, elapsed time = 20655 ms.
我通过使用 Set Operation 重写了查询 A,让我将其命名为查询 B:
SELECT userid
FROM ( SELECT userid
FROM mem..ProfileResult
WHERE QuestionID = 4
AND QuestionLabelID = 0
AND AnswerGroupID = 4
AND ResultValue = 1
INTERSECT
SELECT userid
FROM mem..ProfileResult
WHERE QuestionID = 14
AND QuestionLabelID = 0
AND AnswerGroupID = 19
AND ResultValue = 3
INTERSECT
SELECT userid
FROM mem..ProfileResult
WHERE QuestionID = 23
AND QuestionLabelID = 0
AND AnswerGroupID = 28
AND ( ResultValue & 16384 > 0 )
INTERSECT
SELECT userid
FROM mem..ProfileResult
WHERE QuestionID = 17
AND QuestionLabelID = 0
AND AnswerGroupID = 22
AND ( ResultValue = 6
OR ResultValue = 19
OR ResultValue = 21
)
INTERSECT
SELECT userid
FROM mem..ProfileResult
WHERE QuestionID = 50
AND QuestionLabelID = 0
AND AnswerGroupID = 51
AND ( ResultValue = 10
OR ResultValue = 41
)
) vv;
CPU时间和经过时间是:
CPU time = 8480 ms, elapsed time = 18509 ms
我的简单分析
从上面的结果可以看出,查询 A 的 CPU Time 是 Elapsed time 的 2 倍以上
我搜索这个案例,大多数人说 CPU time 应该小于 Elapsed time,因为 CPU time 是 CPU 运行这个任务的时间。Elapsed time 包括 I/O 时间和其他类型的时间成本。但一种特殊情况是服务器有多个核心 CPU。但是,我刚刚检查了开发数据库服务器,它有一个单核 CPU。
问题 1
如何解释单核 CPU 环境中查询 A 中的 CPU 时间超过 Elapsed time?
问题2
之后,使用set操作,性能真的提升了吗?
我有这个问题是因为查询 B 的逻辑读取为 280627,高于查询 A 的 241885
Brad McGehee在他的文章中说, “假设所有其他条件相同,查询执行的逻辑读取越少,它的效率就越高,执行的速度就越快。”
比,它是否正确地说即使查询 B 具有比查询 A 更高的逻辑读取,但 CPU 时间明显少于查询 A,查询 B 应该具有更好的性能。