在數據庫查詢中不走索引的情況與壓力測試
阿新 • • 發佈:2019-01-16
判斷 not null ren order by 允許 查詢條件 nod tun _for
重點關註:
1) 沒有查詢條件,或者查詢條件沒有建立索引
例如:
select * from tab; 全表掃描。
select * from tab where 1=1;
在業務數據庫中,特別是數據量比較大的表。
是沒有全表掃描這種需求。
1、對用戶查看是非常痛苦的。
2、對服務器來講毀滅性的。
例如:
select * from tab;
SQL改寫成以下語句:
selec * from tab order by price limit 10 需要在price列上建立索引
select * from tab where name=‘zhangsan‘ name列沒有索引 改:1、換成有索引的列作為查詢條件 2、將name列建立索引
2) 查詢結果集是原表中的大部分數據,應該是25%以上。
查詢的結果集,超過了總數行數25%,優化器覺得就沒有必要走索引了。
假如:
tab表 id,name id:1-100w ,id列有索引
select * from tab where id>500000;
如果業務允許,可以使用limit控制。
怎麽改寫 ?
結合業務判斷,有沒有更好的方式。如果沒有更好的改寫方案
盡量不要在mysql存放這個數據了。放到redis裏面。
3) 索引本身失效,統計數據不真實
索引有自我維護的能力。
對於表內容變化比較頻繁的情況下,有可能會出現索引失效。
4) 查詢條件使用函數在索引列上,或者對索引列進行運算,運算包括(+,-,*,/,! 等)
例子:
錯誤的例子:select * from test where id-1=9;
正確的例子:select * from test where id=10;
算術運算
函數運算
desc select * from blog_userinfo where DATE_FORMAT(last_login,‘%Y-%m-%d‘) >= ‘2019-01-01‘;
子查詢
5)隱式轉換導致索引失效.這一點應當引起重視.也是開發中經常會犯的錯誤.
select * from t1 where telnum=110; 這樣會導致索引失效. 錯誤的例子:------------------------ mysql> alter table tab add index inx_tel(telnum); Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> mysql> desc tab; +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(20) | YES | | NULL | | | telnum | varchar(20) | YES | MUL | NULL | | +--------+-------------+------+-----+---------+-------+ 3 rows in set (0.01 sec) mysql> select * from tab where telnum=‘1333333‘; +------+------+---------+ | id | name | telnum | +------+------+---------+ | 1 | a | 1333333 | +------+------+---------+ 1 row in set (0.00 sec) mysql> select * from tab where telnum=1333333; +------+------+---------+ | id | name | telnum | +------+------+---------+ | 1 | a | 1333333 | +------+------+---------+ 1 row in set (0.00 sec) mysql> explain select * from tab where telnum=‘1333333‘; +----+-------------+-------+------+---------------+---------+---------+-------+------+-----------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+---------+---------+-------+------+-----------------------+ | 1 | SIMPLE | tab | ref | inx_tel | inx_tel | 63 | const | 1 | Using index condition | +----+-------------+-------+------+---------------+---------+---------+-------+------+-----------------------+ 1 row in set (0.00 sec) mysql> explain select * from tab where telnum=1333333; +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ | 1 | SIMPLE | tab | ALL | inx_tel | NULL | NULL | NULL | 2 | Using where | +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ 1 row in set (0.00 sec) mysql> explain select * from tab where telnum=1555555; +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ | 1 | SIMPLE | tab | ALL | inx_tel | NULL | NULL | NULL | 2 | Using where | +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ 1 row in set (0.00 sec) mysql> explain select * from tab where telnum=‘1555555‘; +----+-------------+-------+------+---------------+---------+---------+-------+------+-----------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+---------+---------+-------+------+-----------------------+ | 1 | SIMPLE | tab | ref | inx_tel | inx_tel | 63 | const | 1 | Using index condition | +----+-------------+-------+------+---------------+---------+---------+-------+------+-----------------------+ 1 row in set (0.00 sec) mysql> ---------------------------------------
6) <> ,not in 不走索引
EXPLAIN SELECT * FROM teltab WHERE telnum <> ‘110‘; EXPLAIN SELECT * FROM teltab WHERE telnum NOT IN (‘110‘,‘119‘); ------------ mysql> select * from tab where telnum <> ‘1555555‘; +------+------+---------+ | id | name | telnum | +------+------+---------+ | 1 | a | 1333333 | +------+------+---------+ 1 row in set (0.00 sec) mysql> explain select * from tab where telnum <> ‘1555555‘; ----- 單獨的>,<,in 有可能走,也有可能不走,和結果集有關,盡量結合業務添加limit or或in 盡量改成union EXPLAIN SELECT * FROM teltab WHERE telnum IN (‘110‘,‘119‘); 改寫成: EXPLAIN SELECT * FROM teltab WHERE telnum=‘110‘ UNION ALL SELECT * FROM teltab WHERE telnum=‘119‘ -----------------------------------
7) like "%_" 百分號在最前面不走
EXPLAIN SELECT * FROM teltab WHERE telnum LIKE ‘31%‘ 走range索引掃描 EXPLAIN SELECT * FROM teltab WHERE telnum LIKE ‘%110‘ 不走索引 %linux%類的搜索需求,可以使用elasticsearch 專門做搜索服務的數據庫產品
8) 單獨引用聯合索引裏非第一位置的索引列.作為條件查詢時不走索引.
列子: 復合索引: DROP TABLE t1 CREATE TABLE t1 (id INT,NAME VARCHAR(20),age INT ,sex ENUM(‘m‘,‘f‘),money INT); ALTER TABLE t1 ADD INDEX t1_idx(money,age,sex); DESC t1 SHOW INDEX FROM t1 走索引的情況測試: EXPLAIN SELECT NAME,age,sex,money FROM t1 WHERE money=30 AND age=30 AND sex=‘m‘; EXPLAIN SELECT NAME,age,sex,money FROM t1 WHERE money=30 AND age=30 ; EXPLAIN SELECT NAME,age,sex,money FROM t1 WHERE money=30 AND sex=‘m‘; ----->部分走索引 不走索引的: EXPLAIN SELECT NAME,age,sex,money FROM t1 WHERE age=20 EXPLAIN SELECT NAME,age,sex,money FROM t1 WHERE age=30 AND sex=‘m‘; EXPLAIN SELECT NAME,age,sex,money FROM t1 WHERE sex=‘m‘;
壓力測試
1、模擬數據庫數據
為了測試我們創建一個oldboy的庫創建一個t1的表,然後導入50萬行數據,腳本如下:
vim slap.sh #!/bin/bash HOSTNAME="localhost" PORT="3306" USERNAME="root" PASSWORD="123" DBNAME="oldboy" TABLENAME="t1" #create database mysql -h ${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} -e "drop database if exists ${DBNAME}" create_db_sql="create database if not exists ${DBNAME}" mysql -h ${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} -e "${create_db_sql}" #create table create_table_sql="create table if not exists ${TABLENAME}(stuid int not null primary key,stuname varchar(20) not null,stusex char(1) not null,cardid varchar(20) not null,birthday datetime,entertime datetime,address varchar(100)default null)" mysql -h ${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${create_table_sql}" #insert data to table i="1" while [ $i -le 500000 ] do insert_sql="insert into ${TABLENAME} values($i,‘alexsb_$i‘,‘1‘,‘110011198809163418‘,‘1990-05-16‘,‘2017-09-13‘,‘oldboyedu‘)" mysql -h ${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${insert_sql}" let i++ done #select data select_sql="select count(*) from ${TABLENAME}" mysql -h ${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${select_sql}"
執行腳本:
sh slap.sh
2、檢查數據可用性
mysql -uroot -p123
select count(*) from oldboy.t1;
3、在沒有優化之前我們使用mysqlslap來進行壓力測試
mysqlslap --defaults-file=/etc/my.cnf --concurrency=100 --iterations=1 --create-schema=‘oldboy‘ --query="select * from oldboy.t1 where stuname=‘alexsb_100‘" engine=innodb --number-of-queries=2000 -uroot -p123 -verbose
在數據庫查詢中不走索引的情況與壓力測試