mysql select 的时间与实际线路不同
mysql time for select not same as real lines
我在 mysql 服务器中遇到了意外结果。
行数多,查询时间少??
我有一个 table 每个过滤器的总行数:
select count(*) from tcr where eid=648;
+----------+
| count(*) |
+----------+
| 11336 |
select count(*) from tcr where eid=997;
+----------+
| count(*) |
+----------+
| 1262307 |
但查询时间与每个过滤器的总行数相反:
select * from tcr where eid=648 order by start_time desc limit 0,10;
[data display]
10 rows in set (16.92 sec)
select * from tcr where eid=997 order by start_time desc limit 0,10;
[data display]
10 rows in set (0.21 sec)
"reset query cache" 已在每个查询 sql 之前执行。
table tcr 的索引是
KEY `cridx_eid` (`eid`),
KEY `cridx_start_time` (`start_time`)
顺便说一句:附上解释结果:这很奇怪,但它看起来更像我们得到的结果。(eid=997 的行数比 eid=648
explain select * from talk_call_record where eid=648 order by start_time desc limit 0,10;
+----+-------------+------------------+-------+---------------+------------------+---------+------+------+-------------+
|编号 | select_type | table |类型 | possible_keys |钥匙 | key_len |参考 |行 |额外 |
+----+------------+----------------+--------+-- ----------+----------------+--------+------+- -----+--------------+
| 1 |简单 | talk_call_record |索引 | cridx_eid | cridx_start_time | 5 |空 | 3672 |使用哪里 |
explain select * from talk_call_record where eid=997 order by start_time desc limit 0,10;
+----+-------------+------------------+-------+---------------+------------------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------------+-------+---------------+------------------+---------+------+------+-------------+
| 1 | SIMPLE | talk_call_record | index | cridx_eid | cridx_start_time | 5 | NULL | 32 | Using where |
首先,你必须有一个非常大的table。
MySQL 正在使用 start_time
上的索引进行查询。发生的事情是 "walking" 到 table,一次一行。它恰好找到 eid=997
比找到 eid=648
快得多。它只需要找到 10 条记录,所以引擎在找到第 10 条时停止。
你能做什么?查询的最佳索引是 (eid, start_time)
上的复合索引。这将直接转到您想要的值。
我在 mysql 服务器中遇到了意外结果。
行数多,查询时间少??
我有一个 table 每个过滤器的总行数:
select count(*) from tcr where eid=648;
+----------+
| count(*) |
+----------+
| 11336 |
select count(*) from tcr where eid=997;
+----------+
| count(*) |
+----------+
| 1262307 |
但查询时间与每个过滤器的总行数相反:
select * from tcr where eid=648 order by start_time desc limit 0,10;
[data display]
10 rows in set (16.92 sec)
select * from tcr where eid=997 order by start_time desc limit 0,10;
[data display]
10 rows in set (0.21 sec)
"reset query cache" 已在每个查询 sql 之前执行。 table tcr 的索引是
KEY `cridx_eid` (`eid`),
KEY `cridx_start_time` (`start_time`)
顺便说一句:附上解释结果:这很奇怪,但它看起来更像我们得到的结果。(eid=997 的行数比 eid=648
explain select * from talk_call_record where eid=648 order by start_time desc limit 0,10;
+----+-------------+------------------+-------+---------------+------------------+---------+------+------+-------------+
|编号 | select_type | table |类型 | possible_keys |钥匙 | key_len |参考 |行 |额外 | +----+------------+----------------+--------+-- ----------+----------------+--------+------+- -----+--------------+ | 1 |简单 | talk_call_record |索引 | cridx_eid | cridx_start_time | 5 |空 | 3672 |使用哪里 |
explain select * from talk_call_record where eid=997 order by start_time desc limit 0,10;
+----+-------------+------------------+-------+---------------+------------------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------------+-------+---------------+------------------+---------+------+------+-------------+
| 1 | SIMPLE | talk_call_record | index | cridx_eid | cridx_start_time | 5 | NULL | 32 | Using where |
首先,你必须有一个非常大的table。
MySQL 正在使用 start_time
上的索引进行查询。发生的事情是 "walking" 到 table,一次一行。它恰好找到 eid=997
比找到 eid=648
快得多。它只需要找到 10 条记录,所以引擎在找到第 10 条时停止。
你能做什么?查询的最佳索引是 (eid, start_time)
上的复合索引。这将直接转到您想要的值。