简体   繁体   中英

Concurrent query execution is slow in MySQL 5.7

Concurrent query execution is slow in MySQL 5.7.

When I run only this below query it takes - 5.28sec

select pkid,lastname
    from Table1
    where pkid in (select fkid from Table2)
    order by 2 desc limit 10; 

But if I fire the same query 10times concurrently, each query takes around 11secs. Im not sure why its happening, even though my innodb_thread_concurrency is 10.

Concurrent execution stats - no_of_queries vs each_query_time:

1 time - 5.3sec
5 time - 7.8sec
10 times - 11sec

Variables:

max_connections - 1500
innodb_thread_concurrency - 10

Cpu - 16core

Can someone, guide me what I'm missing here.

Note: This is not query optimisation. My issue here is concurrent query execution is slow. Just to show the difference between executing a single query separately vs executing same query 10/5 items concurrently, I used this query.

Explain

{
  "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"
            ]
          }
        }
      ]
    }
  }
}

The queries are competing for resources to do the same thing. They interfere with each other.

Perhaps if you wrote the query using exists and have the right indexes the whole thing would be faster:

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; 

You definitely want an index on table2(fkid) (although you get this for free with the foreign key declaration in MySQL). It is possible than an index on table1(lastname desc, pkid) would also help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM