我想使用一个非阻塞优化表(我已经尝试了正常的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()这看起来是一个很好的方法吗,还是我缺少了/可以改进的东西?
问题:如何在标记中指定代码语言?
发布于 2020-01-18 06:41:00
有缺点。
它实际上是在做什么--复制表并重建索引。您正在优化的两次OPTIMIZE.
CREATE .. SELECT,然后在优化期间再次使用OPTIMIZE写入--将丢失。
RENAME语句中执行所有RENAMEs操作,以避免出现表不存在的短暂时间:RENAME TABLE real TO old, new TO real;所有这些都说,您的优化脚本不能像内置脚本那样好。
pt-online-schema-change,但不做实际的模式更改,是一个可以做得更好的免费工具。
总之,你为什么要使用OPTIMIZE?它几乎从来没有足够的用处来保证这样做。它仅仅是重建一个InnoDB FULLTEXT索引吗?请记住,OPTIMIZE复制表并重新构建所有索引。
我认为,MySQL 8.0有一个“就地”优化。升级到这个。
同时,考虑停止对one DROP INDEX索引的读写、ADD INDEX和FULLTEXT。
https://stackoverflow.com/questions/59796557
复制相似问题