-1

## MySql Server 5.5,数据库引擎 MyIsam,表 fact_transaction 在 date_key、time_key、unit_cost_price、unit_retail_price(组合键)和 fact_stockout_sales 上索引,不包括 time_key。##

查询计划

查询计划

询问

SELECT 
    t.Level, t.Name, t.KeyValue, 
    ROUND( (SUM(t.Gross)/SUM(t.Revenue))*100, 2 ) AS Value, 
    ROUND( (SUM(t.adjustedGross)/SUM(t.adjustedRevenue))*100, 2 ) AS adjustedValue,
    t.dataType AS dataType 
FROM 
   (SELECT "item" AS Level, ds.product_name AS Name, ds.product_id AS KeyValue, 
        SUM(ft.gross_profit) AS Gross, 
        SUM(ft.selling_amount) AS Revenue, 
        SUM(ft.adjusted_gross_profit) AS adjustedGross, 
        SUM(ft.adjusted_selling_amount) AS adjustedRevenue,
        "%" AS dataType 
    FROM fact_transaction AS ft 
    JOIN dim_sku AS ds ON ft.sku_key = ds.sku_key 
    WHERE ft.date_key BETWEEN 20080215 AND 20130107 
      AND ft.time_key BETWEEN 100 AND 235900 
      AND ft.unit_cost_price BETWEEN 0 AND 1333 
      AND ft.unit_retail_price BETWEEN 0 AND 16500 
      AND ft.store_key IN ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16") 
      AND ds.product_id IN (1312009,1312007,... Huge List say 30000) 
      AND ds.category IN ("Male","Female","Unisex") 
      AND ft.day_of_week IN ("1","2","3","4","5","6","7") 
      AND ds.collection_name IN ("Base","SS12","AW12") 
    GROUP BY ds.product_id                          
    UNION 
    SELECT "item" AS Level, ds.product_name AS Name, ds.product_id AS KeyValue, 
        SUM(ft.gross_profit) AS Gross, 
        SUM(ft.selling_amount) AS Revenue, 
        SUM(ft.adjusted_gross_profit) AS adjustedGross, 
        SUM(ft.adjusted_selling_amount) AS adjustedRevenue, 
        "%" AS dataType 
    FROM fact_stockout_sales AS ft 
    JOIN dim_sku AS ds ON ft.sku_key = ds.sku_key 
    WHERE ft.date_key BETWEEN 20080215 AND 20130107 
      AND ft.unit_cost_price BETWEEN 0 AND 1333 
      AND ft.unit_retail_price BETWEEN 0 AND 16500 
      AND ft.store_key IN ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16") 
      AND ds.product_id IN (1312009,1312007,.. Huge List say 30000) 
      AND ds.category IN ("Male","Female","Unisex") 
      AND ft.day_of_week IN ("1","2","3","4","5","6","7") 
      AND ds.collection_name IN ("Base","SS12","AW12") 
      GROUP BY ds.product_id) AS t 
GROUP BY t.KeyValue
4

1 回答 1

0
  1. 测量运行时间。

  2. 测量作为 UNION 运算符一部分的每个查询的运行时间。

  3. 索引 WHERE 子句中使用的每一列。

  4. 将选择性最高的列放在 WHERE 子句中,并测试多列索引的效果。

  5. 减少无用的测试。(当然 ft.day_of_week 可以从 WHERE 子句中删除。)

  6. 重新考虑您的数据类型。星期几和存储键真的是字符串吗?

  7. 重新考虑一次选择五年数据的决定。

  8. 尝试将产品 ID 编号移动到临时表中,然后加入其中。

于 2013-08-03T14:08:01.143 回答