A
A
Alexander2014-07-21 17:18:21
MySQL
Alexander, 2014-07-21 17:18:21

Why does MariaDB return NULL in the keys graph when viewed through EXPLAIN?

Good day!
I decided to look at the request through EXPLAIN and was surprised when I saw NULL in the keys column. Currently using MariaDB (5.5.5-10.0.12-MariaDB-1~wheezy-log). If you run a query on MySQL, then there are indexes in the keys column.
Even if you specify USE INDEX (index) in the query, MariaDB still has NULL in the keys column.
Is MariaDB not using these indexes, or is it just returning incorrect information?

EXPLAIN SELECT SQL_NO_CACHE *
FROM fd_awards_ceremonies cer
LEFT JOIN fd_awards_nominees n ON(cer.ceremony_id = n.ceremony_id)
LEFT JOIN fd_awards_nominations nom ON(nom.nomination_id = n.nomination_id AND nom.nomination_general = 1)
LEFT JOIN fd_content c ON(n.content_id = c.content_id)
WHERE cer.award_id = 1

Result:
+------+-------------+-------+--------+---------------+---------+---------+---------------------+------+-------------------------------------------------+
| id   | select_type | table | type   | possible_keys | key     | key_len | ref                 | rows | Extra                                           |
+------+-------------+-------+--------+---------------+---------+---------+---------------------+------+-------------------------------------------------+
|    1 | SIMPLE      | cer   | ALL    | award_id      | NULL    | NULL    | NULL                |    1 | Using where                                     |
|    1 | SIMPLE      | n     | ALL    | ceremony_id   | NULL    | NULL    | NULL                |    1 | Using where; Using join buffer (flat, BNL join) |
|    1 | SIMPLE      | nom   | eq_ref | PRIMARY       | PRIMARY | 4       | db.n.nominee_id     |    1 | Using where                                     |
|    1 | SIMPLE      | c     | eq_ref | PRIMARY       | PRIMARY | 3       | db.n.content_id     |    1 | Using where                                     |
+------+-------------+-------+--------+---------------+---------+---------+---------------------+------+-------------------------------------------------+
4 rows in set (0.00 sec)

Here is the structure of the fd_awards_ceremonies and fd_awards_nominees tables I have a question about.
CREATE TABLE `fd_awards_ceremonies` (
  `ceremony_id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `award_id` mediumint(8) unsigned NOT NULL,
  `ceremony_year` year(4) DEFAULT NULL,
  `ceremony_country` smallint(5) unsigned NOT NULL,
  `ceremony_location` varchar(255) NOT NULL,
  `ceremony_hosts` varchar(255) DEFAULT NULL,
  `ceremony_hosts_text` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`ceremony_id`),
  UNIQUE KEY `award_id` (`award_id`,`ceremony_year`),
  KEY `award_country` (`ceremony_country`),
  KEY `ceremony_id` (`ceremony_id`),
  CONSTRAINT `fd_awards_ceremonies_ibfk_2` FOREIGN KEY (`ceremony_country`) REFERENCES `fd_countries` (`country_id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `fd_awards_ceremonies_ibfk_1` FOREIGN KEY (`award_id`) REFERENCES `fd_awards` (`award_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

CREATE TABLE `fd_awards_nominees` (
  `nominee_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `nomination_id` int(10) unsigned NOT NULL,
  `content_id` mediumint(8) unsigned NOT NULL,
  `person_id` mediumint(8) unsigned NOT NULL,
  `award_id` mediumint(8) unsigned NOT NULL,
  `ceremony_id` mediumint(8) unsigned NOT NULL,
  `award_status` tinyint(3) unsigned NOT NULL,
  `person_text` varchar(255) DEFAULT NULL,
  `content_text` varchar(255) DEFAULT NULL,
  `song_text` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`nominee_id`),
  KEY `award_id` (`award_id`),
  KEY `ceremony_id` (`ceremony_id`) USING BTREE,
  KEY `nomination_id` (`nomination_id`),
  CONSTRAINT `fd_awards_nominees_ibfk_3` FOREIGN KEY (`nomination_id`) REFERENCES `fd_awards_nominations` (`nomination_id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `fd_awards_nominees_ibfk_1` FOREIGN KEY (`award_id`) REFERENCES `fd_awards` (`award_id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `fd_awards_nominees_ibfk_2` FOREIGN KEY (`ceremony_id`) REFERENCES `fd_awards_ceremonies` (`ceremony_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

Thanks
UPD: missed linking column, updated query and table structure. Unfortunately, the issue is still relevant.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nikoinlove, 2014-07-23
@Gemini_13

Apparently, Maria thinks that a table scan will work faster here than an index search.
If you don't agree with her, then FORCE INDEX will make her not think so.
You can read more here dev.mysql.com/doc/refman/5.6/en/index-hints.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question