mysql数据库优化课程---17、mysql索引优化

2023-06-26

mysql数据库优化课程---17、mysql索引优化

一、总结

一句话总结:一些字段可能会使索引失效,比如like,or等

1、check表监测的使用场景是什么?

视图

视图建立在两个表上, 删除了其中的一个表,check 视图的时候会有错误提示信息

mysql> check table v_user;
---------------------------------------------------------------------------+
| yzmedu.v_user | check | Error    | Table 'yzmedu.class' doesn't exist
                                                                           |
| yzmedu.v_user | check | Error    | View 'yzmedu.v_user' references invalid table(s) or c
olumn(s) or function(s) or definer/invoker of view lack rights to use them |
| yzmedu.v_user | check | error    | Corrupt
                                                                           |
---------------------------------------------------------------------------+

2、复合索引的使用情况怎样(用的少)?

左边

使用左边的那个才能用到复合索引,单独使用右边的不会用到复合索引(其实是用上了,但是无效)

所以复合索引用的极少

对于创建的多列索引,只要查询的条件中用到最左边的列,索引一般就会被使用.

对于创建的多列索引,只要查询的条件中用到最左边的列
,索引一般就会被使用。如下创建一个复合索引。
然后按company_id进行查询,发现使用到了复合索引
使用下面的查询就没有使用到复合索引。
mysql>create index ind_sales2_com_m on onsales2(company_id,moneys);
mysql>explain select * from sales2 where company_id=2006\G
mysql>explain select * from sales2 where moneys=1\G

3、like关键字有索引却用不上的情况?

百分号 左边

百分号在左边,索引用不上,百分号在右边,索引可能用上

#desc select * from user where username like 'linux%'\G
当使用like进行搜索时,%在前索引可能会失效.

如下这个使用到了索引,而下面例子能够使用索引,区别
就在于“%”的位置不同,上面的例子是吧“%”放在
了第一位,而下面的例子则没有
mysql> explain select * from company2 where name like
"3%"\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: company2
type: range
possible_keys: ind_company2_name
key: ind_company2_name
key_len: 11
ref: NULL
rows: 103
Extra: Using where
1 row in set (0.00 sec)

4、字段的null判断的时候会用到索引么?

#desc select * from user where username is null;
当判断null值时会使用username这一列的索引.

5、使用or关键词的时候索引的使用情况怎样?

失效

or关键字:
#desc select * from user where username='user7' or age=15\G
在使用or的情况下两边的索引都有可能失效.

6、全文索引有研究价值么?

没有

用到全文索引的时候,还不如去用sphix,速度快很多

二、内容在总结中

 

mysql数据库优化课程---17、mysql索引优化的相关教程结束。