简体   繁体   中英

Bad sql performance with a simple where condition on an indexed column in mysql

I have a very simple sql where statement

  SELECT    `genre_application`.`genre_id`
      FROM  `genre_application`
      WHERE `genre_application`.`application_id` = 310633997

which takes around 2 seconds even with indexes on the column.

{
"query_block": {
"select_id": 1,
"cost_info": {
  "query_cost": "2098202.80"
},
"table": {
  "table_name": "genre_application",
  "access_type": "index",
  "key": "PRIMARY",
  "used_key_parts": [
    "genre_id",
    "application_id"
  ],
  "key_length": "8",
  "rows_examined_per_scan": 10363019,
  "rows_produced_per_join": 1036301,
  "filtered": "10.00",
  "using_index": true,
  "cost_info": {
    "read_cost": "1890942.42",
    "eval_cost": "207260.38",
    "prefix_cost": "2098202.80",
    "data_read_per_join": "23M"
  },
  "used_columns": [
    "genre_id",
    "application_id"
  ],
  "attached_condition": "(`genre_application`.`application_id` = 310633997)"
}
}

How could I improve the execution time?

This is your query:

SELECT ga.`genre_id`
FROM `genre_application` ga
WHERE ga.`application_id` = 310633997

You need an index where application_id is the first key in the index. The best index is a composite index on genre_application(application_id, genre_id) . Note that the order of the keys in the index matters.

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