专家们,
我需要一个MySQL查询的帮助。它包括两张表格:“二手车”和“县”。
“二手车”:16+百万记录,
“县”:3100份记录
我的查询需要几分钟才能完成。很难排除故障,因为MySQL缓存结果,所以当我重新运行相同的查询时,它会出现在一秒钟以下!
我确信这个子查询会让我心灰意冷,所以我需要一些提示作为替代方案。
这是一个样本:
SELECT p.make,p.total,sum(p.total) AS theCount,
sum(p.total)/
(SELECT sum(p.total) AS theCount FROM usedcars p, counties c
WHERE c.FIPS IN (55009,55061,55083) AND c.statecounty = p.statecounty) AS MktPerc
FROM usedcars p, counties c
WHERE c.FIPS IN (55009,55061,55083)
AND c.statecounty = p.statecounty
GROUP BY make
ORDER BY theCount DESC我正在寻找在特定县按品牌销售的汽车数量,以及该品牌在相同情况下的市场份额(mktPerc)。
市场份额:按品牌销售的汽车数量/按品牌销售的汽车数量
以下是解释:
http://1drv.ms/NqVuYT
下面是一个返回示例。它给了我我想要的。只是时间太长了!
http://1drv.ms/1egJ0xm
发布于 2014-03-18 16:06:38
我会简化以下几点。
SELECT
p.make,
SUM( p.total ) SumTotal,
AVG( p.total ) AvgTotal,
AllBrands.TotalAllBrands,
SUM( p.total ) / AllBrands.TotalAllBrands as MtkPcnt
from
counties c
JOIN usedCars p
ON c.statecounty = p.statecounty,
( SELECT
SUM( p.total ) TotalAllBrands
from
counties c
JOIN usedCars p
ON c.statecounty = p.statecounty,
where
c.FIPS in ( 55009,55061,55083 ) ) AllBrands
where
c.FIPS in ( 55009,55061,55083 ) )
group by
p.make
order by
sum( p.total ) desc为了帮助优化,我需要通过以下方法覆盖索引
table index
counties ( FIPS, StateCounty )
usedCars ( stateCounty, make, total )发布于 2014-06-10 18:50:56
请确保有以下索引:
二手车->状态
国家-> (国际收支计划,州)
使用以下查询:
ALTER TABLE usedcars ADD INDEX usedcar_index (statecounty);
ALTER TABLE countries ADD INDEX country_index (FIPS, statecounty);https://stackoverflow.com/questions/22483560
复制相似问题