0

我在下面的mysql数据库中有这个查询。该查询在大约 1-2 百万行上运行大约需要 6 秒。我有日期、uniqueuserid、collectiontype 的索引。

如何在 mysql 中提高此查询的性能?

select 
DATE_FORMAT(MIN(collection.date),'%Y-%m-%d %H:00') as date, 
COUNT(distinct collection.uniqueuserid) as convertingusers, 
SUM(case when collection.type = 1 then valueofitem end) as valueofitem, 
SUM(case when collection.type = 1 then 1 end) as numofitems, 
SUM(case when collection.type = 1 and collection.network = 1 then collection.valueofitem end) as col1valueofitem, 
SUM(case when collection.type = 1 and collection.network = 1 then 1 end) as col1numofitem, 
SUM(case when collection.type = 1 and collection.network = 2 then collection.valueofitem end) as col2valueofitem, 
SUM(case when collection.type = 1 and collection.network = 2 then 1 end) as col2numofitem, 
SUM(case when collection.type = 1 and collection.network = 3 then collection.valueofitem end) as col3valueofitem, 
SUM(case when collection.type = 1 and collection.network = 3 then 1 end) as col3numofitem, 
SUM(case when collection.type = 2 then collection.valueofitem end) as collectiontypeB, 
SUM(case when collection.type = 3 then collection.valueofitem end) as collectiontypeC,
COUNT(distinct collection.uniqueuserid) as convertingusers 

from collection
where collection.date > date_sub(now(),INTERVAL 3 WEEK) 
group by DATE_FORMAT(collection.date,'%Y-%m-%d') order by collection.date DESC limit 500

根据要求,我在查询之前使用“EXPLAIN”进行了分析

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   PRIMARY     ALL                 1196352 Using temporary; Using filesort
2   DERIVED collection  ALL                 1196352 
4

1 回答 1

0

夫妻心得:

  1. 您正在分组,DATE_FORMAT(collection.date,'%Y-%m-%d')但您正在选择时间 ( DATE_FORMAT(MIN(collection.date),'%Y-%m-%d %H:00'))
  2. 如果collection.date是一个datetime字段,然后GROUP BY DATE(collection.date)SELECT.

我建议将CASEs 放在子查询中,然后像这样执行汇总(您需要添加列别名):

SELECT DATE_FORMAT(a.date,'%Y-%m-%d %H:00') AS date,
    COUNT(a.uniqueuserid) AS convertingusers, 
    SUM(a.valueofitem),
    SUM(a.numofitems),
    SUM(a.col1valueofitem),
    SUM(a.col1numofitem),
    SUM(a.col2valueofitem),
    SUM(a.col2numofitem),
    SUM(a.col3valueofitem),
    SUM(a.col3numofitem),
    SUM(a.collectiontypeB)
FROM
    (SELECT collection.date AS date,
        collection.uniqueuserid AS convertingusers,
        CASE
        WHEN collection.type = 1
            THEN valueofitem
        END AS valueofitem,
        CASE
        WHEN collection.type = 1
            THEN 1
        END AS numofitems,
        CASE
        WHEN collection.type = 1 AND collection.network = 1 
            THEN collection.valueofitem
        END AS col1valueofitem,
        CASE
        WHEN collection.type = 1 AND collection.network = 1
            THEN 1
        END AS col1numofitem,
        CASE
        WHEN collection.type = 1 AND collection.network = 2
            THEN collection.valueofitem
        END AS col2valueofitem,
        CASE
        WHEN collection.type = 1 AND collection.network = 2
            THEN 1
        END AS col2numofitem,
        CASE
        WHEN collection.type = 1 AND collection.network = 3
            THEN collection.valueofitem
        END AS col3valueofitem,
        CASE
        WHEN collection.type = 1 AND collection.network = 3
            THEN 1
        END AS col3numofitem,
        CASE
        WHEN collection.type = 2
            THEN collection.valueofitem
        END AS collectiontypeB,
        CASE
            WHEN collection.type = 3
            THEN collection.valueofitem
        END AS collectiontypeC
    FROM collection
    WHERE collection.date > DATE_SUB(NOW(), INTERVAL 3 WEEK)) a
GROUP BY DATE(a.date)
ORDER BY a.date DESC
LIMIT 500
于 2012-09-26T23:21:38.127 回答