所以我在 SO 上找到了这个代码片段。它本质上为 MySQL 伪造了一个“row_number()”函数。它执行得非常快,这是我喜欢和需要的,但我无法在末尾添加 where 子句。
select
@i:=@i+1 as iterator, t.*
from
big_table as t, (select @i:=0) as foo
在 where iterator = 875
中添加会产生错误。
上面的代码片段执行时间约为 0.0004 秒。我知道我可以将它包装在另一个查询中作为子查询,但它会变得非常慢。
select * from (
select
@i:=@i+1 as iterator, t.*
from
big_table as t, (select @i:=0) as foo) t
where iterator = 875
上面的代码片段需要 10 多秒才能执行。
有什么方法可以加快速度吗?
最佳答案
在这种情况下,您可以将 LIMIT
用作 WHERE
:
select
@i:=@i+1 as iterator, t.*
from
big_table as t, (select @i:=874) as foo
LIMIT 875,1
因为您只想要记录 875,所以这会很快。
关于mysql - 如何使用别名列加快此查询的速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38711635/