【问题标题】:mysql time for select not same as real lines选择的mysql时间与实际行不同
【发布时间】:2017-03-13 10:39:38
【问题描述】:

我在我的 mysql 服务器中遇到了意外的结果。

行数多,查询时间少??

我有一个表和每个过滤器的总行数:

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)

“重置查询缓存”在每个查询 sql 之前执行。 表 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;
+----+-------------+------------------+-------+---------------+------------------+---------+------+------+-------------+

|编号 |选择类型 |表|类型 |可能的键 |关键 | key_len |参考 |行 |额外 | +----+-------------+------------------+--------+--- ---------+------+---------+------+- -----+--------------+ | 1 |简单 |通话记录 |索引 | 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 |

【问题讨论】:

    标签: mysql select explain


    【解决方案1】:

    首先,您必须有一张非常大的桌子。

    MySQL 正在使用start_time 上的索引进行查询。正在发生的事情是它“走”过桌子,一次一排。它碰巧找到eid=997 比找到eid=648 快得多。它只需要找到 10 条记录,因此引擎在到达第 10 条时停止。

    你能做什么?查询的最佳索引是(eid, start_time) 上的复合索引。这将直接转到您想要的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-08
      • 2013-02-21
      • 1970-01-01
      • 1970-01-01
      • 2019-11-30
      • 1970-01-01
      相关资源
      最近更新 更多