ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# mysql优化过程 # 了解desc 可用于分析表结构和sql语句, 用于分析sql语句最常见 ## 分析表结构 ```sql mysql> explain hd_parter; +------------+-----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+-----------------------+------+-----+---------+----------------+ | id | mediumint(8) unsigned | NO | PRI | NULL | auto_increment | | parter | varchar(16) | NO | UNI | | | | key | varchar(32) | NO | | | | | name | varchar(100) | NO | | NULL | | | siteurl | varchar(200) | NO | | NULL | | | status | tinyint(1) | NO | | 0 | | | start_time | int(10) unsigned | NO | | 0 | | | end_time | int(10) unsigned | NO | | 0 | | +------------+-----------------------+------+-----+---------+----------------+ 8 rows in set (0.02 sec) ``` ## 分析语句 ```sql mysql> desc select * from hd_order order by id desc limit 10; +----+-------------+----------+-------+---------------+---------+---------+------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+----------+-------+---------------+---------+---------+------+------+-------+ | 1 | SIMPLE | hd_order | index | NULL | PRIMARY | 4 | NULL | 10 | | +----+-------------+----------+-------+---------------+---------+---------+------+------+-------+ 1 row in set (0.00 sec) ``` ### select_type - SIMPLE 简单表, 不实用子查询或者连接 - PRIMARY 主查询 - SUBQUERY 子查询中的第一个SELECT - UNION ### table 对应的表名, 如果取了别名则显示别名 ### type 性能是越往下越差 - system 表中只有一行,即常量表 - const 单表中最多有一个匹配行,如primary key或unique index - eq_ref 对于前面的每一行,在此表中只查询一条记录,也就是多表连接中使用primary key或unique index - ref 与eq_ref类似,区别在于不是使用primary key或unique index,而是使用普通索引 - ref_or_null 与ref类型,区别在于条件中包含对null的查询 - index_merge 索引合并优化 - unique_subquery in的后面是一个查询主键字段的子查询 - index_subquery 与unique_subquery类似,区别在于in的后面是查询非唯一索引字段的子查询 - range 单表中的范围查询 - index 对于前面的每一行,都通过查询索引来得到数据 - all 对于前面的每一行,都通过扫描全表来得到数据 ### possible_keys 查询时可能用到的索引 ### key 查询时实际使用到的索引 ### key_len 索引字段的长度 ### ref key 列所选择的索引的查找方式 ### rows  扫描行的数量, 越少越好 ### Extra 执行情况的说明和描述 **Using index** 通过索引就能返回结果 **Using where** **Using temporary** 使用了临时表 **Using filesort **查询所需的排序与使用的索引的排序不一致, 重新排序 ### # 前置理论知识 ## sql语句执行过程 http://www.2cto.com/database/201512/453280.html 这是从网上找来的例子 ```sql SELECT DISTINCT < select_list > FROM < left_table > < join_type > JOIN < right_table > ON < join_condition > WHERE < where_condition > GROUP BY < group_by_list > HAVING < having_condition > ORDER BY < order_by_condition > LIMIT < limit_number > ``` 执行顺序是 ```sql FROM <left_table> ON <join_condition> <join_type> JOIN <right_table> WHERE <where_condition> GROUP BY <group_by_list> HAVING <having_condition> SELECT DISTINCT <select_list> ORDER BY <order_by_condition> LIMIT <limit_number> ``` 大概就是 从张表获取数据->根据条件过滤->选择需要的字段->排序->从什么位置取多少条数据 ## 连表join http://coolshell.cn/articles/3463.html ## desc分析的参数 # 分析sql语句 desc ```sql ```