V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
HiHi
V2EX  ›  MySQL

关于 MySQL 之 LIMIT 语句的疑问

  •  
  •   HiHi · 2016-07-09 15:29:55 +08:00 · 2807 次点击
    这是一个创建于 2849 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近写 MySQL 碰到这么个问题. 我只想取数据库中第一条满足条件的记录出来 于是有了这么一条查询语句

    select * from table where conditions limit 1;
    

    但在使用过程中,发现这条语句中的 LIMIT 的实际行为其实似乎是 : 对 WHERE 的结果进行的过滤,即数据库 SCAN 出所有满足条件的结果,然后返回了第一条

    是否 LIMIT 有其它用法或有其它指令,能实现 找出第一条满足条件的记录,并返回 而不是现在的 先找出所有满足条件的记录,然后返回第一条

    如: grep -m 1 re -r filegrep re -r file | head -n 1 的区别

    11 条回复    2016-07-10 14:41:00 +08:00
    liprais
        1
    liprais  
       2016-07-09 15:33:48 +08:00   ❤️ 1
    "If you combine LIMIT row_count with ORDER BY, MySQL ends the sorting as soon as it has found the first row_count rows of the sorted result, rather than sorting the entire result. If ordering is done by using an index, this is very fast. If a filesort must be done, all rows that match the query without the LIMIT clause are selected, and most or all of them are sorted, before the first row_count are found. After the initial rows have been found, MySQL does not sort any remainder of the result set."

    http://dev.mysql.com/doc/refman/5.7/en/limit-optimization.html

    RTFM
    justjavac
        2
    justjavac  
       2016-07-09 15:43:23 +08:00 via Android
    不会的。

    放 MySQL 查询引擎找到合适的记录后,会停止全表扫描的。
    justjavac
        3
    justjavac  
       2016-07-09 15:45:16 +08:00 via Android   ❤️ 1
    当字段有索引时,不会全表扫描。
    当没有索引时会全表扫描。
    但是当使用 limit 1 时,不会全表扫描。
    HiHi
        4
    HiHi  
    OP
       2016-07-09 16:08:34 +08:00
    感谢回复 @liprais @justjavac :

    有官方文档的话,那应该是我的测试方法有𢋷误了
    ``` SQL
    select count(*) from table where conditions limit 10;
    ```
    里面的 count(*)误导了我对 limit 的判断
    bdbai
        5
    bdbai  
       2016-07-09 16:51:50 +08:00 via Android
    试试 explain
    9hills
        6
    9hills  
       2016-07-09 17:38:15 +08:00
    MySQL 没有那么傻。。
    Mac
        7
    Mac  
       2016-07-09 17:43:26 +08:00
    你自己用 heidisql 看返回的查询时间不就知道到底有没有全表搜索了么
    luoyou1014
        8
    luoyou1014  
       2016-07-09 17:48:55 +08:00
    看有没有 order by ,有 order by 会扫出所有数据然后拿排序结果的第一条数据,没有的话只要找到数据就会直接返回结果。
    kamikat
        9
    kamikat  
       2016-07-09 18:06:07 +08:00 via Android
    COUNT 在 InnoDB 似乎是要扫表的
    SoloCompany
        10
    SoloCompany  
       2016-07-09 18:44:50 +08:00
    select count() limit 这明显是姿势不对啊, aggressive function 没有 group by 的情况下记录数永远是 1 ,你先搞清楚 limit 作用的是什么
    wodesuck
        11
    wodesuck  
       2016-07-10 14:41:00 +08:00
    explain select * from table where conditions limit 1; 看看
    讲道理 mysql 应该还是很聪明的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5765 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 06:24 · PVG 14:24 · LAX 23:24 · JFK 02:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.