where and limit both have the function of avoiding full table scan (mysql). The difference is: where can make full use of 索引, while limit can limit 查询行数
The existence of
limit is mainly to prevent 全表扫描. If a statement itself can be concluded without a full table scan, there will be little difference in performance with or without limit, such as 唯一索引, 主键 [Not tested, NND]
For 偏移量offset larger 查询, it is recommended to use the where statement to avoid 全表扫描; because limit itself does not use the narrowing ability of 索引
For any query, the first thing you should think of is how to use the where statement to narrow the range , and then use limit to limit the number of returned rows
If a unique index is established for the username field, adding limit is meaningless. Because the implementation of the unique index database is B-Tree search, the only result can be accurately queried
The existence ofwhere
andlimit
both have the function of avoiding full table scan (mysql
). The difference is:where
can make full use of索引
, whilelimit
can limit查询行数
limit
is mainly to prevent全表扫描
. If a statement itself can be concluded without a full table scan, there will be little difference in performance with or withoutlimit
, such as唯一索引
,主键
[Not tested, NND]For
偏移量offset
larger查询
, it is recommended to use thewhere
statement to avoid全表扫描
; becauselimit
itself does not use the narrowing ability of索引
For any query, the first thing you should think of is how to use the where statement to narrow the range , and then use
limit
to limit the number of returned rowsIf no index is added, adding limit will make it slower. Just tried it
If a unique index is established for the username field, adding limit is meaningless. Because the implementation of the unique index database is B-Tree search, the only result can be accurately queried
Limit only controls the number of rows returned, not the actual query. You can use explain to check
If you encounter a table with a relatively large amount of data, using limit will kill the database
I have always said that adding LIMIT 1 will speed up the speed
Why not use the unique index of where (such as username). limit just limits the displayed items