Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql order by -id vs order by id desc

I wish to fetch the last 10 rows from the table of 1 M rows.

CREATE TABLE `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `updated_date` datetime NOT NULL,
  PRIMARY KEY (`id`)
)

One way of doing this is -

select * from test order by -id limit 10;

**10 rows in set (0.14 sec)**

Another way of doing this is -

select * from test order by id desc limit 10;

**10 rows in set (0.00 sec)**

So I did an 'EXPLAIN' on these queries -

Here is the result for the query where I use 'order by desc'

EXPLAIN select * from test order by id desc limit 10;

enter image description here

And here is the result for the query where I use 'order by -id'

EXPLAIN select * from test order by -id limit 10;

enter image description here

I thought this would be same but is seems there are differences in the execution plan.

like image 444
PiyusG Avatar asked Dec 30 '14 11:12

PiyusG


People also ask

Is ORDER BY DESC or ASC?

The ORDER BY command sorts the result set in ascending order by default. To sort the records in descending order, use the DESC keyword.

What does the statement ORDER BY Column_name Desc do?

ORDER BY DESC statement is used to sort data in result-set in descending order.

Is SQL ORDER BY default ASC or DESC?

The ORDER BY statement in SQL is used to sort the fetched data in either ascending or descending according to one or more columns. By default ORDER BY sorts the data in ascending order. We can use the keyword DESC to sort the data in descending order and the keyword ASC to sort in ascending order.

What does ORDER BY 1 desc mean?

it means order by the very first column from the select list.


2 Answers

RDBMS use heuristics to calculate the execution plan, they cannot always determine the semantic equivalence of two statements as it is a too difficult problem (in terms of theoretical and practical complexity).

So MySQL is not able to use the index, as you do not have an index on "-id", that is a custom function applied to the field "id". Seems trivial, but the RDBMSs must minimize the amount of time needed to compute the plans, so they get stuck with simple problems.

When an optimization cannot be found for a query (i.e. using an index) the system fall back to the implementation that works in any case: a scan of the full table.

like image 69
Nicola Ferraro Avatar answered Sep 27 '22 22:09

Nicola Ferraro


As you can see in Explain results,

1 : order by id
MySQL is using indexing on id. So it need to iterate only 10 rows as it is already indexed. And also in this case MySQL don't need to use filesort algorithm as it is already indexed.

2 : order by -id
MySQL is not using indexing on id. So it needs to iterate all the rows.( e.g. 455952) to get your expected results. In this case MySQL needs to use filesort algorithm as id is not indexed. So it will obviously take more time :)

like image 37
Vishal Zanzrukia Avatar answered Sep 27 '22 20:09

Vishal Zanzrukia