【发布时间】:2020-01-18 00:44:35
【问题描述】:
我想使用一个非阻塞优化表(我已经尝试过正常的OPTIMIZE 并且我注意到大约 2 分钟的停机时间。这是我目前拥有的代码:
@staticmethod
def optimize_table_nonblocking(tablename='eidrdedupe_test'):
conn, cursor = get_new_db_conn_and_cursor()
# populate a (potentially previously-existing) table with the data
cursor.execute('DROP TABLE IF EXISTS %s_tmp' % tablename)
cursor.execute("CREATE TABLE %s_tmp SELECT * FROM %s" % (tablename, tablename))
# optimize that table
cursor.execute("OPTIMIZE TABLE %s_tmp" % tablename)
conn.commit()
# swap the tables // keep the old table so we can examine it if we want
cursor.execute('RENAME TABLE %s TO %s_tmp1' % (tablename, tablename)) # rename current table to _tmp1
cursor.execute('RENAME TABLE %s_tmp TO %s' % (tablename, tablename)) # rename tmp table to main table
cursor.execute('RENAME TABLE %s_tmp1 TO %s_tmp' % (tablename, tablename)) # now previous table is _tmp
conn.commit()
cursor.close(); conn.close()
这似乎是一个好方法,还是我缺少/可以改进的地方?
[问题:如何在标记中指定代码语言?]
【问题讨论】:
-
如果表有并发更新,这些将会丢失(你需要在之后以某种方式重新应用它们)。如果表没有并发更新,那么直接在表上正常优化真的有问题吗?
-
@Thilo -- 是的,我宁愿在运行时重新运行更新(我们存储了一个日志,这样就不会成为问题)。
-
您使用的是什么存储引擎?文档声称 InnoDB 的优化是在线完成的。
-
InnoDB -- 它是重建一个 fts 索引,除非我在对搜索相关的内容进行重大更新时运行它,否则它会严重退化。
标签: mysql sql nonblocking