download:JavaEE在线就业班2.0【最新升级版】
函數操作
對條件字段做函數操作走不了索引。
select * from t1 where date© =‘2019-05-21’;
優化:改成範圍查询
select * from t1 where c>=‘2019-05-21 00:00:00’ and c<=‘2019-05-21 23:59:59’;
隱式轉換
操作符與不同類型的操作對象一同運用時,就會發作類型轉換以使操作兼容。
select user_name,tele_phone from user_info where tele_phone =11111111111; / tele_phone varchar /
實践會做函數操作:
select user_name,tele_phone from user_info where cast(tele_phone as singed int) =11111111111;
優化:類型統一
select user_name,tele_phone from user_info where tele_phone =‘11111111111’;
含糊查询
通配符在前面
select * from t1 where a like ‘%1111%’;
優化:含糊查询必需包含條件字段前面的值
select * from t1 where a like ‘1111%’;
範圍查询
範圍查询數據量太多,需求回表,因而不走索引。
select * from t1 where b>=1 and b <=2000;
優化:降低單次查询範圍,分屢次查询。(實践可能速度沒得快太多,倡議走索引)
select from t1 where b>=1 and b <=1000;
show profiles;
±---------±-----------±-----------------------------------------+
| Query_ID | Duration | Query |
±---------±-----------±-----------------------------------------+
| 1 | 0.00534775 | select from t1 where b>=1 and b <=1000 |
| 2 | 0.00605625 | select * from t1 where b>=1 and b <=2000 |
±---------±-----------±-----------------------------------------+
2 rows in set, 1 warning (0.00 sec)
計算操作
即便是简單的計算
explain select * from t1 where b-1 =1000;
優化:將計算操作放在等號後面
explain select * from t1 where b =1000 + 1;