SELECT
COUNT(keywords) AS count_keywords
FROM (
SELECT * FROM keyword_searches GROUP BY keywords, ip
) tmp_table
WHERE created_at BETWEEN '2012-03-31 00:00:00' AND '2015-03-31 23:59:59';
如何优化此查询?
答案 0 :(得分:0)
首先使用内部查询中的where
条件过滤记录,然后在外部查询中计算keywords
。
SELECT
COUNT(keywords) AS count_keywords
FROM (
SELECT * FROM keyword_searches WHERE created_at BETWEEN '2012-03-31 00:00:00' AND '2015-03-31 23:59:59' GROUP BY keywords, ip
) tmp_table ;
在您的情况下,您获得的所有记录都不满足条件BETWEEN '2012-03-31 00:00:00' AND '2015-03-31 23:59:59'