如何优化这个没有重复关键字和ips的mysql查询?

时间:2015-03-31 07:25:52

标签: mysql

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';

如何优化此查询?

1 个答案:

答案 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'