使用group by和order by的查询优化

时间:2014-11-10 10:38:30

标签: php mysql query-optimization

我正在研究mysql查询一个每分钟都在增加的表。该查询也用于具有limit子句的分页。该表包含在商店网站上访问的用户会话。我已经为store_id,session_id添加了索引 以下是查询。

SELECT *,
    GROUP_CONCAT(email) as email_id 
FROM 
( 
    SELECT * 
    FROM clickstream 
    WHERE clickstream.store_id = '' 
    ORDER BY id DESC 
) as clck 
GROUP BY session_id 
ORDER BY server_now DESC 
LIMIT ".$offset.",".PER_PAGE_PRODUCT_COUNT

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

store_id上的索引可以帮助您完成此查询,您无法通过其他索引进一步加快查询速度。您只需删除子查询:

SELECT 
    *,
    GROUP_CONCAT(email ORDER BY id) as email_id
FROM 
    clickstream 
WHERE 
    clickstream.store_id = '' 
GROUP BY 
    session_id 
ORDER BY 
    server_now DESC 
LIMIT 
    $offset,PER_PAGE_PRODUCT_COUNT