› 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 · 3625 views
    This topic created in 3787 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 file 跟 grep re -r file | head -n 1 的区别

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

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

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