mysql优化查询
使用索引查詢
MariaDB [test]> explain select * from te where id=22; #在沒有增加索引情況下,rows為7,即查詢行數(shù) +------+-------------+-------+------+---------------+------+---------+------+------+-------------+ | 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: 0MariaDB [test]> explain select * from te where id=22; #rows變?yōu)? +------+-------------+-------+------+---------------+--------+---------+-------+------+-------+ | 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關(guān)鍵字查詢時,%號位于首位會導(dǎo)致無法使用索引查詢,否則可以正常使用索引查詢,如下
多列索引問題
轉(zhuǎn)載于:https://www.cnblogs.com/biaopei/p/7730497.html
總結(jié)
- 上一篇: Python可视化:Seaborn(二)
- 下一篇: ***远程连接MYSQL提示1130 -