mysql优化利器之explain使用介绍 |
一、语法 {EXPLAIN | DESCRIBE | DESC} tbl_name [col_name | wild] {EXPLAIN | DESCRIBE | DESC} [explain_type] SELECT select_options explain_type: {EXTENDED | PARTITIONS} 二、数据库准备 表一: DROP TABLE IF EXISTS `products`; SET @saved_cs_client = @@character_set_client; SET character_set_client = utf8; CREATE TABLE `products` ( `products_id` int(11) unsigned NOT NULL auto_increment, `products_type` int(11) unsigned NOT NULL default '1', `products_quantity` float NOT NULL default '0', `products_model` varchar(32) default NULL, `products_upc` varchar(32) default NULL, `products_isbn` varchar(32) default NULL, `products_image` varchar(128) default NULL, `products_image_thumbnail` varchar(200) NOT NULL, `products_price` decimal(15,4) NOT NULL default '0.0000', `products_virtual` tinyint(1) NOT NULL default '0', `products_date_added` datetime NOT NULL default '0001-01-01 00:00:00', `products_last_modified` datetime default NULL, `products_date_available` datetime default NULL, `products_weight` float NOT NULL default '0', `products_status` tinyint(1) NOT NULL default '0', `products_tax_class_id` int(11) NOT NULL default '0', `manufacturers_id` int(11) default NULL, `products_web_id` int(11) default NULL, `products_ordered` float NOT NULL default '0', `products_quantity_order_min` float NOT NULL default '1', `products_quantity_order_units` float NOT NULL default '1', `products_priced_by_attribute` tinyint(1) NOT NULL default '0', `product_is_free` tinyint(1) NOT NULL default '0', `product_is_call` tinyint(1) NOT NULL default '0', `products_quantity_mixed` tinyint(1) NOT NULL default '0', `product_is_always_free_shipping` tinyint(1) NOT NULL default '0', `products_qty_box_status` tinyint(1) NOT NULL default '1', `products_quantity_order_max` float NOT NULL default '0', `products_sort_order` int(11) NOT NULL default '0', `products_discount_type` tinyint(1) NOT NULL default '0', `products_discount_type_from` tinyint(1) NOT NULL default '0', `products_price_sorter` decimal(15,4) NOT NULL default '0.0000', `master_categories_id` int(11) NOT NULL default '0', `products_mixed_discount_quantity` tinyint(1) NOT NULL default '1', `metatags_title_status` tinyint(1) NOT NULL default '0', `metatags_products_name_status` tinyint(1) NOT NULL default '0', `metatags_model_status` tinyint(1) NOT NULL default '0', `metatags_price_status` tinyint(1) NOT NULL default '0', `metatags_title_tagline_status` tinyint(1) NOT NULL default '0', `itemno` varchar(32) default NULL, `products_images_no` varchar(10) default '0', `products_url` varchar(512) default NULL, PRIMARY KEY (`products_id`), UNIQUE KEY `itemno` (`itemno`) ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; SET character_set_client = @saved_cs_client; 表二: DROP TABLE IF EXISTS `products_image`; SET @saved_cs_client = @@character_set_client; SET character_set_client = utf8; CREATE TABLE `products_image` ( `id` int(10) unsigned NOT NULL auto_increment, `products_id` int(10) unsigned NOT NULL, `products_images_no` varchar(10) default '0', `image_dir` varchar(200) default NULL, `products_image_thumbnail` varchar(200) default NULL, `flag` int(2) default NULL, `up_time` datetime default NULL, `web_from` varchar(20) default NULL, PRIMARY KEY (`id`), KEY `idx_porducts_id` (`products_id`) ) ENGINE=MyISAM AUTO_INCREMENT=1DEFAULT CHARSET=utf8; SET character_set_client = @saved_cs_client; 三、关于explain选项 下面是一个实例: mysql> explain select products_id from products limit 1; +----+-------------+----------+-------+---------------+---------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+----------+-------+---------------+---------+---------+------+------+-------------+ | 1 | SIMPLE | products | index | NULL | PRIMARY | 4 | NULL | 3113 | Using index | +----+-------------+----------+-------+---------------+---------+---------+------+------+-------------+ id MySQL Query Optimizer选定的执行计划中查询的序列号 。 select_type 1、SIMPLE:简单的select查询,不使用union及子查询 table 1、system:表仅有一行(系统表) 。这是const连接类型的一个特例 。 others possible_keys:指出mysql能在该表中使用哪些索引有助于查询 。如果为空,说明没有可用的索引 extra 1、Distinct: 一旦mysql找到了与行相联合匹配的行,就不再搜索了 。 四、具体的实例 1、mysql版本 mysql> select version(); +------------+ | version() | +------------+ | 5.1.73-log | +------------+ 1 row in set (0.00 sec) 2、sql语句分析1 mysql> explain select products_id from products; +----+-------------+----------+-------+---------------+---------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+----------+-------+---------------+---------+---------+------+------+-------------+ | 1 | SIMPLE | products | index | NULL | PRIMARY | 4 | NULL | 3113 | Using index | +----+-------------+----------+-------+---------------+---------+---------+------+------+------------- 3、sql语句分析2 mysql> explain select products_id from (select * from products limit 10) b ; +----+-------------+------------+------+---------------+------+---------+------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+------+---------------+------+---------+------+------+-------+ | 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 10 | | | 2 | DERIVED | products | ALL | NULL | NULL | NULL | NULL | 3113 | | +----+-------------+------------+------+---------------+------+---------+------+------+-------+ 4、sql语句分析3 mysql> explain select products_id from products where products_id=10 union select products_id from products where products_id=20 ; +----+--------------+------------+-------+---------------+---------+---------+-------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------+------------+-------+---------------+---------+---------+-------+------+-------------+ | 1 | PRIMARY | products | const | PRIMARY | PRIMARY | 4 | const | 1 | Using index | | 2 | UNION | products | const | PRIMARY | PRIMARY | 4 | const | 1 | Using index | | NULL | UNION RESULT | <union1,2> | ALL | NULL | NULL | NULL | NULL | NULL | | +----+--------------+------------+-------+---------------+---------+---------+-------+------+-------------+ 5、sql语句分析4 mysql> explain select * from products where products_id in ( select products_id from products where products_id=10 union select products_id from products where products_id=20 ); +----+--------------------+------------+-------+---------------+---------+---------+-------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------------+------------+-------+---------------+---------+---------+-------+------+-------------+ | 1 | PRIMARY | products | ALL | NULL | NULL | NULL | NULL | 3113 | Using where | | 2 | DEPENDENT SUBQUERY | products | const | PRIMARY | PRIMARY | 4 | const | 1 | Using index | | 3 | DEPENDENT UNION | products | const | PRIMARY | PRIMARY | 4 | const | 1 | Using index | | NULL | UNION RESULT | <union2,3> | ALL | NULL | NULL | NULL | NULL | NULL | | +----+--------------------+------------+-------+---------------+---------+---------+-------+------+-------------+ 完成 |