使用group by子句进行MySQL查询优化

时间:2014-05-02 09:21:51

标签: mysql sql query-optimization

我想根据国家/地区,合作伙伴和零售商计算总点击次数和唯一次点击次数。 我已经达到了预期的效果,但我认为它不是最佳解决方案,对于更长的数据集,它需要更长的时间。我该如何改进这个查询? 这是我的测试表,设计查询和预期输出:

"country_id","partner","retailer","id_customer","id_clickout"
"1","A","B","100","XX"
"1","A","B","100","XX"
"2","A","B","100","XX"
"2","A","B","100","GG"
"2","A","B","100","XX"
"2","A","B","101","XX"

DROP TABLE IF EXISTS x;
CREATE TEMPORARY TABLE x AS
SELECT test1.country_id, test1.partner,test1.retailer, test1.id_customer, 
SUM(CASE WHEN test1.id_clickout IS NULL THEN 0 ELSE 1 END) AS clicks,
CASE WHEN test1.id_clickout IS NULL THEN 0 ELSE 1 END AS unique_clicks
FROM test1
GROUP BY 1,2,3,4
;
SELECT country_id,partner,retailer, SUM(clicks), SUM(unique_clicks)
FROM x
GROUP BY 1,2,3

输出:

"country_id","partner","retailer","SUM(clicks)","SUM(unique_clicks)"
"1","A","B","2","1"
"2","A","B","4","2"

这是DDL和输入数据:

CREATE TABLE test (
 country_id INT(11) DEFAULT NULL,
 partner VARCHAR(256) CHARACTER SET utf8 DEFAULT NULL,
 retailer VARCHAR(256) CHARACTER SET utf8 DEFAULT NULL,
 id_customer BIGINT(20) DEFAULT NULL,
 id_clickout VARCHAR(256) CHARACTER SET utf8 DEFAULT NULL)
  ENGINE=InnoDB DEFAULT CHARSET=utf8;


INSERT INTO test VALUES(1,'A','B','100','XX'),(1,'A','B','100','XX'),
            (2,'A','B','100','XX'),(2,'A','B','100','GG'),
            (2,'A','B','100','XX'),(2,'A','B','101','xx')

2 个答案:

答案 0 :(得分:1)

我认为这就是你所追求的目标:

SELECT country_id,partner,retailer,COUNT(retailer) as `sum(clicks)`,count(distinct id_clickout) as `SUM(unique_clicks)`
FROM test1
GROUP BY country_id,partner,retailer

结果:

COUNTRY_ID  PARTNER  RETAILER  SUM(CLICKS)  SUM(UNIQUE_CLICKS)
1           A        B         2            1
2           A        B         4            2

请参阅SQL Fiddle中的结果。

答案 1 :(得分:1)

SELECT
  country_id,
  partner,
  retailer,
  COUNT(id_clickout)   AS clicks,
  COUNT(DISTINCT CASE WHEN id_clickout IS NOT NULL THEN id_customer END) AS unique_clicks
FROM
  test1
GROUP BY
  1,2,3
;

COUNT(a_field)不会计算任何NULL值。

因此,COUNT(id_clickout)只计算NOT NULL的次数。

同样,CASE WHEN中的unique_clicks语句仅返回id_customer所显示的记录,否则返回NULL。这意味着COUNT(DISTINCT CASE)仅计算不同的客户,并且仅在他们点击时计算。

编辑:

我刚刚意识到,它可能比那更简单...

SELECT
  country_id,
  partner,
  retailer,
  COUNT(*)                    AS clicks,
  COUNT(DISTINCT id_customer) AS unique_clicks
FROM
  test1
WHERe
  id_clickout IS NOT NULL
GROUP BY
  1,2,3
;

结果中唯一的重大差异在于,之前显示country_id, partner, retailed的任何0 clicks现在都不会出现在结果中。

country_id, partner, retailed, id_clickout, id_customercountry_id, partner, retailed, id_customer, id_clickout上使用INDEX时,此查询应该明显加快。