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 · Jul 9, 2016 · 3330 views
    This topic created in 3622 days ago, the information mentioned may be changed or developed.

    最近写 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 replies    2016-07-10 14:41:00 +08:00
    F281M6Dh8DXpD1g2
        1
    F281M6Dh8DXpD1g2  
       Jul 9, 2016   ❤️ 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  
       Jul 9, 2016 via Android
    不会的。

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

    有官方文档的话,那应该是我的测试方法有𢋷误了
    ``` SQL
    select count(*) from table where conditions limit 10;
    ```
    里面的 count(*)误导了我对 limit 的判断
    bdbai
        5
    bdbai  
       Jul 9, 2016 via Android
    试试 explain
    9hills
        6
    9hills  
       Jul 9, 2016
    MySQL 没有那么傻。。
    Mac
        7
    Mac  
       Jul 9, 2016
    你自己用 heidisql 看返回的查询时间不就知道到底有没有全表搜索了么
    luoyou1014
        8
    luoyou1014  
       Jul 9, 2016
    看有没有 order by ,有 order by 会扫出所有数据然后拿排序结果的第一条数据,没有的话只要找到数据就会直接返回结果。
    kamikat
        9
    kamikat  
       Jul 9, 2016 via Android
    COUNT 在 InnoDB 似乎是要扫表的
    SoloCompany
        10
    SoloCompany  
       Jul 9, 2016
    select count() limit 这明显是姿势不对啊, aggressive function 没有 group by 的情况下记录数永远是 1 ,你先搞清楚 limit 作用的是什么
    wodesuck
        11
    wodesuck  
       Jul 10, 2016
    explain select * from table where conditions limit 1; 看看
    讲道理 mysql 应该还是很聪明的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5644 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 158ms · UTC 03:35 · PVG 11:35 · LAX 20:35 · JFK 23:35
    ♥ Do have faith in what you're doing.