数据库:zhcxkj_center_strategy_comprehensive
查询时间:18.717
执行次数: 50
扫描行数:251120
发送行数:184.44
指纹SQL: SELECT id , promotion_id , promotion_name , promotion_type , source_id , source_name , site_id , offer_id , pf_listing_id , original_price , price , currency_id , STATUS , meli_percent , seller_percent , create_by , update_by , create_name , update_name , create_time , update_time , is_deleted , version FROM ? FORCE INDEX ( idx_source_site_promotion ) where source_id = ? and site_id = ? and promotion_id in ( ? , ? , ? ) and STATUS = ? and promotion_type = ? and is_deleted = ? limit ? ;
真实SQL:SELECT id, promotion_id, promotion_name, promotion_type, source_id, source_name, site_id, offer_id, pf_listing_id, original_price, price, currency_id, STATUS, meli_percent, seller_percent, create_by, update_by, create_name, update_name, create_time, update_time, is_deleted, version FROM t_promotion_candidate_detail_mercadolibre_2 FORCE INDEX(idx_source_site_promotion) where source_id = 29225 and site_id = MCO and promotion_id in ( P-MCO15205004 , P-MCO15383010 , P-MCO15385004 ) and STATUS = candidate and promotion_type = DEAL and is_deleted = 0 limit 200;
无人工修改
AISQL修改建议:SELECT id,
promotion_id,
promotion_name,
promotion_type,
source_id,
source_name,
site_id,
offer_id,
pf_listing_id,
original_price,
price,
currency_id,
STATUS,
meli_percent,
seller_percent,
create_by,
update_by,
create_name,
update_name,
create_time,
update_time,
is_deleted,
VERSION
FROM t_promotion_candidate_detail_mercadolibre_2
WHERE source_id = 29225
AND site_id = MCO
AND promotion_id IN (P-MCO15205004,P-MCO15383010,P-MCO15385004)
AND STATUS = candidate
AND promotion_type = DEAL
AND is_deleted = 0
ORDER BY id
LIMIT 200;
优化说明:
1. 移除了FORCE INDEX强制索引提示,让优化器自动选择最优索引
2. 添加了ORDER BY子句确保结果有序,避免无排序的LIMIT可能带来的性能问题
3. 将IN列表中的值紧凑排列,减少SQL文本大小
4. 查询条件已经非常具体,且使用了多个等值条件,索引选择性应该很好
如果现有索引idx_source_site_promotion包含(source_id, site_id, promotion_id)字段,则优化器会自动选择该索引。
explain表:
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | SIMPLE | t_promotion_candidate_detail_mercadolibre_2 | None | range | idx_source_site_promotion | idx_source_site_promotion | 1599 | None | 364912 | 10 | Using index condition; Using where |
额外建议:
索引建议:
取出表 【t_promotion_candidate_detail_mercadolibre_2】 where条件表达式 【source_id = 29225】 100000 条记录,重复的数据有:【100000】 条,没有必要为该字段创建索引。
取出表 【t_promotion_candidate_detail_mercadolibre_2】 where条件表达式 【site_id = MCO】 100000 条记录,重复的数据有:【100000】 条,没有必要为该字段创建索引。
取出表 【t_promotion_candidate_detail_mercadolibre_2】 where条件表达式 【STATUS = candidate】 100000 条记录,重复的数据有:【100000】 条,没有必要为该字段创建索引。
取出表 【t_promotion_candidate_detail_mercadolibre_2】 where条件表达式 【promotion_type = DEAL】 100000 条记录,重复的数据有:【58323】 条,没有必要为该字段创建索引。
取出表 【t_promotion_candidate_detail_mercadolibre_2】 where条件表达式 【is_deleted = 0】 100000 条记录,重复的数据有:【88891】 条,没有必要为该字段创建索引。
→ 【t_promotion_candidate_detail_mercadolibre_2】表 【promotion_id】字段,索引已经存在,无需添加任何索引。
【t_promotion_candidate_detail_mercadolibre_2】表 【promotion_id】字段,索引分析:
+--------------------------------+--------------------------------+---------------+---------------+
| TABLE_NAME | INDEX_NAME | COLUMN_NAME | CARDINALITY |
+================================+================================+===============+===============+
| t_promotion_candidate_detail_m | idx_promotion_id_source_id_sit | promotion_id | 8949 |
| ercadolibre_2 | e_id_version_is_deleted | | |
+--------------------------------+--------------------------------+---------------+---------------+
| t_promotion_candidate_detail_m | idx_source_promotion_status | promotion_id | 17089 |
| ercadolibre_2 | | | |
+--------------------------------+--------------------------------+---------------+---------------+
| t_promotion_candidate_detail_m | idx_source_site_promotion | promotion_id | 17912 |
| ercadolibre_2 | | | |
+--------------------------------+--------------------------------+---------------+---------------+
这个问题同:https://pm.zhcxkj.com/zentao/bug-view-59150.html
强制索引是你们上次提的工单要求加的