Get first id of the latest 25000 entries?

All we need is an easy explanation of the problem, so here it is.

I have a database with 200000 rows. Of those 200000 rows I want the latest 25000 and from those I need the oldest id (column entity_id).

This is my query so far: select * from sales_flat_order order by entity_id desc limit 25000;

This is aleady running forever. Is there a better way? Maybe its easier to get the entity_id on position 25000, but it has to be ordered, I need to have id on position 25000 of the latest entries.

This is the create statement of the table:


How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

You can use LIMIT offset

select * 
from sales_flat_order 
order by entity_id desc limit 24999,1;

The OFFSET value allows us to specify which row to start from retrieving data.

offset — > 24999 hence the starting position is 25000.

row_count — > 1 only one value returned

Learn more on MySQL LIMIT clause

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply