MySql更新优化策略

2020-07-10 08:13:13易采站长站整理

| table       | type | possible_keys | key  | key_len | ref  | rows   | Extra       |
+————-+——+—————+——+———+——+——–+————-+
| funkSpeed   | ALL  | NULL          | NULL | NULL    | NULL | 324746 | Using where |
+————-+——+—————+——+———+——+——–+————-+
1 row in set (0.00 sec)

然后使用联合索引:


mysql> select uin,id from funkSpeed where uin=10023 and id=162;
+————+———-+
| uin        |   id     |
+————+———-+
| 10023      | 162      |
+————+———-+
1 row in set (0.00 sec)
mysql> explain select uin,id from funkSpeed where uin=10023 and id=162;
+————-+——+—————+———-+———+————-+——+————-+
| table       | type | possible_keys | key      | key_len | ref         | rows | Extra       |
+————-+——+—————+———-+———+————-+——+————-+
| funkSpeed   | ref  | uin_id        | uin_id   | 12      | const,const |    4 | Using index |
+————-+——+—————+———-+———+————-+——+————-+
1 row in set (0.00 sec)

可以看到几乎是秒查,这个时候基本可以断定问题是出现在索引这个地方了

我select的时候次数比较少,每两个select之间id相差10000,所以这里可以忽略掉,而且这里没办法优化,除非在id上面添加索引。

问题发生在


update fuckSpeed set type=[type],typeid=[typeid] where id=[id]

这里在更新的时候也是会用到查询的,我的mysql版本是5.5,不能explain update,不然肯定可以验证我所说的,这里要更新32w+条数据,每条数据都会去更新,每条数据0.2s左右,这太吓人了~~

相关文章 大家在看