Select * and query optimizer

I have a very simple MySQL table with very strange behavior. By the way, strange behavior is exactly what I want him to do, but I don’t want to bring it into production, I don’t know why he does what he does.

In any case, I have a table created like this:

Create table `raceTimes` (
    `userID` mediumint(8) unsigned,
    `time` time,
    primary key (`userID`),
    key `idx_time` (`time`)
) engine=InnoDB default charset=utf8;

Now, when I make a Select * request from raceTimes, I get a result set as follows:

mysql> Select * from raceTimes;
+--------+----------+
| userID | time     |
+--------+----------+
|     14 | 12:37:46 |
|      6 | 12:41:11 |
|      5 | 12:48:45 |
|     13 | 12:55:46 |
|     10 | 13:13:37 |
|      9 | 13:40:37 |
|     17 | 15:30:44 |
|     18 | 15:46:58 |
|      3 | 16:16:45 |
|      8 | 16:40:11 |
|      7 | 16:41:11 |
|      4 | 16:48:45 |
|     16 | 20:30:44 |
|     15 | 20:37:44 |
|      1 | 21:00:00 |
|      2 | 21:16:00 |
|     11 | 23:13:37 |
|     20 | 23:14:58 |
|     19 | 23:46:58 |
|     12 | 23:55:46 |
+--------+----------+

Note that the result set is a time-based order, from lowest to highest. So, this is exactly what I want the table to do, since I'm trying to use this for the leaderboard in the game. When I run an explanation on my request, I get the following:

mysql> explain select * from raceTimes;
+----+-------------+------------+-------+---------------+----------+---------+------+------+-------------+
| id | select_type | table      | type  | possible_keys | key      | key_len | ref  | rows | Extra       |
+----+-------------+------------+-------+---------------+----------+---------+------+------+-------------+
|  1 | SIMPLE      | raceTimes  | index | NULL          | idx_time | 4       | NULL |   20 | Using index |
+----+-------------+------------+-------+---------------+----------+---------+------+------+-------------+

. - idx_time ( ), . .

, , . . , idx_time , , (8). .

, , , , :

Create table `raceTimes2` (
    `userID` mediumint(8) unsigned,
    `time` time,
    key `idx_time` (`time`)
) engine=InnoDB default charset=utf8;

. , , idx_time. , , :

mysql> explain select * from testTable6 use index(`idx_time`);
+----+-------------+------------+------+---------------+------+---------+------+------+-------+
| id | select_type | table      | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+------------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | raceTimes2 | ALL  | NULL          | NULL | NULL    | NULL |   20 |       |
+----+-------------+------------+------+---------------+------+---------+------+------+-------+

, . , , , , , ?

+5
2

, . :

MySQL idx_time, . , , , ( InnoDB , (time, userID) ), , time.

userID - , MySQL . "use index (idx_time)" , WHERE time.

Edit:
, , , USE INDEX MySQL , - ( WHERE/ON) . . explain = 'index' , , type = 'ALL'.

MySQL .

+2

, "order by"! SQL . , .

, , :

order by time desc

, .

, . - - . "" ( SQL Server), , .

+2

All Articles