Is the performance of mysql: limit much worse than that of between or in?

for example, select * from table limit 100
if there are 1 million data in the table,
, then this statement will scan the whole table, filter out all the 1 million data that meets the requirements
, and then fetch the first 1 million rows

.

and if you know that id is incremented from 1
, then select * from table where id between 1 and 100
performance will be much higher?

Nov.15,2021

sql like select * from table limit 100 does not cause a full table scan unless query or sort conditions are added.

select * from table where id between 1 and 100 will be faster if id is the primary key or unique key

there will not be much difference in speed between the two above


select * from table limit 100is indeed a full table scan, but in the process of full table scanning, the scan stops as soon as the 100th record is read, so the time is only the consumption of scanning 100th record, so it is very fast.
the following SQL:
select from (select from table) a limit 100;

should be performed in the following way.

if id increases progressively from 1, the execution efficiency of select * from table where id between 1 and 100 also depends on the situation:
1.id has an index, the primary key or unique key is better, that is, go to the index, scan 100 items and then stop;
there is no index on the 2.id, then the data of 1-100 is screened out after a full table scan.

Menu