mysql - 如何使用别名列加快此查询的速度?

标签 mysql sql execution-time mysql-variables

所以我在 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/

相关文章:

MySQL 外连接与数据透视表

比较各种 pthread 结构的性能

php - 如何设置 PHP 脚本的最长执行时间?

mysql - 如何在xampp中为varchar值添加默认值

mysql - Moodle:安装失败 - 检测到编码错误

javascript - 无法使用 jQuery load() 将 php 结果加载到 div

具有有限层次结构的 MySQL 自连接

jquery - 手机间隙 : Cannot multiple SQL request in same page

sql - 如何忽略 Oracle 分析函数中的空值

c++ - 计算我的 C++ 程序的执行时间和大 O 时间