本文介紹了MySQL 5.7中的并發查詢執行速度較慢的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
MySQL 5.7中并發查詢執行速度較慢。
當我只運行This Below查詢時,它需要-5.28秒
select pkid,lastname
from Table1
where pkid in (select fkid from Table2)
order by 2 desc limit 10;
但是,如果我同時執行相同的查詢10次,則每個查詢大約需要11秒。我不確定為什么會發生這種情況,即使我的Innodb_線程_并發性為10。
并發執行統計信息-no_of_queries vs each_Query_time:
1 time - 5.3sec
5 time - 7.8sec
10 times - 11sec
變量:
max_connections - 1500
innodb_thread_concurrency - 10
CPU-16core
誰能告訴我這里我錯過了什么?
注意:這不是查詢優化。我這里的問題是并發查詢執行速度很慢。為了顯示單獨執行單個查詢與同時執行相同查詢10/5項之間的區別,我使用了這個查詢。
解釋
{
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "1541542.63"
},
"ordering_operation": {
"using_temporary_table": true,
"using_filesort": true,
"cost_info": {
"sort_cost": "1.00"
},
"nested_loop": [
{
"table": {
"table_name": "Table2",
"access_type": "index",
"possible_keys": [
"Table2_FK4_IDX"
],
"key": "Table2_FK4_IDX",
"used_key_parts": [
"FKID"
],
"key_length": "9",
"rows_examined_per_scan": 1246072,
"rows_produced_per_join": 732208,
"filtered": "58.76",
"using_index": true,
"loosescan": true,
"cost_info": {
"read_cost": "2586.21",
"eval_cost": "146441.71",
"prefix_cost": "149027.92",
"data_read_per_join": "2G"
},
"used_columns": [
"TABLE2ID",
"FKID"
],
"attached_condition": "(`db1234`.`Table2`.`FKID` is not null)"
}
},
{
"table": {
"table_name": "Table1",
"access_type": "eq_ref",
"possible_keys": [
"PRIMARY"
],
"key": "PRIMARY",
"used_key_parts": [
"PKID"
],
"key_length": "8",
"ref": [
"db1234.Table2.FKID"
],
"rows_examined_per_scan": 1,
"rows_produced_per_join": 1,
"filtered": "100.00",
"cost_info": {
"read_cost": "1246072.00",
"eval_cost": "0.20",
"prefix_cost": "1541541.63",
"data_read_per_join": "19K"
},
"used_columns": [
"PKID",
"LASTNAME"
]
}
}
]
}
}
}
推薦答案
查詢爭用資源來做同樣的事情。它們相互干擾。
如果您使用exists
編寫查詢并擁有正確的索引,整個過程可能會更快:
select t1.pkid, t1.lastname
from Table1 t1
where exists (select 1 from table2 t2 where t2.fkid = t1.pkid)
order by 2 desc
limit 10;
您肯定需要table2(fkid)
上的索引(盡管您可以通過MySQL中的foreign key
聲明免費獲得)。table1(lastname desc, pkid)
上的索引也有可能有所幫助。
這篇關于MySQL 5.7中的并發查詢執行速度較慢的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,