您的位置:首页 > 数据库 > MySQL

mysql索引优化续

2016-07-30 15:06 471 查看
(1)索引类型:

Btree索引:抽象的可以理解为“排好序的”快速查找结构
myisam,innodb中默认使用Btree索引

hash索引:hash索引计算速度非常的快,但数据是随机放置的,无法对范围进行优化,无法利用前缀索引,排序也无法优化
memory表里默认使用hash索引

(二)单列索引与多列索引
一、单列索引
二、多列索引
怎么才能够让多列索引充分发挥作用?
1.在多列索引上,索引使用匹配需要符合左前缀规则

有如下联合索引:index(a,b,c)

select * from test_table where a=1 and b=2 and c=3; 从左到右都使用到了索引,虽然有大于号,但是也会使用半块索引

select * from test_table where a=1 and c=3 and b=3; mysql很聪明的,虽然查询的条件列不是按照顺序来的,但是mysql会自动识别的所以仍然都可以使用到索引

select * from test_table where a=1 and b=2 and c>3; 从左到右都使用到了索引,虽然有大于号,但是也会使用半块索引

select * from test_table where a=1 and b>2 and c=3; 从左到右都使用到了索引,虽然有大于号,但是也会使用半块索引,依然c列索引也会使用到,所以都使用到了索引

select * from test_table b=2 and c=3; 索引生效是从左到右的如果断掉了后面的索引列就不会生效了,这个没有用到a索引,所以 一个索引都没有使用到

select * from test_table b=2; 同上

select * from test_table c=3; 同上

分组统计建议要先按照分组的字段进行排序然后再统计

我们想知道某条sql语句是不是按照我们希望的方式使用了索引我们可以使用explain来查看sql语句的执行计划:
explain用法说明:

只需要在sql语句前面加上explain即可

mysql> explain select * from t where c1='a' and c5='e' order by c2,c3;

+----+-------------+-------+------+---------------+------+---------+-------+------+--------------------------+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

+----+-------------+-------+------+---------------+------+---------+-------+------+--------------------------+

| 1 | SIMPLE | t | ref | c1 | c1 | 3 | const | 6 | Using where; Using index |

+----+-------------+-------+------+---------------+------+---------+-------+------+--------------------------+

table:表示使用的数据表
type:连接类型有多个值,不同的值体现了sql语句不同的优化级别

keys:mysql使用的索引

key_len:mysql使用的索引长度

rows:mysql执行查询的行数,数值越大越不好说明没有用好索引

extra:mysql解决查询的详细详细有几个参数需要解释下
using filesort:说明使用执行该sql语句先使用了数据排序

using index:说明使用了索引

using temporary:说明mysql创建了临时表来容纳结果

(三)show profiles的使用

一般情况情况是这样的我们考虑需要建立给表建立索引是由于查询缓慢,这个时候我们可以通过开启show profiles来了解我们优化的进展,和方案的可行性。

show profiles:默认一般都是关闭的,但是会话级别可以开启这个功能,开启后可以让mysql收集在执行sql语句时的所有相关信息,通过这个可以比较明确的了解优化的成果。

1.查看show profile是否开启

mysql> show variables like "%pro%";

+---------------------------+-------+

| Variable_name | Value |

+---------------------------+-------+

| have_profiling | YES |

| profiling | OFF |

| profiling_history_size | 15 |

| protocol_version | 10 |

| proxy_user | |

| slave_compressed_protocol | OFF |

| stored_program_cache | 256 |

+---------------------------+-------+

2.如果profiling关闭了则开启选项
mysql> set profiling=1;

3.查看sql语句运行的信息

在开启profile后系统中执行的sql语句都将被记录

mysql> show profiles;

+----------+-------------+--------------------------------------------------------------------------------------------------------+

| Query_ID | Duration | Query |

+----------+-------------+--------------------------------------------------------------------------------------------------------+

| 1 | 0.00031950 | show variables like "%pro%" |

| 2 | 0.00020125 | show tables |

| 3 | 0.40749175 | select count(*) from b2b_my_test_value |

| 4 | 0.00008600 | show processlist |

| 5 | 0.40956300 | select count(*) from b2b_my_test_value |

| 6 | 0.00007225 | show profile for query |

| 7 | 0.00004150 | show profile for query |

| 8 | 23.30362350 | select avg(`my_age`),`my_aihao` from b2b_my_test_value group by `my_aihao` |

| 9 | 0.00018900 | show index from b2b_my_test_value |

| 10 | 0.00015050 | explain select avg(`my_age`),`my_aihao` from b2b_my_test_value group by `my_aihao` |

| 11 | 0.00014875 | explain select avg(`my_age`),`my_aihao` from b2b_my_test_value group by `my_aihao` |

| 12 | 0.00016725 | explain select avg(`my_age`),`my_aihao` from b2b_my_test_value group by `my_aihao` order by `my_aihao` |

| 13 | 0.00018725 | explain select avg(`my_age`),`my_aihao` from b2b_my_test_value group by `my_aihao` |

| 14 | 0.00020875 | show index from b2b_my_test_value |

| 15 | 0.00008125 | explain select avg(`my_age`),`my_aihao` from b2b_my_test_value where group by `my_aihao` |

+----------+-------------+--------------------------------------------------------------------------------------------------------+

15 rows in set, 1 warning (0.00 sec)

这样我们就可以很方便的了解sql执行的时间了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: