简体   繁体   中英

optimize and check table mysql

I have a database used for statistics, with over 2000 tables, each with ~100 millions rows.

I think a few times a week it'd be nice to check the tables to make sure they are healthy, and if not, repair and optimize them.

60% of the tables are updated everyday, and the rest are archives.

My question is about check(repair)/optimize : Is it good to check table health and optimize them a few times a week to make sure the system runs smoothly?

Generally you need to OPTIMIZE table when its datafile is too much fragmented on disk (use tools for your filesystem to check - the script linked by GiantRobot is not calculating fragmentation), when there were many rows updated and changed their size (that would create row fragmentation) or after deleting many records when you won't be adding them again soon. Because free space is used by MySQL for new rows, when the deleted and new records have the same row size, no OPTIMIZE is needed.

CHECK TABLE is used only when you suspect data corruption (which should not occur during normal run). Some linux distributions (Debian for example) have startup scripts which run CHECK TABLE for all tables on MySQL server startup. REPAIR TABLE is then used to repair the corrupted tables.

ANALYZE TABLE can be used to update index cardinality, which is used in determining the query execution plan. Usually it is needed only in special cases.

It is not clear from your question how are your statistics tables used... how many writes, deletes and reads? My statistic tables have writes all the time, and once a day it is read, data consolidated and written to other table, then deleted. In such case, there is no need to run OPTIMIZE as data are not read often and free space is reused for new data. I am using partitioning by day, so instead of deleting the records (which is quite slow) I just DROP PARTITION (which takes 1 sec max.)

2000 tables and 100 millions rows each looks huge! if you try to optimize each table few times a week, during the optimization, the tables will be locked so you wont be able to write those tables until you finish the work here is a script that checks only fragmented tables (%60 of your tables probably) and optimize only the fragmented ones, let us know if it works.

Note: I couldn't test the script since i use windows machine for my current project. Read the comments and notes below the script for the link i provided

Optimize only fragmented tables in MySQL

Repair is very slow with large tables; you should probably avoid very large tables, especially with lots of indexes, with MyISAM. Prefer to partition them instead to reduce the repair times. This may require a major change to your application code.

You can also see problems with repair here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM