强制“group by”对 DESC 进行排序会加速代码还是减慢代码速度?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据我的测试,添加任何排序修饰符来分组,例如
-
来更改排序顺序会减慢速度。但是,您可以指定:
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:
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.我认为您错了:
GROUP BY
不会对数据进行排序。这是默认的 MySQL 行为,因为 MySQL 添加了与您设置的GROUP BY
相同的ORDER BY
,正如您在第一句话中提到的那样。因此,如果您使用
ORDER BY NULL
禁用排序,则根本不会进行排序。 GROUP BY 只会将行分组在一起,如果可能的话使用索引。因此,“技巧”是错误的,因为您将删除在mydate
上使用索引的能力。只要索引适合,GROUP BY
的性能就很好。所以:
如果你在 (
mydate
) 上有一个索引,那么应该非常快,并且应该一样快(根据表结构,MyISAM 在相反的顺序上会慢一点)。
如果您有 WHERE 子句,请检查是否已在索引中添加列,例如:
将需要 (
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 sameORDER BY
as theGROUP 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. TheGROUP 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 onmydate
.GROUP BY
performs great as long as the index is good for it.So:
should be really fast if you have an index on (
mydate
), andshould 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:
will need an index on (
field1
,mydate
).放慢速度
这里发生的情况是,您要求 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/