简体   繁体   English

为什么 GROUP BY 比 SQL 中的 DISTINCT 快?

[英]Why is GROUP BY faster than DISTINCT in SQL?

This article explains that GROUP BY is faster than DISTINCT https://sqlperformance.com/2017/01/t-sql-queries/surprises-assumptions-group-by-distinct本文说明 GROUP BY 比 DISTINCT https://sqlperformance.com/2017/01/t-sql-queries/surprises-assumptions-group-by-distinct 快

Why doesn't the implementation of DISTINCT just use the same underlying logic if it's faster?如果 DISTINCT 的实现更快,为什么不使用相同的底层逻辑?

A very key part of this article – easily overlooked – is the reference to "execution plans."本文的一个非常关键的部分——很容易被忽视——是对“执行计划”的引用。 As presented by the EXPLAIN *query* verb.正如EXPLAIN *query*动词所呈现的那样。

Every time you present an SQL query to the database engine, it "compiles it" into an "execution plan," which is what the underlying logic actually "executes" to give you the results that you want.每次您向数据库引擎提交 SQL 查询时,它都会将其“编译”为“执行计划”,这是底层逻辑实际“执行”的内容,以便为您提供所需的结果。 The formulation of this plan is entirely "case by case," and it can be based on quite a number of things, including table sizes and "statistics" which the engine is constantly accumulating about the data which each table contains.该计划的制定完全是“逐案”,它可以基于很多东西,包括表大小和引擎不断积累的关于每个表包含的数据的“统计信息”。

(The EXPLAIN verb will present this plan-information to you, for any query. The format, content, and purpose of it is entirely engine-dependent, but the principle is not.) (对于任何查询, EXPLAIN动词都会向您显示此计划信息。它的格式、内容和目的完全取决于引擎,但原理不是。)

Therefore, the "take-away" that I suggest that you take from this article is not that GROUP BY is [always...] "faster than DISTINCT ," but rather, how the engine goes about its business such that [sometimes] this is the case.因此,我建议您从本文中获得的“外卖”不是GROUP BY [总是...]“比DISTINCT快”,而是引擎如何开展其业务,使得[有时]就是这样。 It goes without saying that "every engine is different," but the principles governing all of them, by now, are more-or-less the same.不言而喻,“每个引擎都是不同的”,但到目前为止,支配所有引擎的原则或多或少是相同的。

Whether or not "this actually turns out to be true, for you, with your data, in your case," is less important than your understanding of what actually happens behind-the-scenes when you submit any SQL query to a database engine.当您将任何SQL 查询提交到数据库引擎时,“对于您而言,对于您的数据,这实际上是否属实”并不重要。 There's much more to it than meets the eye...它的意义远不止眼前所见……

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM