MYSQL数据库mysql优化利器之explain使用介绍
《MYSQL数据库mysql优化利器之explain使用介绍》要点: MYSQL数据库一、语法 MYSQL数据库
{EXPLAIN | DESCRIBE | DESC}
tbl_name [col_name | wild]
{EXPLAIN | DESCRIBE | DESC}
[explain_type] SELECT select_options
explain_type: {EXTENDED | PARTITIONS}
MYSQL数据库二、数据库准备 MYSQL数据库表一: MYSQL数据库
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,`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;
MYSQL数据库表二: MYSQL数据库
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,`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;
MYSQL数据库三、关于explain选项 MYSQL数据库下面是一个实例: MYSQL数据库
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 |
+----+-------------+----------+-------+---------------+---------+---------+------+------+-------------+
MYSQL数据库id MYSQL数据库MySQL? Query? Optimizer选定的执行计划中查询的序列号. MYSQL数据库select_type MYSQL数据库1、SIMPLE:简单的select查询,不使用union及子查询 MYSQL数据库table MYSQL数据库1、system:表仅有一行(系统表).这是const连接类型的一个特例. MYSQL数据库?others MYSQL数据库possible_keys:指出mysql能在该表中使用哪些索引有助于查询.如果为空,说明没有可用的索引 MYSQL数据库extra MYSQL数据库1、Distinct: 一旦mysql找到了与行相联合匹配的行,就不再搜索了. MYSQL数据库四、具体的实例 MYSQL数据库1、mysql版本 MYSQL数据库
mysql> select version();
+------------+
| version() |
+------------+
| 5.1.73-log |
+------------+
1 row in set (0.00 sec)
MYSQL数据库2、sql语句分析1 MYSQL数据库
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 |
+----+-------------+----------+-------+---------------+---------+---------+------+------+-------------
MYSQL数据库3、sql语句分析2 MYSQL数据库
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 | |
+----+-------------+------------+------+---------------+------+---------+------+------+-------+
MYSQL数据库4、sql语句分析3 MYSQL数据库
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 | |
+----+--------------+------------+-------+---------------+---------+---------+-------+------+-------------+
MYSQL数据库5、sql语句分析4 MYSQL数据库
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 | |
+----+--------------------+------------+-------+---------------+---------+---------+-------+------+-------------+
MYSQL数据库完成 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |