[英]Is it possible to write a T-SQL query for “Get largest 2” that has O(n) run-time complexity?
Obviously it is possible to find the largest 2 elements in a collection in O(n) time complexity. 显然,有可能以O(n)时间复杂度找到集合中最大的2个元素。 I wondering if this can be done in SQL Server or if any query that has an
ORDER BY
will always run in at least O(n)*log(n) time. 我想知道是否可以在SQL Server中完成此操作,或者是否具有
ORDER BY
任何查询始终至少在O(n)* log(n)时间中运行。
"if any query . . . has an ORDER BY
will always run in O(n logn) time." “如果任何查询
ORDER BY
具有ORDER BY
则始终会在O(n logn)时间内运行。”
False. 假。 That is not "obvious" at all.
那根本不是“显而易见的”。 The following query:
以下查询:
select top (2) t.*
from t
order by t.size desc;
Will run in O(1) time (I think) with a clustered index on t(size)
and in O(log n) time with a b-tree index. (我认为)将在O(1)的时间内运行,并且聚集索引在
t(size)
,在O(log n)的时间内运行在b树索引上。 For normal data sizes, O(log n) is pretty close to constant time -- just put in a constant of 20 or so. 对于正常的数据大小,O(log n)非常接近恒定时间-只需输入20左右的常数即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.