Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
join mysql中未使用索引_Mysql_Indexing - Fatal编程技术网

join mysql中未使用索引

join mysql中未使用索引,mysql,indexing,Mysql,Indexing,我试图改进一些连接两个表的查询的性能,我在表gio_A_Master_SL_temp中为columskuconfig添加了一个名为skuconf的索引,在表gio_cat_hist_SL_temp中为列sku_config添加了一个名为skuconf的索引,但是我可以通过explain命令看到索引没有在表的联合中使用(仅在创建派生表时使用) 这是一个问题 EXPLAIN SELECT a.Country, a.MonthNum, a.CatBP, a.Cat1, a.Cat2, a.Cat3,

我试图改进一些连接两个表的查询的性能,我在表gio_A_Master_SL_temp中为columskuconfig添加了一个名为skuconf的索引,在表gio_cat_hist_SL_temp中为列sku_config添加了一个名为skuconf的索引,但是我可以通过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
;
这是解释结果的图像

我添加索引的列是varchar(28)类型,它们都具有与showfullcolumn命令相同的排序规则

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 
我不知道join没有使用哪些索引,有没有办法让索引为表的连接工作?。此外,任何改进查询的建议都是非常受欢迎的。提前感谢。

试试这个:

    (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

这些桌子有多大?使用索引然后进行查找是有代价的,所以有时只需进行完整扫描就可以了。此外,您也不能加入索引表。您正在使用
group by
@JuanCarlosOropeza创建一个时态表,即使没有
group by
,子查询也可能会在
联接中忽略索引(虽然它们可能会帮助被
s分离的
组。@Uueerdo这就是我在提到时态表时所说的,不是吗?@JuanCarlosOropeza我只是认为值得指出的是,这些临时表不会从索引中受益。这是一个无效的组,因为它不知道哪个
国家
cat3
,等等。否。它将在进行单独的求和和和计数之前进行逻辑连接。因此,数字将被夸大。