Waarom is query 1 consequent zo traag, terwijl ie eigenlijk het snelst zou moeten zijn, er zijn namelijk geen topics met forumid 1.
Bug in de optimizer of zie ik iets simpels over het hoofd?
Bug in de optimizer of zie ik iets simpels over het hoofd?
SQL:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
| mysql> select id, subject from topics where forumid = 1 order by lastpost desc limit 1; Empty set (0.58 sec) mysql> select id, subject from topics where forumid = 118 order by lastpost desc limit 1; +---------+---------+ | id | subject | +---------+---------+ | 1542189 | test | +---------+---------+ 1 row in set (0.00 sec) mysql> explain select id, subject from topics where forumid = 1 order by lastpost desc limit 1; +----+-------------+--------+-------+---------------+----------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------+-------+---------------+----------+---------+------+------+-------------+ | 1 | SIMPLE | topics | index | NULL | lastpost | 4 | NULL | 1 | Using where | +----+-------------+--------+-------+---------------+----------+---------+------+------+-------------+ 1 row in set (0.00 sec) mysql> explain select id, subject from topics where forumid = 118 order by lastpost desc limit 1; +----+-------------+--------+-------+---------------+----------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------+-------+---------------+----------+---------+------+------+-------------+ | 1 | SIMPLE | topics | index | NULL | lastpost | 4 | NULL | 1 | Using where | +----+-------------+--------+-------+---------------+----------+---------+------+------+-------------+ 1 row in set (0.00 sec) CREATE TABLE `topics` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `forumid` int(10) unsigned NOT NULL DEFAULT '0', `userid` int(10) unsigned NOT NULL DEFAULT '0', `locked` enum('yes','no') NOT NULL DEFAULT 'no', `sticky` enum('yes','no') NOT NULL DEFAULT 'no', `subject` varchar(255) NOT NULL, `posts` int(11) NOT NULL, `lastpost` int(10) unsigned NOT NULL DEFAULT '0', PRIMARY KEY (`id`), KEY `userid` (`userid`), KEY `subject` (`subject`), KEY `lastpost` (`lastpost`) ) MySQL 5.5.47-0+deb8u1 |