这是我的问题:
SELECT IFNULL(d_source,"TOTAL") AS SOURCE
, IFNULL(SUM(STORECOUNT),0) AS STORECOUNT
, IFNULL(SUM(STORECOUNTCP),0) AS STORECOUNTCP
, IFNULL(SUM(TDP),0) AS TDP
, IFNULL(SUM(TDPCP),0) AS TDPCP
, IFNULL(ROUND(SUM(TDP) / SUM(STORECOUNT),2),0) AS AVGUPCSPERSTORE
, IFNULL(ROUND(SUM(TDPCP) / SUM(STORECOUNTCP),2),0) AS AVGUPCSPERSTORECP
, IFNULL(ROUND(SUM(TOTAL) / SUM(STORECOUNT) / 8.57,2),0) AS AVGUNITSPERSTORE
, IFNULL(ROUND(SUM(CPTOTAL) / SUM(STORECOUNTCP) / 4.29,2),0) AS AVGUNITSPERSTORECP
, IFNULL(ROUND(SUM(TOTAL),0),0) AS TOTAL
, IFNULL(ROUND(SUM(CPTOTAL),0),0) AS CPTOTAL
, IFNULL(ROUND(SUM(PPTOTAL),0),0) AS PPTOTAL
, ROUND(sum(2016_Apr),0) AS 2016_Apr , ROUND(sum(2016_May),0) AS 2016_May
FROM (
SELECT d_source
,key_retailer
,COUNT(DISTINCT(CASE WHEN datetimesql BETWEEN '2016-04-01' AND '2016-05-31' AND unit_sales > 0 THEN store_id END)) STORECOUNT
,COUNT(DISTINCT(CASE WHEN datetimesql BETWEEN '2016-05-01' AND '2016-05-31' AND unit_sales > 0 THEN store_id END)) STORECOUNTCP
,COUNT(DISTINCT(CASE WHEN datetimesql BETWEEN '2016-04-01' AND '2016-05-31' AND unit_sales > 0 THEN CONCAT(upc,store_id) END)) TDP
,COUNT(DISTINCT(CASE WHEN datetimesql BETWEEN '2016-05-01' AND '2016-05-31' AND unit_sales > 0 THEN CONCAT(upc,store_id) END)) TDPCP
,SUM(CASE WHEN datetimesql BETWEEN '2016-04-01' AND '2016-05-31' THEN unit_sales ELSE 0 END) TOTAL
,SUM(CASE WHEN datetimesql BETWEEN '2016-05-01' AND '2016-05-31' THEN unit_sales ELSE 0 END) CPTOTAL
,SUM(CASE WHEN datetimesql BETWEEN '2016-04-01' AND '2016-04-30' THEN unit_sales ELSE 0 END) PPTOTAL
,SUM(CASE WHEN datetimesql BETWEEN '2016-04-01' AND '2016-04-30' THEN unit_sales ELSE 0 END) 2016_Apr
,SUM(CASE WHEN datetimesql BETWEEN '2016-05-01' AND '2016-05-31' THEN unit_sales ELSE 0 END) 2016_May
FROM storeCheckRecords
INNER JOIN allStores ON allStores.id = store_id
WHERE d_source !='STORELOCATOR'
GROUP BY d_source
) T
GROUP BY d_source WITH ROLLUP
以下是解释:
id s_type table type p_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 2 Using filesort
2 DERIVED SCR ALL store_id NULL NULL NULL 1229950 Using where; Using filesort
2 DERIVED allStores eq_ref PRIMARY PRIMARY 4 dfc.SCR 1 Using where
我不明白我缺少什么来停止搜索整个SCR表并对其进行索引以便它不会搜索所有SCR表。感谢您的帮助。