Mysql – Database Offset & Limit without order by

MySQLpostgresqlquery

To my understanding, in MySQL/Postgres database there is no guarantee for the order of values with a select statement without an order-by clause, thus one should be getting rows arbitrarily. If so, then why are there usually no duplicates when I perform such query?

SELECT * FROM `table` LIMIT 0,5
SELECT * FROM `table` LIMIT 5,5
SELECT * FROM `table` LIMIT 10,5

Aren't these queries safe to use, considering that one only cares about not fetching duplicates and does not mind about the ordering? If so, how then?

Best Answer

Your syntax won't work with PostgreSQL, but I can answer your question for PostgreSQL anyway:

Your query will often work as you intend because the three statements will often use the same execution plan. There is no guarantee for that though.

One possibility where even a sequential scan in PostgreSQL will return a different order is if there is already a sequential scan of the table in progress: Then PostgreSQL will start scanning where the other statement is currently scanning the table, i.e. somewhere in the middle, so that both scan can execute somewhat synchronized to reduce the I/O operations necessary.