选择MAX或按限制订购1 [英] Select MAX or Order By Limit 1
问题描述
要继续研究这个问题:我发现一些结果与Sean McSomething的描述截然不同:
To follow up on this question: I found some results very different from what Sean McSomething describes:
我有一个约有3亿行的表.
I have a table with about 300M rows.
Select max(foo) from bar;
大约需要15秒.运行
Select max(foo) from bar;
takes about 15 sec. to run
Select foo from bar order by foo desc limit 1;
需要3秒钟.运行
Sean的声明看起来像MIN()是可行的-在最坏的情况下它更快,在最好的情况下没有区别"只是在这种情况下不成立...但我不知道为什么.谁能提供解释?
Sean's statement "It looks like MIN() is the way to go - it's faster in the worst case, indistinguishable in the best case" just doesn't hold for this case...but I have no idea why. Can anyone offer an explanation?
由于无法在此处显示表的结构:假设bar是ndb_cluster中的一个没有关系的表,因此foo是一个没有索引的任意数据点.
Since I am unable to show the table's structure here: assume that bar is a table in an ndb_cluster with no relations, foo is an arbitrary data point with no index.
推荐答案
为避免完全通过,请在foo
列上添加INDEX
.
To avoid a full pass, add an INDEX
on foo
column.
这篇关于选择MAX或按限制订购1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!