查询太慢了,我该如何优化呢?

时间:2015-03-30 12:41:22

标签: mysql

我有一个包含大量数据的数据库,我的查询优化有问题。 如何优化此查询

explain SELECT distinct UNIX_TIMESTAMP(timestamp)*1000 as timestamp,count(ip_src) as counter from acid_event a,saher.network n where inet_aton(n.net)!=a.ip_src group by timestamp;
+----+-------------+-------+-------+---------------+-------------+---------   +------+---------+----------------------------------------------+
| id | select_type | table | type  | possible_keys | key         | key_len | ref  | rows    | Extra                                        |
+----+-------------+-------+-------+---------------+-------------+---------+------+---------+----------------------------------------------+
|  1 | SIMPLE      | n     | index | NULL          | fsi         | 27      | NULL | 2691027 | Using index; Using temporary; Using filesort |
|  1 | SIMPLE      | a     | index | NULL          | timestamp_2 | 13      | NULL | 6770718 | Using where; Using index; Using join buffer  |
+----+-------------+-------+-------+---------------+-------------+---------+------+---------+----------------------------------------------+

1 个答案:

答案 0 :(得分:0)

  1. 尝试通过以下方式摆脱INET_ATON()acid_event

  2. 中添加一个包含IP地址数值的新列
  3. 然后,如果结果集很大(例如数千),请摆脱UNIX_TIMESTAMP(timestamp)*1000

  4. 如果这还不够,请修复索引。这是一个book,可以深入解释索引。