【发布时间】:2015-09-17 05:56:17
【问题描述】:
在chipChange 表中,我有数百万条记录。我想学习的是优化下面查询的方法。目前看来这需要几个小时才能完成
- 从chipChange表中获取数据
- 更新 playerStats 表
您认为我可以如何提高此类查询的性能?
UPDATE playerStats pst
INNER JOIN
(
Select
chipChange.uid,
sum(case when (type=2) and (eventId!=16 and eventId!=17 and eventId!=18 and eventId!=10) then 1 else 0 end) sum1,
sum(case when (type=1 or type=3 or type=9) and (eventId!=16 and eventId!=17 and eventId!=18 and eventId!=10) then 1 else 0 end) sum2,
sum(case when type=2 and eventId=10 then 1 else 0 end) sum3,
sum(case when (type=1 or type=3 or type=9) and eventId=10 then 1 else 0 end) sum4,
sum(case when type=2 and (eventId=16 or eventId=17 or eventId=18) then 1 else 0 end) sum5,
sum(case when (type=1 or type=3 or type=9) and (eventId=16 or eventId=17 or eventId=18) then 1 else 0 end) sum5
from chipChange
where (type=1 or type=2 or type=3 or type=9)
group by uid
) cht on pst.uid=cht.uid
SET
pst.total1 = cht.sum1 + cht.sum2,
pst.total2 = cht.sum1,
pst.total3 = cht.sum3 + cht.sum4,
pst.total4 = cht.sum3,
pst.total5 = cht.sum5 + cht.6,
pst.total6 = cht.sum5;
【问题讨论】:
-
请为两张表提供
SHOW CREATE TABLE。 -
你为什么需要那个? @RickJames 注意:uid 列在两个表中都有索引。
-
这是一些有用的信息。然后是使用的引擎。以及行是否加载了统计信息以外的内容。也许数据类型会激发一些需要提出的观点。
-
另外,如果您运行的是 5.6,请提供
EXPLAIN UPDATE ...,以便我们验证它是否以预期的方式执行查询。
标签: mysql database performance