简单的 sql 查询需要 16 秒以上。这是表格。
CREATE TABLE IF NOT EXISTS `udr` (
`userid` int(11) NOT NULL DEFAULT '0',
`time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`upbytes` int(11) NOT NULL DEFAULT '0',
`downbytes` int(11) NOT NULL DEFAULT '0',
`traffictype` int(11) NOT NULL DEFAULT '1',
KEY `userid` (`userid`),
KEY `time` (`time`),
KEY `traffictype` (`traffictype`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
有 1600 万条记录。 (本来有1.41亿,剪枝看是不是问题)
有问题的查询是(like)
select time,upbytes,downbytes
from udr
where userid = 315533 and
time between '2014-01-01 14:35:28' and '2014-01-02 14:35:28'
我认为问题出在时间部分,所以我删除了时间条件并尝试以下操作
select time,upbytes,downbytes from udr where userid = 315533
...
10282 rows in set (19.42 sec)
查询仍然需要 16 秒以上。
这是我的关键配置参数
key_buffer = 32M
max_allowed_packet = 16M
thread_stack = 192K
thread_cache_size = 8
query_cache_limit = 1M
query_cache_size = 16M
关于这个的想法用完了。
谢谢
mysql> explain select * from udr where userid = '315533';
+----+-------------+-------+------+---------------+--------+---------+-------+-------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+--------+---------+-------+-------+-------+
| 1 | SIMPLE | udr | ref | userid | userid | 4 | const | 12738 | |
+----+-------------+-------+------+---------------+--------+---------+-------+-------+-------+
1 row in set (0.02 sec)
iotop 报告大量磁盘 io。我怀疑 mysql 正在将所有记录检索到内存中。
最佳答案
SELECT time,upbytes,downbytes
FROM udr
WHERE userid = 315533 AND
time BETWEEN '2014-01-01 14:35:28' AND '2014-01-02 14:35:28'
在此查询中,您正在搜索两列,time
和 userid
。您确实在这两列上都有索引,但 MySQL 一次只能使用其中一个。
MySQL 将选择使用time
索引或userid
索引。如果选择userid
,则需要检索12738行,导致查询时间过长。然后 MySQL 将搜索检索到的数据以过滤掉 time
列。
解决方案是在两列上添加一个索引:
ALTER TABLE udr ADD KEY (`userid`, `time`);
这样 MySQL 就可以同时搜索 userid
和 time
而无需先检索数据。
注意:创建新索引可能需要几分钟时间,请耐心等待。
关于mysql - 简单的 mysql 查询需要 16 秒以上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20879957/