Search code examples
mysqlinnodb

Is it safe to use LIMIT without ORDER BY


I'm using InnoDB. I need to query to get 10 records from a table is any order.

Is it safe to use LIMIT without ORDER BY? Would it be faster?


Solution

  • If you are not using the ORDER BY then you are not sorting your records, so it will definitely make your data retrieval faster. So if you simply use LIMIT then it would be faster as compared to the data retrieved through ORDER BY. But do note that, in that case the data will not be in any order.

    As far as the safety is concerned, I am not sure about which safety you are thinking of, as there is no potential harm in a query which uses only LIMIT and does not uses an ORDER BY clause.

    You can also look at the article: ORDER BY … LIMIT Performance Optimization