我的查询是这样的:
SELECT date_format( created_at, '%Y-%m-%d' ) AS the_date,
COUNT(s.id) AS total,
(SELECT COUNT(ks.id) FROM kc_shares ks WHERE site = 'facebook' AND date_format( created_at, '%Y-%m-%d' ) = the_date ) AS total_facebook,
(SELECT COUNT(ks.id) FROM kc_shares ks WHERE site = 'twitter' AND date_format( created_at, '%Y-%m-%d' ) = the_date ) AS total_twitter
FROM `kc_shares` s
GROUP BY `the_date`
我想要得到的是每日分享的数量,其中包含总份额、facebook 的总份额(因此 site = 'facebook')和 twitter 的总份额。这就是为什么我需要GROUP BY
.
当它有几千行时,没有问题。但是该表目前几乎有 200,000 行,并且查询非常慢,大约需要 20-30 秒,我猜还要更多。
我尝试向站点和 created_at 字段添加索引,但无济于事。
谢谢