ITPub博客

首页 > 数据库 > MySQL > MySQL 5.7使用pt-online-schema-change对大表加索引

MySQL 5.7使用pt-online-schema-change对大表加索引

原创 MySQL 作者:ywxj_001 时间:2021-02-01 13:12:58 0 删除 编辑

业务需求:

在线对MySQL 5.7的一个大表加索引。

普通加索引方式,适合于小表:

ALTER TABLE `EMP`

ADD INDEX IDX_EMP_ORDERSYSNO (ORDERSYSNO) USING BTREE;


对大表需要使用pt-online-schema-change工具,可以不锁表。

加组合字段索引:

[root@DB02 ~]# pt-online-schema-change --user=root --password=123456 --host=192.168.1.18  --alter "ADD INDEX IDX_DOSTELLE_ST (SEGMENT,transaction) USING BTREE" D=ora,t=dostelle --no-check-replication-filters  --execute --charset=utf8  --max-load=Threads_running=20


加单一字段索引:

[root@DB001 ~]# pt-online-schema-change --user=root --password=XXX --host=192.168.1.98  --alter "ADD INDEX IDX_ORDERID (ORDERID) USING BTREE" D=ora,t=idemp --no-check-replication-filters  --execute --charset=utf8  --max-load=Threads_running=20

Found 1 slaves:

localhost.localdomain -> 192.168.1.99:socket

Will check slave lag on:

localhost.localdomain -> 192.168.1.99:socket

Operation, tries, wait:

  analyze_table, 10, 1

  copy_rows, 10, 0.25

  create_triggers, 10, 1

  drop_triggers, 10, 1

  swap_tables, 10, 1

  update_foreign_keys, 10, 1

Altering `ora`.`idemp`...

Creating new table...

Created new table ora._idemp_new OK.

Altering new table...

Altered `ora`.`_idemp_new` OK.

2021-02-01T11:33:58 Creating triggers...

2021-02-01T11:34:40 Created triggers OK.

2021-02-01T11:34:40 Copying approximately 6915514 rows...

Copying `ora`.`idemp`:  11% 04:01 remain

Copying `ora`.`idemp`:  20% 03:46 remain

Copying `ora`.`idemp`:  31% 03:18 remain

Copying `ora`.`idemp`:  41% 02:49 remain

Copying `ora`.`idemp`:  50% 02:25 remain

Copying `ora`.`idemp`:  60% 01:59 remain

Copying `ora`.`idemp`:  70% 01:29 remain

Copying `ora`.`idemp`:  80% 00:58 remain

Copying `ora`.`idemp`:  90% 00:29 remain

Copying `ora`.`idemp`:  99% 00:00 remain

2021-02-01T11:50:59 Copied rows OK.

2021-02-01T11:50:59 Analyzing new table...

2021-02-01T11:50:59 Swapping tables...

2021-02-01T11:51:13 Swapped original and new tables OK.

2021-02-01T11:51:13 Dropping old table...

2021-02-01T11:51:14 Dropped old table `ora`.`_idemp_old` OK.

2021-02-01T11:51:14 Dropping triggers...

2021-02-01T11:51:14 Dropped triggers OK.

Successfully altered `ora`.`idemp`.

You have mail in /var/spool/mail/root


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/22996654/viewspace-2755009/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
在零售、金融、互联网行业从事数据库相关工作10余年,有丰富的数据库管理和架构的相关经验。 涉及SQLServer、Oracle、MySQL、PostgreSQL等多种数据库。 目前在一家互联网公司担任DBA经理。 负责整个集团数据库的架构设计和管理工作。

注册时间:2010-01-19

  • 博文量
    247
  • 访问量
    436233