问题描述
有没有更好的方法来做这样的查询:
Is there a better way of doing a query like this:
SELECT COUNT(*)
FROM (SELECT DISTINCT DocumentId, DocumentSessionId
FROM DocumentOutputItems) AS internalQuery
我需要计算此表中不同项目的数量,但不同项目超过两列.
I need to count the number of distinct items from this table but the distinct is over two columns.
我的查询工作正常,但我想知道是否可以仅使用一个查询(不使用子查询)获得最终结果
My query works fine but I was wondering if I can get the final result using just one query (without using a sub-query)
推荐答案
如果您想提高性能,可以尝试在两列的散列值或串联值上创建持久计算列.
If you are trying to improve performance, you could try creating a persisted computed column on either a hash or concatenated value of the two columns.
一旦它被持久化,如果该列是确定性的并且您使用的是健全的"数据库设置,则可以对其进行索引和/或可以在其上创建统计信息.
Once it is persisted, provided the column is deterministic and you are using "sane" database settings, it can be indexed and / or statistics can be created on it.
我相信计算列的不同计数将等同于您的查询.
I believe a distinct count of the computed column would be equivalent to your query.
这篇关于在多列上计算 DISTINCT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!