SQL優化以及各類SQL執行計畫情況(持續更新)

2021-10-21 23:50:09 字數 3189 閱讀 1841

create table `t_user` (

`id` int(10) unsigned not null auto_increment,

`username` varchar(255) collate utf8mb4_bin not null,

`email` varchar(255) collate utf8mb4_bin not null,

`phone` varchar(255) collate utf8mb4_bin not null,

`***` varchar(255) collate utf8mb4_bin not null,

`grade_id` int(11) not null,

`clazz_id` int(11) not null,

primary key (`id`),

key `username` (`username`,`email`,`phone`)

) engine=innodb auto_increment= default charset=utf8mb4 collate=utf8mb4_bin;

create table `t_grade` (

`id` int(10) unsigned not null auto_increment,

`grade_name` varchar(255) collate utf8mb4_bin not null,

primary key (`id`)

) engine=innodb auto_increment=1 default charset=utf8mb4 collate=utf8mb4_bin;

create table `t_clazz` (

`id` int(10) unsigned not null auto_increment,

`clazz_name` varchar(255) collate utf8mb4_bin not null,

`grade_id` int(11) not null,

primary key (`id`)

) engine=innodb auto_increment=1 default charset=utf8mb4 collate=utf8mb4_bin;

進行sql語句優化的時候,我們的目標往往是優化到走索引而不掃瞄整顆索引樹

-- 當資料量很少的時候,很可能明明可以走索引,執行計畫給出的結果卻是全表掃瞄。以下的情況均是資料量達到一定量的情況下的執行計畫

explain select * from t_user; -- 不走索引,全表掃瞄,type = all

explain select id from t_user; -- 走索引,掃瞄整顆索引樹,type = index

explain select username from t_user; -- 走索引,掃瞄整顆索引樹,type = index

explain select phone from t_user; -- 走索引,掃瞄整顆索引樹,type = index

explain select * from t_user where username = '李四'; -- 走索引,type = ref

explain select * from t_user where username != '李四'; -- 不走索引,全表掃瞄,type = all

explain select * from t_user where username like '李四'; -- 走索引,type = range

explain select * from t_user where username like '李四%'; -- 走索引,type = range

explain select * from t_user where username like '%李四'; -- 不走索引,全表掃瞄,type = all

explain select * from t_user where username in ('李四'); -- 走索引,type = ref,當 in 條件只有乙個時,優化器會優化為 =

explain select * from t_user where username in ('李四', '張三');-- 走索引,type = range

explain select * from t_user where username not in ('李四'); -- 不走索引,全表掃瞄,type = all

explain select * from t_user where username = '李四' and email = '[email protected]'; -- 走索引,type = ref

explain select * from t_user where email = '[email protected]' and username = '李四'; -- 走索引,type = ref

explain select * from t_user where email = '[email protected]' or username = '李四'; -- 不走索引,全表掃瞄,type = all

explain select * from t_user where email = '[email protected]'; -- 不走索引,全表掃瞄,type = all

explain select username, email, phone from t_user where username = '李四'; -- 走索引,type = ref

explain select email from t_user where username != '李四'; -- 會走索引,掃瞄整顆索引樹,type = index

explain select username from t_user where username not in ('李四'); -- 走索引,掃瞄整顆索引樹,type = index

explain select * from t_user a left join t_grade b on a.grade_id = b.id where username = '李四'; -- 走索引,t_user 表的 type = ref, t_grade 表的 type = eq_ref

這裡可以大致總結為:

當查詢欄位有非索引欄位時,!=、 like 『%***』、or、not in 往往不會走索引,會進行全表掃瞄,日常開發中常注意這些情況就好。

Sql優化 執行計畫

一段sql 寫好以後,可以通過檢視sql的執行計畫,初步 該sql在執行時的效能好壞,尤其是在發現某個sql語句的效率較差時,我們可以通過檢視執行計畫,分析出該sql 的問題所在。1 開啟熟悉的檢視工具 pl sql developer。在pl sql developer中寫好一段sql 後,按f5...

mysql 以及hive 執行sql

mysql 有兩種方式 針對sql指令碼即多條sql放在一起 mysql u使用者名稱 p密碼 d資料庫 sql指令碼檔案路徑全名 如果在 sql 指令碼檔案中使用了 use 資料庫,則 d資料庫 選項可以忽略 或者進入命令列mysql 中 直接source path sql 如果是直接執行 sql...

sql優化過程 執行計畫

1 什麼是sql執行計畫 sql執行計畫,就是一條sql語句,在資料庫中實際執行的時候,一步步的分別都做了什麼。就是我們用explain分析一條sql語句時展示出來的那些資訊 2 sql執行計畫的作用 sql執行計畫的意義就在於我們可以通過執行計畫更加清晰的認識到這一條語句,分為了哪幾步,有沒有用到...