Innodb 没有主键的表插入性能问题

问题:
这几天发现客户那里的mysql集群有时插入特别的慢,通过慢查询日志以及pt-query-digest得知主要是对一张
表的插入特别的慢。 这张表的数据量大概有3百多万行,插入操作特别的频繁。
通过开启innodb monitor得知, 并行的插入操作都阻塞在一个地方:

--Thread 1284204864 has waited at dict0boot.ic line 45 for 305.00 seconds the semaphore:
Mutex at 0x2ac80a08 created file dict0dict.c line 698, lock var 1

通过查看mysql代码得知,他们都在等待一个全局的字典锁

  36 Returns a new row id.
  37 @return the new id */
  38 UNIV_INLINE
  39 row_id_t
  40 dict_sys_get_new_row_id(void)
  41
  42 {
  43    row_id_t  id;
  44
  45    mutex_enter(&(dict_sys->mutex));  //等待位置
  46
  47    id = dict_sys->row_id;
  48
  49    if (0 == (id % DICT_HDR_ROW_ID_WRITE_MARGIN)) {
  50
  51        dict_hdr_flush_row_id();
  52    }
  53
  54    dict_sys->row_id++;
  55
  56    mutex_exit(&(dict_sys->mutex));
  57
  58    return(id);
  59 }

原因是innodb 使用聚集索引来组织数据, 如果要插入的表没有主键或非空的唯一键的话, innodb就会从全局
为这行分配一个row_id。 由于是全局锁,非常的影响插入性能。

何登成有一篇blog很好地分析了这个问题:
http://www.mysqlops.com/2011/12/05/innodb-without-key.html

解决方案:
提供给客户现场的解决方案是添加一个自增列为主键。
由于是个主从复制集群,添加自增列比较麻烦,如果应用允许的话,最好的办法是把原表drop掉,然后重建这张表,再导入数据。因为通过alter添加自增列的操作是复制不安全的。

Adding an AUTO_INCREMENT column to a table with ALTER TABLE might not produce the same ordering of the rows on the slave and the master. This occurs because the order in which the rows are numbered depends on the specific storage engine used for the table and the order in which the rows were inserted. If it is important to have the same order on the master and slave, the rows must be ordered before assigning an AUTO_INCREMENT number. Assuming that you want to add an AUTO_INCREMENT column to a table t1 that has columns col1 and col2, the following statements produce a new table t2 identical to t1 but with an AUTO_INCREMENT column:

CREATE TABLE t2 LIKE t1;
ALTER TABLE t2 ADD id INT AUTO_INCREMENT PRIMARY KEY;
INSERT INTO t2 SELECT * FROM t1 ORDER BY col1, col2;
Important

To guarantee the same ordering on both master and slave, the ORDER BY clause must name all columns of t1.


转载请注明转自高孝鑫的博客
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值