mysql优化查询

使用索引查询

MariaDB [test]> explain select * from te where id=22;                   #在没有增加索引情况下,rows为7,即查询行数
+------+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id   | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+------+-------------+-------+------+---------------+------+---------+------+------+-------------+
|    1 | SIMPLE      | te    | ALL  | NULL          | NULL | NULL    | NULL |    7 | Using where |
+------+-------------+-------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.01 sec)

MariaDB [test]> alter table te add index tindex(id);                    #增加索引
Query OK, 0 rows affected (0.09 sec)
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [test]> explain select * from te where id=22;                   #rows变为1
+------+-------------+-------+------+---------------+--------+---------+-------+------+-------+
| id   | select_type | table | type | possible_keys | key    | key_len | ref   | rows | Extra |
+------+-------------+-------+------+---------------+--------+---------+-------+------+-------+
|    1 | SIMPLE      | te    | ref  | tindex        | tindex | 5       | const |    1 |       |
+------+-------------+-------+------+---------------+--------+---------+-------+------+-------+
1 row in set (0.00 sec)

MariaDB [test]> 

索引使用注意事项
使用like关键字查询时,%号位于首位会导致无法使用索引查询,否则可以正常使用索引查询,如下
这里写图片描述
多列索引问题

MariaDB [test]> alter table te add index indextwo(id,name);         #创建索引
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [test]> show create table te\G;
*************************** 1. row ***************************
       Table: te
Create Table: CREATE TABLE `te` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL,
  `se` varchar(3) DEFAULT 'man',
  `city` varchar(3) DEFAULT 'gx',
  KEY `indextwo` (`id`,`name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

ERROR: No query specified

MariaDB [test]> explain select * from te where name='ds';                       #rows为7
+------+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id   | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+------+-------------+-------+------+---------------+------+---------+------+------+-------------+
|    1 | SIMPLE      | te    | ALL  | NULL          | NULL | NULL    | NULL |    7 | Using where |
+------+-------------+-------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

MariaDB [test]> explain select * from te where id=3 and name='ds';              #增加多列索引的首列后,可以正常使用索引
+------+-------------+-------+------+---------------+----------+---------+-------------+------+-----------------------+
| id   | select_type | table | type | possible_keys | key      | key_len | ref         | rows | Extra                 |
+------+-------------+-------+------+---------------+----------+---------+-------------+------+-----------------------+
|    1 | SIMPLE      | te    | ref  | indextwo      | indextwo | 28      | const,const |    1 | Using index condition |
+------+-------------+-------+------+---------------+----------+---------+-------------+------+-----------------------+
1 row in set (0.00 sec)

MariaDB [test]> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值