Search code examples
sqlselectlimit

Is it faster to use limit statement with known max count?


a query from a large table like:

select something from sometable limit somecount;

I know the limit statement is usefull to avoid get too much rows from a query. But how about using it when not much rows got but in a large table? for example, there is a table create like this

CREATE TABLE if not exists users (
    id integer primary key autoincrement,
    name varchar(80) unique not null,
    password varchar(20) not null,
    role integer default 1,  -- 0 -> supper admin; 1 -> user
    banned integer default 0 
);

case 1: i want to get users where id=100. Here id is primary key, surely it can get 1 row at most.which is faster between 2 statements below?

select * from users where id=100;
select * from users where id=100 limit 1;

case 2: i want to get users where name='jhon'. Here name is unique, also it can get 1 row at most.which is faster between 2 statements below?

select * from users where name='jhon';
select * from users where name='jhon' limit 1;

case 3: i want to get users where role=0. Here role is neither primary key nor unique, but i know there are only 10 rows at most. which is faster between 2 statements below?

select * from users where role=0;
select * from users where role=0 limit 10;

Solution

  • If you care about performance, then add indexes to handle all three queries. This requires an additional index on: users(role). The id column already has an index as the primary key; name has an index because it is declared unique.

    For the first two cases, the limit shouldn't make a difference. Without limit, the engine finds the matching row in the index and returns it. If the engine doesn't use the "unique" information, then it might need to peek at the next value in the index, just to see if it is the same.

    The third case, with no index, is a bit different. Without an index, the engine will want to scan all the rows to find all matches. With an index, it can find all the matching rows there. Add a limit to that, and it will just stop at the first one.

    The appropriate indexes will be a bigger boost to performance than using limit, on average.