我正在两个表上执行MySQL查询
keyword_stats
+-------------+---------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| keywordid | int(11) | NO | MUL | NULL | |
| campaignid | int(11) | NO | | NULL | |
| clicks | int(11) | NO | MUL | NULL | |
| impressions | int(11) | NO | MUL | NULL | |
| cost | decimal(10,2) | NO | MUL | NULL | |
| conversions | int(11) | NO | MUL | NULL | |
| timestamp | timestamp | NO | MUL | CURRENT_TIMESTAMP | |
| statsdate | date | NO | MUL | NULL | |
+-------------+---------------+------+-----+-------------------+----------------+
和
关键字表
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| clientid | int(11) | NO | MUL | NULL | |
| campaignid | int(11) | NO | | NULL | |
| keywordid | int(11) | NO | MUL | NULL | |
| text | varchar(125) | NO | MUL | NULL | |
| status | varchar(10) | NO | MUL | NULL | |
+------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
使用以下SQL查询
SELECT
k.status, statsdate,
SUM( impressions ) AS impressions, SUM( clicks ) AS clicks,
SUM( conversions ) AS conversions, SUM( cost ) AS cost
FROM keyword_stats
LEFT JOIN
(
SELECT
text,keywordid,status
FROM keywords
) AS k USING (keywordid)
WHERE
campaignid = 56486451
AND statsdate BETWEEN '2011-03-01' AND '2011-03-23'
AND k.status = "enabled"
GROUP BY keywordid
ORDER BY conversions DESC, clicks DESC, impressions DESC LIMIT 0, 10
结果
+---------+------------+-------------+--------+-------------+-----------+
| status | statsdate | impressions | clicks | conversions | cost |
+---------+------------+-------------+--------+-------------+-----------+
| enabled | 2011-03-01 | 71256166 | 242079 | 4247 | 891572.71 |
| enabled | 2011-03-01 | 1101 | 10 | 1 | 43.19 |
| enabled | 2011-03-01 | 210 | 6 | 0 | 23.40 |
| enabled | 2011-03-01 | 331 | 4 | 0 | 15.29 |
| enabled | 2011-03-01 | 672 | 3 | 0 | 11.41 |
| enabled | 2011-03-01 | 486 | 2 | 0 | 8.93 |
| enabled | 2011-03-01 | 254 | 2 | 0 | 9.48 |
| enabled | 2011-03-01 | 2201 | 1 | 0 | 2.96 |
| enabled | 2011-03-01 | 581 | 1 | 0 | 3.25 |
| enabled | 2011-03-01 | 483 | 1 | 0 | 4.39 |
+---------+------------+-------------+--------+-------------+-----------+
10 rows in set (12.12 sec)
查询运行并按预期执行,除非您可能会看到第一个结果包含意外结果,因为任何关键字都无法将点击次数,转化次数或费用作为总和我想要计算的数字MySQL正在组合总计以实现该结果
编辑添加关键字
+------------+---------+------------+-------------+--------+-------------+-----------+
| keywordid | status | statsdate | impressions | clicks | conversions | cost |
+------------+---------+------------+-------------+--------+-------------+-----------+
| 2147483647 | enabled | 2011-03-01 | 71256166 | 242079 | 4247 | 891572.71 |
| 101936939 | enabled | 2011-03-01 | 1101 | 10 | 1 | 43.19 |
| 23039553 | enabled | 2011-03-01 | 210 | 6 | 0 | 23.40 |
| 117364874 | enabled | 2011-03-01 | 331 | 4 | 0 | 15.29 |
| 18862051 | enabled | 2011-03-01 | 672 | 3 | 0 | 11.41 |
| 16695651 | enabled | 2011-03-01 | 486 | 2 | 0 | 8.93 |
| 14690232 | enabled | 2011-03-01 | 254 | 2 | 0 | 9.48 |
| 18046691 | enabled | 2011-03-01 | 2201 | 1 | 0 | 2.96 |
| 22232901 | enabled | 2011-03-01 | 581 | 1 | 0 | 3.25 |
| 15072731 | enabled | 2011-03-01 | 483 | 1 | 0 | 4.39 |
+------------+---------+------------+-------------+--------+-------------+-----------+
10 rows in set (11.99 sec)
答案 0 :(得分:1)
keywordid
在两个表中均为PRIMARY KEY
。
最有可能的是,您有两个表中有大量记录的keywordid
会导致此keywordid
上的交叉联接。
另请注意,首先,LEFT JOIN
在您的查询中是多余的,因为您正在对k.status
进行过滤,其次,MySQL
在优化内联视图方面效果不佳。< / p>
请使用:
SELECT k.status, statsdate,
SUM( impressions ) AS impressions, SUM( clicks ) AS clicks,
SUM( conversions ) AS conversions, SUM( cost ) AS cost
FROM keyword_stats
JOIN keywords
USING (keywordid)
WHERE campaignid = 56486451
AND statsdate BETWEEN '2011-03-01' AND '2011-03-23'
AND k.status = "enabled"
GROUP BY
keywordid
ORDER BY
conversions DESC, clicks DESC, impressions DESC
LIMIT 0, 10
答案 1 :(得分:0)
您是否需要每天的信息?或者你需要所有日子的数据总和吗?
如果您想要每天的数据,请按照表达式将“statsdate”放入您的组中。
然后,我觉得你不需要做一个子查询。您可以将表名放在左连接中。
如果您希望每个“keywordid”的数据都在结果字段中输入“keywordid”。状态不是强制性的,因为始终是“已启用”
示例:
SELECT
keywordid, statsdate,
SUM( impressions ) AS impressions, SUM( clicks ) AS clicks,
SUM( conversions ) AS conversions, SUM( cost ) AS cost
FROM keyword_stats
LEFT JOIN keywords k USING (keywordid)
WHERE
campaignid = 56486451
AND statsdate BETWEEN '2011-03-01' AND '2011-03-23'
AND k.status = "enabled"
GROUP BY keywordid, statsdate
ORDER BY conversions DESC, clicks DESC, impressions DESC LIMIT 0, 10