这个表包含1.2mill的结果,我无法编辑它,因为有些应用程序我也没有源代码来访问它。我想添加一个数量字段,但我不能。
以下是我正在使用的查询:
SELECT SUM(assets.hourlyEarnings) as earnings,
assets_inventory.uid
FROM (assets)
JOIN assets_inventory ON assets.id = assets_inventory.assetID
WHERE assets_inventory.uid IN (SELECT users.uid
FROM users
WHERE users.assetTime < 1305350756)
GROUP BY uid
有许多重复记录。
这是表格:
CREATE TABLE IF NOT EXISTS assets_inventory (
id int(11) NOT NULL AUTO_INCREMENT,
uid bigint(20) NOT NULL,
assetID int(11) NOT NULL,
PRIMARY KEY (id),
KEY uid (uid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1231992 ;
平均而言,我需要6-7秒才能得出结果,任何提高速度的建议都将受到赞赏!
答案 0 :(得分:1)
如果您需要所有uid
值的列表,是否有相关收入:
SELECT DISTINCT
ai.uid,
COALESCE(x.earnings, 0) AS earnings
FROM ASSETS_INVENTORY ai
LEFT JOIN (SELECT t.id,
SUM(t.hourlyearnings) AS earnings
FROM ASSETS t
GROUP BY t.id) x ON x.id = ai.assetid
WHERE EXISTS (SELECT NULL
FROM USERS u
WHERE u.uid = ai.uid
AND u.assettime < 1305350756)
否则:
SELECT ai.uid,
SUM(a.hourlyearnings) AS earnings
FROM ASSETS_INVENTORY ai
JOIN ASSETS a ON a.id = ai.assetid
WHERE EXISTS (SELECT NULL
FROM USERS u
WHERE u.uid = ai.uid
AND u.assettime < 1305350756)
GROUP BY ai.uid
...或:
SELECT ai.uid,
SUM(a.hourlyearnings) AS earnings
FROM ASSETS_INVENTORY ai
JOIN ASSETS a ON a.id = ai.assetid
JOIN USERS u ON u.uid = ai.uid
AND u.assettime < 1305350756
GROUP BY ai.uid