Search the MySQL manual:
MySQL Manual

Buy this Reference Manual in softcover from Barnes & Noble!

/ / Up / Table of Contents

4.5.1 Синтаксис команды OPTIMIZE TABLE

OPTIMIZE TABLE tbl_name[,tbl_name]...

Команда OPTIMIZE TABLE должна использоваться после удаления большей части таблицы или если в таблице было внесено много изменений в строки переменной длины (таблицы, в которых есть столбцы VARCHAR, BLOB или TEXT). Удаленные записи поддерживаются при помощи связного списка, и последующие операции INSERT повторно используют позиции старых записей. Чтобы перераспределить неиспользуемое пространство и дефрагментировать файл данных, можно воспользоваться командой OPTIMIZE TABLE.

На данный момент команда OPTIMIZE TABLE работает только с таблицами MyISAM и BDB. Для таблиц BDB команда OPTIMIZE TABLE выполняет ANALYZE TABLE. См. раздел See section 4.5.2 Синтаксис команды ANALYZE TABLE.

Можно применить OPTIMIZE TABLE к таблицам других типов, запустив mysqld с параметром --skip-new или --safe-mode, но в этом случае OPTIMIZE TABLE лишь только выполняет ALTER TABLE.

Команда OPTIMIZE TABLE работает следующим образом:

Команда OPTIMIZE TABLE для MyISAM представляет собой эквивалент выполнения myisamchk --quick --check-only-changed --sort-index --analyze над таблицей.

Обратите внимание: во время работы OPTIMIZE TABLE таблица заблокирована!

User Comments

Posted by on Wednesday January 15 2003, @9:26pm [Delete] [Edit]

There have been a series of reports from users indicating that there are problems running OPTIMIZE TABLE on large tables.

The problems reported range from corrupted indexes to corrupted tables.

Be cautious before running OPTIMIZE TABLE - ensure that you have current backups for your table. Some failures with OPTIMIZE TABLE may be repairable using REPAIR TABLE. Other failures are fatal to the table.

The MySQL team is currently investigating the problem. An official statement will be added to the documentation shortly.

Zak Greant
MySQL AB Community Advocate

Posted by [name withheld] on Saturday March 8 2003, @3:20pm [Delete] [Edit]

What exactly is defined as a large table? I think I might be bitten by this bug but I'm not entirely sure. I spent quite a bit of time optimizing a query with a number of inner joins, and 2 outer joins. I have it down so almost all tables is only search one row, with the exception of one outer join that has to search 4 rows, and then the main table that searches most all the records. Query runs great, average of about 0.53 seconds to return 1,000 rows joining across 5 tables.

However, I then run OPTIMIZE TABLE on all my tables, and notice that my query goes from 0.53 seconds to over 6 to 7 seconds. I do an explain and see that suddenly MySQL is using different keys than before. So I force index usage so that the query, table order, indexes, everything are exactly the same. However explain shows that two of my joins that once only searched 1 row now are searching 10 and 16 rows? I can't for the life of me figure out why, so I usually rebuild my database from backups and don't run the OPTIMIZE TABLE anymore. It would appear it should be renamed UNOPTIMIZE TABLE :)

So what is defined as large table? The largest table in that query holds 14,000+ rows.

Add your own comment.

Top / / / Up / Table of Contents