Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is the index on s not used for sort here?
    primarykey
    data
    text
    <p>Setup:</p> <pre><code>mysql&gt; create table test(id integer unsigned,s varchar(30)); Query OK, 0 rows affected (0.05 sec) mysql&gt; insert into test(id,s) value(1,'s'); Query OK, 1 row affected (0.00 sec) mysql&gt; insert into test(id,s) value(1,'tsr'); Query OK, 1 row affected (0.00 sec) mysql&gt; insert into test(id,s) value(1,'ts3r'); Query OK, 1 row affected (0.00 sec) mysql&gt; create index i_test_id on test(id); Query OK, 3 rows affected (0.08 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql&gt; create index i_test_s on test(s); Query OK, 3 rows affected (0.05 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql&gt; insert into test(id,s) value(21,'ts3r'); Query OK, 1 row affected (0.00 sec) </code></pre> <p>And then run this:</p> <pre><code>mysql&gt; explain select * from test where id in (1) order by s desc; +----+-------------+-------+------+---------------+-----------+---------+-------+------+-----------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+-----------+---------+-------+------+-----------------------------+ | 1 | SIMPLE | test | ref | i_test_id | i_test_id | 5 | const | 2 | Using where; Using filesort | +----+-------------+-------+------+---------------+-----------+---------+-------+------+-----------------------------+ 1 row in set (0.02 sec) </code></pre> <p>We can see it uses filesort instead of using the index on <code>s</code>,which will be slow when the selected result set is big.How to optimize it?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload