0

I have this query below in a mysql database. The query is taking ~6 seconds to run on ~1-2 million rows. I have indexs on date, uniqueuserid, collectiontype.

How can I increase the performance of this query in 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

As requested, I've run an analysis with "EXPLAIN" before the query

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.datedatetimeフィールドの場合GROUP BY DATE(collection.date)、 で書式設定を行いSELECTます。

s をサブクエリ内に配置してから、次のようにロールアップを実行することをお勧めしCASEます (列エイリアスを追加する必要があります)。

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 に答える