我试图提高连接两个表的某个查询的性能,我已经为表gio_A_Master_SL_temp添加了一个名为skuconf的索引,并为该列添加了一个名为skuconf的索引,以及一个名为skuconf的索引。表gio_cat_hist_SL_temp中的sku_config,但我可以通过explain命令看到索引没有在表的联合中使用(仅在创建派生表时)
这是查询
EXPLAIN SELECT
a.Country,
a.MonthNum,
a.CatBP,
a.Cat1,
a.Cat2,
a.Cat3,
sum(a.SKU_sold) as SKU_sold,
sum(a.items) AS items,
sum(a.Revenue) AS revenue,
sum(b.SKU_visible) AS SKU_visible
FROM
(SELECT
Country,
MonthNum,
SKUConfig,
CatBP,
Cat1,
Cat2,
Cat3,
count(DISTINCT SKUConfig) AS SKU_sold,
sum(OrderAfterCan) AS items,
sum(NMV) AS Revenue
FROM
gio_A_Master_SL_temp
GROUP BY
SKUConfig) a
LEFT JOIN
(SELECT
sku_config,
count(*) AS SKU_Visible
FROM
gio_cat_hist_SL_temp
GROUP BY
sku_config) b ON a.SKUConfig = b.sku_config
GROUP BY a.CatBP, a.Cat1, a.Cat2, a.Cat3
;
这是EXPLAIN结果的图像
我添加索引的列是类型varchar(28),并且它们都具有与SHOW FULL COLUMN命令显示的相同的排序规则
SKUconfig varchar(28) utf8_general_ci YES MUL select,insert,update,references
sku_config varchar(28) utf8_general_ci YES MUL select,insert,update,references
我不知道连接没有使用索引,有没有办法让索引能够用于联合表?此外,非常欢迎任何改进查询的建议。提前谢谢。
答案 0 :(得分:0)
试试这个:
(SELECT
a.Country,
a.MonthNum,
a.SKUConfig,
a.CatBP,
a.Cat1,
a.Cat2,
a.Cat3,
count(DISTINCT a.SKUConfig) AS SKU_sold,
sum(a.OrderAfterCan) AS items,
sum(a.NMV) AS Revenue,
count(b.SKUConfig) AS SKU_Visible
FROM
gio_A_Master_SL_temp a
LEFT JOIN
gio_cat_hist_SL_temp b
ON
a.SKUConfig = b.sku_config
GROUP BY
SKUConfig
) combine_result