2017-06-09 62 views
1

直升機,我有一個疑問,UID列不同的積分不能使用索引,什麼事業?:MySQL的選擇SQL很慢,其中一個指數失去

請有人告訴我,謝謝!

表item_sort_20170525有222466057線, 節目表創建此:

CREATE TABLE `item_sort_20170525` (
    `id` int(10) NOT NULL AUTO_INCREMENT 
    `iid` bigint(20) NOT NULL DEFAULT '0' 
    `uid` bigint(20) NOT NULL DEFAULT '0' 
    `kw_id` int(10) NOT NULL DEFAULT '0' 
    `platform` tinyint(2) NOT NULL DEFAULT '0' 
    `is_p4p` tinyint(1) NOT NULL DEFAULT '0' 
    `page` tinyint(2) NOT NULL DEFAULT '1' 
    `pos` smallint(4) NOT NULL DEFAULT '0' 
    `real_pos` char(6) NOT NULL DEFAULT '' 
    `created` int(10) NOT NULL DEFAULT '0' 
    PRIMARY KEY (`id`), 
    KEY `idx_keyword` (`kw_id`) USING BTREE, 
    KEY `idx_iid` (`iid`,`platform`) USING BTREE, 
    KEY `idx_uid` (`uid`,`platform`) USING BTREE 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 

如果UID = 896588234然後

SELECT `kw_id`, COUNT(kw_id) AS `count` FROM `item_sort_20170525` 
WHERE `uid` = 896588234 AND `platform` IN (12, 11) GROUP BY `kw_id` ORDER BY `kw_id` DESC LIMIT 21; 

顯示解釋:

select_type : SIMPLE 
table   : item_sort_20170525 
type   : range 
possible_keys : idx_keyword,idx_uid 
key   : idx_uid 
key_len  : 9 
ref   : 
rows   : 585 
Extra   : Using index condition; Using temporary; Using filesort 

如果UID = 2259613579然後

SELECT `kw_id`, COUNT(kw_id) AS `count` FROM `item_sort_20170525` force index(`idx_uid`) 
WHERE `uid` = 2259613579 AND `platform` IN (12, 11) GROUP BY `kw_id` ORDER BY `kw_id` DESC LIMIT 21; 

顯示解釋:

select_type : SIMPLE 
table   : item_sort_20170525 
type   : ALL 
possible_keys : idx_keyword,idx_uid 
key   : 
key_len  : 
ref   : 
rows   : 225015710 
Extra   : Using where; Using temporary; Using filesort 

賠指數(idx_uid)其中uid EQ大INT像2259613579,然後使用力指數(idx_uid)剛剛同失敗! 這個MySQL optimer_trace:

{ 
    "steps": [ 
    { 
     "join_preparation": { 
     "select#": 1, 
     "steps": [ 
      { 
      "expanded_query": "/* select#1 */ select `tem_sort_20170525`.`kw_id` AS `kw_id`,count(`tem_sort_20170525`.`kw_id`) AS `count` from `tem_sort_20170525` where ((`tem_sort_20170525`.`uid` = 2259613579) and (`tem_sort_20170525`.`platform` in (12,11))) group by `tem_sort_20170525`.`kw_id` order by `tem_sort_20170525`.`kw_id` desc limit 21" 
      } 
     ] /* steps */ 
     } /* join_preparation */ 
    }, 
    { 
     "join_optimization": { 
     "select#": 1, 
     "steps": [ 
      { 
      "condition_processing": { 
       "condition": "WHERE", 
       "original_condition": "((`tem_sort_20170525`.`uid` = 2259613579) and (`tem_sort_20170525`.`platform` in (12,11)))", 
       "steps": [ 
       { 
        "transformation": "equality_propagation", 
        "resulting_condition": "((`tem_sort_20170525`.`platform` in (12,11)) and multiple equal(2259613579, `tem_sort_20170525`.`uid`))" 
       }, 
       { 
        "transformation": "constant_propagation", 
        "resulting_condition": "((`tem_sort_20170525`.`platform` in (12,11)) and multiple equal(2259613579, `tem_sort_20170525`.`uid`))" 
       }, 
       { 
        "transformation": "trivial_condition_removal", 
        "resulting_condition": "((`tem_sort_20170525`.`platform` in (12,11)) and multiple equal(2259613579, `tem_sort_20170525`.`uid`))" 
       } 
       ] /* steps */ 
      } /* condition_processing */ 
      }, 
      { 
      "table_dependencies": [ 
       { 
       "table": "`tem_sort_20170525`", 
       "row_may_be_null": false, 
       "map_bit": 0, 
       "depends_on_map_bits": [ 
       ] /* depends_on_map_bits */ 
       } 
      ] /* table_dependencies */ 
      }, 
      { 
      "ref_optimizer_key_uses": [ 
       { 
       "table": "`tem_sort_20170525`", 
       "field": "uid", 
       "equals": "2259613579", 
       "null_rejecting": false 
       } 
      ] /* ref_optimizer_key_uses */ 
      }, 
      { 
      "rows_estimation": [ 
       { 
       "table": "`tem_sort_20170525`", 
       "const_keys_added": { 
        "keys": [ 
        "idx_keyword" 
        ] /* keys */, 
        "cause": "group_by" 
       } /* const_keys_added */, 
       "range_analysis": { 
        "table_scan": { 
        "rows": 225015710, 
        "cost": 4.61e7 
        } /* table_scan */, 
        "potential_range_indices": [ 
        { 
         "index": "PRIMARY", 
         "usable": false, 
         "cause": "not_applicable" 
        }, 
        { 
         "index": "idx_keyword", 
         "usable": true, 
         "key_parts": [ 
         "kw_id", 
         "id" 
         ] /* key_parts */ 
        }, 
        { 
         "index": "idx_iid", 
         "usable": false, 
         "cause": "not_applicable" 
        }, 
        { 
         "index": "idx_uid", 
         "usable": true, 
         "key_parts": [ 
         "uid", 
         "platform", 
         "id" 
         ] /* key_parts */ 
        } 
        ] /* potential_range_indices */, 
        "setup_range_conditions": [ 
        ] /* setup_range_conditions */, 
        "group_index_range": { 
        "chosen": false, 
        "cause": "not_applicable_aggregate_function" 
        } /* group_index_range */, 
        "analyzing_range_alternatives": { 
        "range_scan_alternatives": [ 
         { 
         "index": "idx_uid", 
         "ranges": [ 
          "2259613579 <= uid <= 2259613579 AND 11 <= platform <= 11", 
          "2259613579 <= uid <= 2259613579 AND 12 <= platform <= 12" 
         ] /* ranges */, 
         "index_dives_for_eq_ranges": true, 
         "rowid_ordered": false, 
         "using_mrr": false, 
         "index_only": false, 
         "rows": 29, 
         "cost": 36.81, 
         "chosen": true 
         } 
        ] /* range_scan_alternatives */, 
        "analyzing_roworder_intersect": { 
         "usable": false, 
         "cause": "too_few_roworder_scans" 
        } /* analyzing_roworder_intersect */ 
        } /* analyzing_range_alternatives */, 
        "chosen_range_access_summary": { 
        "range_access_plan": { 
         "type": "range_scan", 
         "index": "idx_uid", 
         "rows": 29, 
         "ranges": [ 
         "2259613579 <= uid <= 2259613579 AND 11 <= platform <= 11", 
         "2259613579 <= uid <= 2259613579 AND 12 <= platform <= 12" 
         ] /* ranges */ 
        } /* range_access_plan */, 
        "rows_for_plan": 29, 
        "cost_for_plan": 36.81, 
        "chosen": true 
        } /* chosen_range_access_summary */ 
       } /* range_analysis */ 
       } 
      ] /* rows_estimation */ 
      }, 
      { 
      "considered_execution_plans": [ 
       { 
       "plan_prefix": [ 
       ] /* plan_prefix */, 
       "table": "`tem_sort_20170525`", 
       "best_access_path": { 
        "considered_access_paths": [ 
        { 
         "access_type": "ref", 
         "index": "idx_uid", 
         "rows": 36, 
         "cost": 43.2, 
         "chosen": true 
        }, 
        { 
         "access_type": "range", 
         "rows": 22, 
         "cost": 42.61, 
         "chosen": true 
        } 
        ] /* considered_access_paths */ 
       } /* best_access_path */, 
       "cost_for_plan": 42.61, 
       "rows_for_plan": 22, 
       "chosen": true 
       } 
      ] /* considered_execution_plans */ 
      }, 
      { 
      "attaching_conditions_to_tables": { 
       "original_condition": "((`tem_sort_20170525`.`uid` = 2259613579) and (`tem_sort_20170525`.`platform` in (12,11)))", 
       "attached_conditions_computation": [ 
       { 
        "table": "`tem_sort_20170525`", 
        "rechecking_index_usage": { 
        "recheck_reason": "low_limit", 
        "limit": 21, 
        "row_estimate": 22, 
        "range_analysis": { 
         "table_scan": { 
         "rows": 225015710, 
         "cost": 2.7e8 
         } /* table_scan */, 
         "potential_range_indices": [ 
         { 
          "index": "PRIMARY", 
          "usable": false, 
          "cause": "not_applicable" 
         }, 
         { 
          "index": "idx_keyword", 
          "usable": true, 
          "key_parts": [ 
          "kw_id", 
          "id" 
          ] /* key_parts */ 
         }, 
         { 
          "index": "idx_iid", 
          "usable": false, 
          "cause": "not_applicable" 
         }, 
         { 
          "index": "idx_uid", 
          "usable": false, 
          "cause": "not_applicable" 
         } 
         ] /* potential_range_indices */, 
         "setup_range_conditions": [ 
         ] /* setup_range_conditions */, 
         "group_index_range": { 
         "chosen": false, 
         "cause": "cannot_do_reverse_ordering" 
         } /* group_index_range */ 
        } /* range_analysis */ 
        } /* rechecking_index_usage */ 
       } 
       ] /* attached_conditions_computation */, 
       "attached_conditions_summary": [ 
       { 
        "table": "`tem_sort_20170525`", 
        "attached": "((`tem_sort_20170525`.`uid` = 2259613579) and (`tem_sort_20170525`.`platform` in (12,11)))" 
       } 
       ] /* attached_conditions_summary */ 
      } /* attaching_conditions_to_tables */ 
      }, 
      { 
      "clause_processing": { 
       "clause": "ORDER BY", 
       "original_clause": "`tem_sort_20170525`.`kw_id` desc", 
       "items": [ 
       { 
        "item": "`tem_sort_20170525`.`kw_id`" 
       } 
       ] /* items */, 
       "resulting_clause_is_simple": true, 
       "resulting_clause": "`tem_sort_20170525`.`kw_id` desc" 
      } /* clause_processing */ 
      }, 
      { 
      "clause_processing": { 
       "clause": "GROUP BY", 
       "original_clause": "`tem_sort_20170525`.`kw_id`", 
       "items": [ 
       { 
        "item": "`tem_sort_20170525`.`kw_id`" 
       } 
       ] /* items */, 
       "resulting_clause_is_simple": true, 
       "resulting_clause": "`tem_sort_20170525`.`kw_id`" 
      } /* clause_processing */ 
      }, 
      { 
      "refine_plan": [ 
       { 
       "table": "`tem_sort_20170525`", 
       "access_type": "table_scan" 
       } 
      ] /* refine_plan */ 
      }, 
      { 
      "reconsidering_access_paths_for_index_ordering": { 
       "clause": "GROUP BY", 
       "index_order_summary": { 
       "table": "`tem_sort_20170525`", 
       "index_provides_order": true, 
       "order_direction": "desc", 
       "index": "idx_keyword", 
       "plan_changed": true, 
       "access_type": "index_scan" 
       } /* index_order_summary */ 
      } /* reconsidering_access_paths_for_index_ordering */ 
      } 
     ] /* steps */ 
     } /* join_optimization */ 
    }, 
    { 
     "join_execution": { 
     "select#": 1, 
     "steps": [ 
     ] /* steps */ 
     } /* join_execution */ 
    } 
    ] /* steps */ 
} 

{ 
    "steps": [ 
    { 
     "join_preparation": { 
     "select#": 1, 
     "steps": [ 
      { 
      "expanded_query": "/* select#1 */ select `item_sort_20170525`.`kw_id` AS `kw_id`,count(`item_sort_20170525`.`kw_id`) AS `count` from `item_sort_20170525` FORCE INDEX (`idx_uid`) where ((`item_sort_20170525`.`uid` = 896588234) and (`item_sort_20170525`.`platform` in (12,11))) group by `item_sort_20170525`.`kw_id` order by `item_sort_20170525`.`kw_id` desc limit 21" 
      } 
     ] /* steps */ 
     } /* join_preparation */ 
    }, 
    { 
     "join_optimization": { 
     "select#": 1, 
     "steps": [ 
      { 
      "condition_processing": { 
       "condition": "WHERE", 
       "original_condition": "((`item_sort_20170525`.`uid` = 896588234) and (`item_sort_20170525`.`platform` in (12,11)))", 
       "steps": [ 
       { 
        "transformation": "equality_propagation", 
        "resulting_condition": "((`item_sort_20170525`.`platform` in (12,11)) and multiple equal(896588234, `item_sort_20170525`.`uid`))" 
       }, 
       { 
        "transformation": "constant_propagation", 
        "resulting_condition": "((`item_sort_20170525`.`platform` in (12,11)) and multiple equal(896588234, `item_sort_20170525`.`uid`))" 
       }, 
       { 
        "transformation": "trivial_condition_removal", 
        "resulting_condition": "((`item_sort_20170525`.`platform` in (12,11)) and multiple equal(896588234, `item_sort_20170525`.`uid`))" 
       } 
       ] /* steps */ 
      } /* condition_processing */ 
      }, 
      { 
      "table_dependencies": [ 
       { 
       "table": "`item_sort_20170525` FORCE INDEX (`idx_uid`)", 
       "row_may_be_null": false, 
       "map_bit": 0, 
       "depends_on_map_bits": [ 
       ] /* depends_on_map_bits */ 
       } 
      ] /* table_dependencies */ 
      }, 
      { 
      "ref_optimizer_key_uses": [ 
       { 
       "table": "`item_sort_20170525` FORCE INDEX (`idx_uid`)", 
       "field": "uid", 
       "equals": "896588234", 
       "null_rejecting": false 
       } 
      ] /* ref_optimizer_key_uses */ 
      }, 
      { 
      "rows_estimation": [ 
       { 
       "table": "`item_sort_20170525` FORCE INDEX (`idx_uid`)", 
       "const_keys_added": { 
        "keys": [ 
        "idx_keyword" 
        ] /* keys */, 
        "cause": "group_by" 
       } /* const_keys_added */, 
       "range_analysis": { 
        "table_scan": { 
        "rows": 225015710, 
        "cost": 2e308 
        } /* table_scan */, 
        "potential_range_indices": [ 
        { 
         "index": "PRIMARY", 
         "usable": false, 
         "cause": "not_applicable" 
        }, 
        { 
         "index": "idx_keyword", 
         "usable": false, 
         "cause": "not_applicable" 
        }, 
        { 
         "index": "idx_iid", 
         "usable": false, 
         "cause": "not_applicable" 
        }, 
        { 
         "index": "idx_uid", 
         "usable": true, 
         "key_parts": [ 
         "uid", 
         "platform", 
         "id" 
         ] /* key_parts */ 
        } 
        ] /* potential_range_indices */, 
        "setup_range_conditions": [ 
        ] /* setup_range_conditions */, 
        "group_index_range": { 
        "chosen": false, 
        "cause": "not_applicable_aggregate_function" 
        } /* group_index_range */, 
        "analyzing_range_alternatives": { 
        "range_scan_alternatives": [ 
         { 
         "index": "idx_uid", 
         "ranges": [ 
          "896588234 <= uid <= 896588234 AND 11 <= platform <= 11", 
          "896588234 <= uid <= 896588234 AND 12 <= platform <= 12" 
         ] /* ranges */, 
         "index_dives_for_eq_ranges": true, 
         "rowid_ordered": false, 
         "using_mrr": false, 
         "index_only": false, 
         "rows": 585, 
         "cost": 704.01, 
         "chosen": true 
         } 
        ] /* range_scan_alternatives */, 
        "analyzing_roworder_intersect": { 
         "usable": false, 
         "cause": "too_few_roworder_scans" 
        } /* analyzing_roworder_intersect */ 
        } /* analyzing_range_alternatives */, 
        "chosen_range_access_summary": { 
        "range_access_plan": { 
         "type": "range_scan", 
         "index": "idx_uid", 
         "rows": 585, 
         "ranges": [ 
         "896588234 <= uid <= 896588234 AND 11 <= platform <= 11", 
         "896588234 <= uid <= 896588234 AND 12 <= platform <= 12" 
         ] /* ranges */ 
        } /* range_access_plan */, 
        "rows_for_plan": 585, 
        "cost_for_plan": 704.01, 
        "chosen": true 
        } /* chosen_range_access_summary */ 
       } /* range_analysis */ 
       } 
      ] /* rows_estimation */ 
      }, 
      { 
      "considered_execution_plans": [ 
       { 
       "plan_prefix": [ 
       ] /* plan_prefix */, 
       "table": "`item_sort_20170525` FORCE INDEX (`idx_uid`)", 
       "best_access_path": { 
        "considered_access_paths": [ 
        { 
         "access_type": "ref", 
         "index": "idx_uid", 
         "rows": 585, 
         "cost": 702, 
         "chosen": true 
        }, 
        { 
         "access_type": "range", 
         "rows": 439, 
         "cost": 821.01, 
         "chosen": false 
        } 
        ] /* considered_access_paths */ 
       } /* best_access_path */, 
       "cost_for_plan": 702, 
       "rows_for_plan": 585, 
       "chosen": true 
       } 
      ] /* considered_execution_plans */ 
      }, 
      { 
      "attaching_conditions_to_tables": { 
       "original_condition": "((`item_sort_20170525`.`uid` = 896588234) and (`item_sort_20170525`.`platform` in (12,11)))", 
       "attached_conditions_computation": [ 
       { 
        "access_type_changed": { 
        "table": "`item_sort_20170525` FORCE INDEX (`idx_uid`)", 
        "index": "idx_uid", 
        "old_type": "ref", 
        "new_type": "range", 
        "cause": "uses_more_keyparts" 
        } /* access_type_changed */ 
       } 
       ] /* attached_conditions_computation */, 
       "attached_conditions_summary": [ 
       { 
        "table": "`item_sort_20170525` FORCE INDEX (`idx_uid`)", 
        "attached": "((`item_sort_20170525`.`uid` = 896588234) and (`item_sort_20170525`.`platform` in (12,11)))" 
       } 
       ] /* attached_conditions_summary */ 
      } /* attaching_conditions_to_tables */ 
      }, 
      { 
      "clause_processing": { 
       "clause": "ORDER BY", 
       "original_clause": "`item_sort_20170525`.`kw_id` desc", 
       "items": [ 
       { 
        "item": "`item_sort_20170525`.`kw_id`" 
       } 
       ] /* items */, 
       "resulting_clause_is_simple": true, 
       "resulting_clause": "`item_sort_20170525`.`kw_id` desc" 
      } /* clause_processing */ 
      }, 
      { 
      "clause_processing": { 
       "clause": "GROUP BY", 
       "original_clause": "`item_sort_20170525`.`kw_id`", 
       "items": [ 
       { 
        "item": "`item_sort_20170525`.`kw_id`" 
       } 
       ] /* items */, 
       "resulting_clause_is_simple": true, 
       "resulting_clause": "`item_sort_20170525`.`kw_id`" 
      } /* clause_processing */ 
      }, 
      { 
      "refine_plan": [ 
       { 
       "table": "`item_sort_20170525` FORCE INDEX (`idx_uid`)", 
       "pushed_index_condition": "((`item_sort_20170525`.`uid` = 896588234) and (`item_sort_20170525`.`platform` in (12,11)))", 
       "table_condition_attached": null, 
       "access_type": "range" 
       } 
      ] /* refine_plan */ 
      } 
     ] /* steps */ 
     } /* join_optimization */ 
    }, 
    { 
     "join_execution": { 
     "select#": 1, 
     "steps": [ 
      { 
      "creating_tmp_table": { 
       "tmp_table_info": { 
       "table": "intermediate_tmp_table", 
       "row_length": 13, 
       "key_length": 4, 
       "unique_constraint": false, 
       "location": "memory (heap)", 
       "row_limit_estimate": 161319 
       } /* tmp_table_info */ 
      } /* creating_tmp_table */ 
      }, 
      { 
      "filesort_information": [ 
       { 
       "direction": "desc", 
       "table": "intermediate_tmp_table", 
       "field": "kw_id" 
       } 
      ] /* filesort_information */, 
      "filesort_priority_queue_optimization": { 
       "limit": 21, 
       "rows_estimate": 540, 
       "row_size": 12, 
       "memory_available": 720896, 
       "chosen": true 
      } /* filesort_priority_queue_optimization */, 
      "filesort_execution": [ 
      ] /* filesort_execution */, 
      "filesort_summary": { 
       "rows": 22, 
       "examined_rows": 530, 
       "number_of_tmp_files": 0, 
       "sort_buffer_size": 440, 
       "sort_mode": "<sort_key, rowid>" 
      } /* filesort_summary */ 
      } 
     ] /* steps */ 
     } /* join_execution */ 
    } 
    ] /* steps */ 
} 
+0

表中有多少行?有多少人有'uid' = 2259613579? –

回答

1

您的疑問:

SELECT `kw_id`, COUNT(kw_id) AS `count` 
    FROM `item_sort_20170525` 
    WHERE `uid` = 896588234 
    AND `platform` IN (12, 11) 
    GROUP BY `kw_id` 
    ORDER BY `kw_id` DESC 
    LIMIT 21; 

在這裏,你有兩個過濾條件:一組uid平等和platform。 然後你有一個分組標準,這也是一個相反的排序標準。

您能否將platform條件從一個集合更改爲一個範圍?如果是這樣,那就做吧。 platform BETWEEN 11 AND 12。儘管如此,查詢計劃員看起來似乎只有一個問題。

然後嘗試一個以平等標準開始,然後有範圍標準,然後有分組標準的複合索引。在這種情況下:

(uid, platform, kw_id) 

應該讓您的查詢從索引範圍掃描滿意。將kw_id添加到索引使其成爲涵蓋索引,這意味着索引可以滿足查詢所需的所有內容。它也可能允許反向範圍掃描產生DESC排序。

此外,由於您已聲明kw_idNOT NULL,因此您可以使用COUNT(*)代替COUNT(kw_id)。這可能會有所幫助,但可能並不多。

專業提示:始終格式化您的查詢,以便您在查看時選擇,篩選,分組和排序條件跳出。您在表格中擁有的行數越多,這就越重要。

+0

非常感謝,我還有一個問題:爲什麼大數字不使用索引?不是大整數 – Coola

1

O.Jones的回答非常好。還有兩件事你可以嘗試:

SELECT kw_id, SUM(cnt) 
FROM ((SELECT `kw_id`, COUNT(*) AS cnt 
     FROM `item_sort_20170525` 
     WHERE `uid` = 896588234 AND `platform` = 11 
    ) UNION ALL 
     (SELECT `kw_id`, COUNT(*) AS cnt 
     FROM `item_sort_20170525` 
     WHERE `uid` = 896588234 AND `platform` = 12 
    ) 
    ) i 
GROUP BY `kw_id` 
ORDER BY `kw_id` DESC 
LIMIT 21; 

你想要相同的索引,item_sort_20170525(uid, platform, kw_id)。 MySQL應該能夠通過刪除內部組的文件排序。所以,如果沒有非常多的kw_id,那麼外部的group by應該不是非常昂貴的。

另一種選擇是使用相關的子查詢。這假設你在某個地方有一個kw_id的列表。查詢看起來像

select kw_id, 
     (select count(*) 
     from `item_sort_20170525` i 
     where i.kw_id = k.kw_id and `uid` = 896588234 and 
       `platform` in (11, 12) 
     ) as cnt 
from kw 
order by kw.kw_id desc; 

這個版本將工作會,如果大多數/所有kw_id■找在項目表中的至少一個匹配行。對於此查詢,您需要item_sort_20170525(kw_id, uid, platform)上的索引。

+0

使用gigarow規模的表格,值得嘗試各種各樣的東西來獲得高效的查詢!好的補充。 –

+0

非常感謝你,我還有一個其他問題: 爲什麼大數字不使用索引?不是大整數 – Coola