mysql常用sql語句執行時間記錄 sql優化

2021-10-09 15:54:25 字數 4095 閱讀 3429

資料量:3200000條

1.1、數量查詢
select count(1)

from users

--耗時:12.997s

select count(*)

from users

--耗時:12.449s

select count(id)

from users

--id是主鍵

--耗時:12.414s

select count(`password`)

from users

--password,字元型,普通欄位無索引

--耗時:4.869s

select count(`status`)

from users

--status,整形,普通欄位無索引

--耗時:5.042s

select count(`status`)

from users

--status,整形,有索引

--耗時:1.604s

1.2、分頁查詢
select *

from users

limit 0,10

--耗時:0.148s

select *

from users

limit 320000,10

--資料量的10%

--耗時:0.648s

select *

from users

limit 640000,10

--資料量的20%

--耗時:1.205s

--大約是10%的1.8倍左右

select *

from users

limit 1600000,10

--資料量的50%

--耗時:2.711s

--大約是10%的18.3倍左右

select *

from users

limit 3199990,10

--資料量的100%

--耗時:5.006s

--大約是10%的33.8倍左右

select *

from users

limit 499999,10

--耗時:0.923s

--50萬資料出現1s以上的耗時

select id,user_name,`password`,`status`,create_time

from users

limit 3199990,10

--耗時:5.410s

--查詢具體的字段,'*'由具體欄位名替換

select *

from users

where id > 500000

limit 10

--耗時:0.142s

select *

from users

where id > 320000

limit 10

--耗時:0.035s

select *

from users

where id > 640000

limit 10

--耗時:0.044s

select *

from users

where id > 3199990

limit 10

--耗時:0.030s

1.3、模糊查詢
select *

from users

where user_name like '%螢脂%'

--耗時7.157s

--實際資料2條

select *

from users

where match(user_name) against('螢脂')

--耗時:0.193s

(建立全文索引,設定ngram全文解析器,支援版本:>=5.6)

(alter table users add fulltext index name_fulltext(user_name) with parser ngram;)

users:3200000資料

user_info:3200000資料

mall_order:1600000資料

** mall_order的user_id未建索引前:**

select m.id,n.age,n.name,count(1) ordernum

from users m

left join user_info n

on m.id = n.id

left join mall_order mo

on mo.user_id = n.id

where m.id > 3000000

group by n.id

limit 500

--耗時:2.878s

select m.id,n.age,n.name,count(1) ordernum

from users m

left join user_info n

on m.id = n.id

left join mall_order mo

on mo.user_id = n.id

where m.id > 3000000 and mo.user_id > 3000000

group by n.id

limit 500

--耗時:9.048s

mall_order的user_id建索引後:

select m.id,n.age,n.name,count(1) ordernum

from users m

left join user_info n

on m.id = n.id

left join mall_order mo

on mo.user_id = n.id

where m.id > 3000000

group by n.id

limit 500

--耗時:1.492s

select m.id,n.age,n.name,count(1) ordernum

from users m

left join user_info n

on m.id = n.id

left join mall_order mo

on mo.user_id = n.id

where m.id > 3000000 and mo.user_id > 3000000

group by n.id

limit 500

--耗時:0.765s

select m.id,ui.age,ui.name,ui.ordernum

from users m,

(select id,age,name,ordernum

from user_info n,(select user_id,count(1) ordernum from mall_order where user_id > 3000000 group by user_id) mo

where n.id > 3000000 and mo.user_id = n.id

limit 500

) ui

where m.id = ui.id and m.id > 3000000

limit 500

--耗時:0.099s

mysql顯示SQL語句執行時間

檢視 mysql 語法 詳細執行時間 與 cpu 記憶體使用量 mysql query profiler mysql 的 sql 語法調整主要都是使用 explain 但是這個並沒辦法知道詳細的 ram memory cpu 等使用量.於 mysql 5.0.37 以上開始支援 mysql quer...

mysql顯示SQL語句執行時間

檢視 mysql 語法 詳細執行時間 與 cpu 記憶體使用量 mysql query profiler mysql 的 sql 語法調整主要都是使用 explain 但是這個並沒辦法知道詳細的 ram memory cpu 等使用量.於 mysql 5.0.37 以上開始支援 mysql quer...

Mysql的SQL語句執行時間

mysql 的 sql 語法調整主要都是使用 explain 但是這個並沒辦法知道詳細的 ram memory cpu 等使用量.於 mysql 5.0.37 以上開始支援 mysql query profiler,可以查詢到此 sql 會執行多少時間,並看出 cpu memory 使用量,執行過程...