When should database indexes be rebuilt?

半城伤御伤魂 提交于 2019-11-28 18:40:51

There is no need to 'rebuild' an index. They are always kept up-to-date. Maybe he was referring to rebuilding the table. Depending on your usage patterns and schema, you can get fragmented pages in InnoDB, and I think in MyISAM also. Rebuilding the table can improve performance by getting rid of fragmentation of your data on disk. I don't use MyISAM tables regularly, but I believe it is recommended to run 'OPTIMIZE TABLE' with certain usage patterns. See the MySQL docs on OPTIMIZE TABLE for some good info on both MyISAM and InnoDB.

I'm not as familiar with MyISAM intricacies, but with InnoDB it is true that statistics can get out of date. The database keeps estimated statistics on how your data is distributed for a given index, and it's possible for those to get out-of-date, but MySQL/InnoDB has some built in functionality to try to keep statistics current. You usually don't have to worry about it.

So if you are using InnoDB, the answer is no, you typically don't need to actively do anything to keep your indexes performing well. I'm not as sure with MyISAM, I think it's more common to need to optimize those tables regularly.

It's usually a good idea to set up a cronjob to optimize indexes and check for errors.

See mysqlcheck. A typical cron job looks something like mysqlcheck -Aaos , which checks all tables in all databases for errors, optimizes indexes, and only outputs on error.

The answer you linked to about "regular maintenance" was in the specific context of a temporary table that gets truncated and repopulated regularly. You don't need to do this to the vast majority of MySQL database installs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!