标签:myisam innodb mysql 存储引擎与索引 sql优化
两个小型表 item、category:CREATE TABLE `item` ( `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(150) NOT NULL, `category_id` mediumint(8) unsigned NOT NULL, PRIMARY KEY (`id`), KEY `name` (`name`), KEY `category_id` (`category_id`) ) CHARSET=utf8 CREATE TABLE `category` ( `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(150) NOT NULL, PRIMARY KEY (`id`), KEY `name` (`name`) ) CHARSET=utf8
EXPLAIN SELECT item.id,category.name AS category_name FROM item JOIN category ON item.category_id=category.id;
+----+-------------+----------+-------+---------------+-------------+---------+--------------------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+----------+-------+---------------+-------------+---------+--------------------+------+-------------+ | 1 | SIMPLE | category | index | PRIMARY | name | 452 | NULL | 103 | Using index | | 1 | SIMPLE | item | ref | category_id | category_id | 3 | dbname.category.id | 5 | Using index | +----+-------------+----------+-------+---------------+-------------+---------+--------------------+------+-------------+
EXPLAIN SELECT item.id,category.name AS category_name FROM item JOIN category ON item.category_id=category.id;
+----+-------------+----------+--------+---------------+---------+---------+-------------------------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+----------+--------+---------------+---------+---------+-------------------------+------+-------+ | 1 | SIMPLE | item | ALL | category_id | NULL | NULL | NULL | 1003 | | | 1 | SIMPLE | category | eq_ref | PRIMARY | PRIMARY | 3 | dbname.item.category_id | 1 | | +----+-------------+----------+--------+---------------+---------+---------+-------------------------+------+-------+
标签:myisam innodb mysql 存储引擎与索引 sql优化
原文地址:http://blog.csdn.net/defonds/article/details/46731005