强制“group by”对 DESC 进行排序会加速代码还是减慢代码速度?

发布于 2024-12-01 22:06:12 字数 469 浏览 0 评论 0原文

在 MySQL 中,group by 执行隐式按 ASC 排序
如果您想添加 ORDER BY ASC,这非常有用,因为这样结果就已经排序了。

但是如果你想要ORDER BY .. DESC MySQL 必须完全相反地对结果集进行排序。

这个技巧会加速选择、减慢速度还是什么都不做

SELECT field1, field2 FROM atable 
GROUP BY -mydate   -- group by trick to force a `group by ... desc`
ORDER BY mydate DESC

我知道我可以只对代码计时,但我希望对当前的问题有更深入的了解。< br> 所有相关的索引都自然就位了,因为没有索引进行优化是愚蠢的。

In MySQL group by does an implicit order by ASC.
This is great if you wanted to add an ORDER BY ASC because then the results are already ordered.

But if you want to ORDER BY .. DESC MySQL has to order the resultset exactly the other way round.

Will this trick speed up the select, slow it down, or do nothing at all

SELECT field1, field2 FROM atable 
GROUP BY -mydate   -- group by trick to force a `group by ... desc`
ORDER BY mydate DESC

I know I can just time the code, but I'm looking to gain some deeper insight into the issues at hand.
All the relevant indexes are in place naturally, because it would be silly to optimize without indexes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨后咖啡店 2024-12-08 22:06:12

根据我的测试,添加任何排序修饰符来分组,例如 - 来更改排序顺序会减慢速度。

但是,您可以指定:

SELECT id, name, sum(amount) FROM customers GROUP BY id DESC

MySQL 会很乐意以 DESC 顺序对结果进行排序,而不需要额外的 order by 子句。这不会像添加 - 那样产生额外的运行时间。

From my tests, adding any sort a modifier to group by like - to change the sort order slows things down.

However you are allowed to specify:

SELECT id, name, sum(amount) FROM customers GROUP BY id DESC

And MySQL will happily order the results in DESC order without needing an extra order by clause. This will not incur the extra runtime that adding the - does.

孤凫 2024-12-08 22:06:12

我认为您错了:GROUP BY 不会对数据进行排序。这是默认的 MySQL 行为,因为 MySQL 添加了与您设置的 GROUP BY 相同的 ORDER BY,正如您在第一句话中提到的那样。

因此,如果您使用 ORDER BY NULL 禁用排序,则根本不会进行排序。 GROUP BY 只会将行分组在一起,如果可能的话使用索引。因此,“技巧”是错误的,因为您将删除在 mydate 上使用索引的能力。只要索引适合,GROUP BY 的性能就很好。

所以:

SELECT field1, field2 FROM atable 
GROUP BY mydate
ORDER BY NULL

如果你在 (mydate) 上有一个索引,那么应该非常快,并且

SELECT field1, field2 FROM atable 
GROUP BY mydate
ORDER BY mydate DESC

应该一样快(根据表结构,MyISAM 在相反的顺序上会慢一点)。

如果您有 WHERE 子句,请检查是否已在索引中添加列,例如:

SELECT field1, field2 FROM atable 
WHERE fied1 = 5
GROUP BY mydate
ORDER BY mydate DESC

将需要 (field1,mydate) 上的索引。

I think you're mistaken: GROUP BY doesn't sort data. It's the default MySQL behaviour that does, as MySQL adds the same ORDER BY as the GROUP BY you've set, as you've mentioned in your first sentence.

So, if you disable the sort, by using ORDER BY NULL, there's no sorting at all. The GROUP BY will only group rows together, using indexes if possible. Hence the «trick» is wrong, as you'll remove the ability to use an index on mydate. GROUP BY performs great as long as the index is good for it.

So:

SELECT field1, field2 FROM atable 
GROUP BY mydate
ORDER BY NULL

should be really fast if you have an index on (mydate), and

SELECT field1, field2 FROM atable 
GROUP BY mydate
ORDER BY mydate DESC

should be as fast (depending on the table structure, MyISAM is a little bit slower in reverse order).

If you have a WHERE clause, check that you've added the columns in the index, for example:

SELECT field1, field2 FROM atable 
WHERE fied1 = 5
GROUP BY mydate
ORDER BY mydate DESC

will need an index on (field1,mydate).

栖迟 2024-12-08 22:06:12

放慢速度

这里发生的情况是,您要求 MySQL 根据(可能)非索引列 mydate 对记录进行排序。

任何排序都需要时间,但与非索引列相比,索引列上的排序速度非常快。

这里有一些额外的阅读: http://www.mysqlperformanceblog .com/2006/09/01/order-by-limit-performance-optimization/

Slow It Down

What's happening here is that you are asking MySQL to sort the records based on a (probably) non-indexed column mydate.

Any sort takes time, but sorts on indexed columns are blazing fast compared to non-indexed ones.

Here's some additional reading: http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文