在分组之前排序,反之亦然:哪个更快?
只是把这个扔在这里,因为在听到同事的困境后我很好奇。
在一般的生产环境查询中,先排序再分组更快,还是先分组再排序更快?
是的,一个通用生产环境。我知道这取决于很多事情:您要查询多少行,初始结果集有多少行,有多少列,我正在使用什么数据库模式,明天是否会下雨等等,我知道,我知道。我说的是一般性的,因为我实际上没有任何界限来明确地激发我的好奇心。
我们只是说,根据您的经验,哪个往往更快?
Just throwing this out here 'cause I became curious after hearing my colleague's predicament.
In a general production environ query, would it be faster to sort first before grouping, or to group first before sorting?
And yeah, a general production environ. I know that this is dependent on a lot of things: how many rows you're querying from, how many rows the initial result set is, how many columns, what DB schema I'm using, whether or not it's bound to rain tomorrow, etc. I know, I know. I'm saying general because I don't really have any bounds to explicitly set this curiosity of mine on.
Let's just say, based on your experience, which tends to be faster?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要分组,必须首先进行排序(引擎将执行此操作)。如果你尝试先排序,那么结果不一定是排序的:
所以不要尝试先排序,因为无论如何你都需要排序,这只会让你的代码变得混乱。
To group it is necessary to sort first (the engine will do it). And if you try to sort first then the result is not necessarily sorted:
So don't try to sort first because you will need to sort after anyway and it will just make your code confusing.