16

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?

Code
  • 6,041
  • 4
  • 35
  • 75

5 Answers5

7

It depends what you consider as safe- if you want consistent result (meanning, getting the same result everytime as long as the table's content will not change) or if you want specific result (biggests, newest, oldest, whatever)- than this requires order. If by safety you meam that the query wont crush, and you dont care which X results you get- than yes, using limit is fine (this is actually done automatically by many sql tools, like mysql workbench, to speed things up).

In terms of speed- will make it faster without order for two reasons:

  1. ordering takes time.
  2. Using limit allow the server to stop as it finds the first X results. You can see that queries with limit 10 will run faster than limit 100000 on large tables. When you use order, the server must go through all result, so cant stop in the middle.

So yes, using limit without order will make it faster

moffeltje
  • 4,521
  • 4
  • 33
  • 57
Nir Levy
  • 12,750
  • 3
  • 21
  • 38
6

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

Dexter
  • 4,036
  • 3
  • 47
  • 55
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • 1
    I've ran into this issue just now. Without `order by` the order of the data is not predictable. – UnixAgain Feb 21 '19 at 03:31
  • Sure it is, nothing happens truly random by executing code. There are rules and procedures which always give you the data in a certain order and thus it is predictable – floGalen Sep 23 '20 at 09:38
  • 1
    The same data with the same query run in the same version of database engine will produce the same results. But if any of those changes, the resultset _may_ change. – Rick James Dec 02 '22 at 23:38
4

It's not safe.

Without a order by, the results may not be consistent over consecutive excutions of the same query.

Refer to mysql limit collapse, which result in data interaction

For example:(course_id is the primay key).

Get the first page 10 rows;

select course_id,grade_id from sc_base_course where  agency_id = 10000  limit 0,10;
+-----------+----------+
| course_id | grade_id |
+-----------+----------+
|        13 |        1 |
|         6 |        3 |
|        12 |        4 |
|         8 |        2 |
|         7 |        2 |
|         9 |        4 |
|        16 |        1 |
|         1 |        2 |
|        17 |        1 |
|        14 |        5 |
+-----------+----------+

Get the second page 7 rows

select course_id,grade_id from sc_base_course where  agency_id = 10000  limit 10,10;
+-----------+----------+
| course_id | grade_id |
+-----------+----------+
|        11 |        4 |
|        12 |        4 |
|        13 |        1 |
|        14 |        5 |
|        15 |        1 |
|        16 |        1 |
|        17 |        1 |
+-----------+----------+
LF00
  • 27,015
  • 29
  • 156
  • 295
2

Yes, you can and yes, it would be faster (assuming the order does not matter to you). order by requires sorting. This means the database has to do more work to get you the result. Most commonly limit is used with order by since want to put some ordering constraints on which 10 records you get (say most recent, highest rank of some sort, etc.)

leeor
  • 17,041
  • 6
  • 34
  • 60
1

The 3 answers so far are good. But they are not totally correct.

Contrary to the other answers, sometimes leaving out ORDER BY will not make it faster. The optimizer might happen to generate the rows in that order anyway. Examples:

  • GROUP BY usually orders the results. So, if the ORDER BY would match the GROUP BY, there is no extra effort.
  • ORDER BY the_primary_key might match the order of fetching. This is almost guaranteed to be the case for InnoDB without a WHERE clause.

"Safe" does not compute.

Rick James
  • 135,179
  • 13
  • 127
  • 222