关于 mysql 读写速度疑问

2015-04-16 13:55:45 +08:00
 delavior
一张表300多万条记录,一个简单的select count(*)查询,执行时间长达100多秒,正常吗?
同样的表记录数为30多万时,同样的查询语句执行时间不到0.1秒。
4291 次点击
所在节点    MySQL
30 条回复
ALeo
2015-04-16 14:02:12 +08:00
用count(1)
tabris17
2015-04-16 14:08:14 +08:00
没任何where group by?
fising
2015-04-16 14:08:32 +08:00
1. MyISAM
2. USE INDEX
tb4649120073rs
2015-04-16 14:21:32 +08:00
explain一下看看
XadillaX
2015-04-16 14:29:02 +08:00
COUNT(1)
delavior
2015-04-16 14:49:21 +08:00
@ALeo @XadillaX 结果没什么变化,还是100多秒
delavior
2015-04-16 14:49:57 +08:00
@tabris17 没有
delavior
2015-04-16 14:55:18 +08:00
@fising 对数据库不懂,而且现在只允许查询,不允许对表结构和内容作修改.我只是觉得这查询速度不太正常,想确认一下是不是不正常和什么原因
delavior
2015-04-16 14:55:37 +08:00
@tb4649120073rs
mysql> explain history_uint;
+--------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+-------+
| itemid | bigint(20) unsigned | NO | MUL | NULL | |
| clock | int(11) | NO | | 0 | |
| value | bigint(20) unsigned | NO | | 0 | |
| ns | int(11) | NO | | 0 | |
+--------+---------------------+------+-----+---------+-------+
4 rows in set (0.09 sec)
heyli
2015-04-16 15:18:09 +08:00
表引擎是InnoDB?
delavior
2015-04-16 15:19:40 +08:00
@tb4649120073rs mysql> explain select count(*) from history_uint;
+----+-------------+--------------+-------+---------------+----------------+----
-----+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key
_len | ref | rows | Extra |
+----+-------------+--------------+-------+---------------+----------------+----
-----+------+---------+-------------+
| 1 | SIMPLE | history_uint | index | NULL | history_uint_1 | 12
| NULL | 3393731 | Using index |
+----+-------------+--------------+-------+---------------+----------------+----
-----+------+---------+-------------+
1 row in set (0.00 sec)
delavior
2015-04-16 15:20:12 +08:00
@heyli mysql默认不是myisam吗,没改过
wanjun
2015-04-16 15:20:44 +08:00
是不是你的表没有主键
wanjun
2015-04-16 15:22:04 +08:00
默认 InnoDB ,问题应该不是使用什么引擎的问题
huigeer
2015-04-16 15:30:14 +08:00
是不是你的表没有主键 + 1024,
innodb的灵魂就是主键
denghongcai
2015-04-16 15:41:26 +08:00
desc看下
explain看上去没问题,用了索引
tonyzhimin
2015-04-16 15:45:50 +08:00
你这个要先确认是啥存储引擎
delavior
2015-04-16 15:52:50 +08:00
@wanjun 是没有主键,是跟这有关系吗
idblife
2015-04-16 16:25:07 +08:00
很难理解不加主键的行为
或者说绝对不理解
heyli
2015-04-16 16:30:47 +08:00
先确定你的表引擎先 innoDB没记错的话 count是全表扫描的
SHOW CREATE TABLE 看下 ENGINE

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/184129

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX