トップへ(mam-mam.net/)

MySQLのデータベースを最適化するには

検索:

「MySQLのデータベースを最適化するには」

MySQLのデータベースを最適化したいのですが、
テーブル単位ではなく、データベース単位で最適化したいのですが。

回答

権限のあるユーザーで以下のコマンドを使えばよい。
mysqlcheck -o -u ユーザー名 -pパスワード データベース名


全データベースの最適化を行いたい場合は
-A
オプションも追加します。


でOKです。
また、
--auto-repair 
オプションをついかすれば、自動修復も行ってくれます。

ソース

> mysql -u root -pパスワード
でログイン後、

-- データベース db_test の作成
Create database db_test character set utf8;
-- db_testにユーザ user パスワード password でユーザー作成と権限を与える
Grant all on db_test.* to user@localhost identified by 'password' with grant option;
Flush privileges;
-- データベースにテーブルを作成
Create table db_test.t_bu(bu_id int auto_increment, bu varchar(30), primary key(bu_id));
Create table db_test.t_unit(unit_id int auto_increment, bu_id int, unit varchar(30) , primary key(unit_id));
Create table db_test.t_emp(emp_id int auto_increment, unit_id int, emp varchar(30) , primary key(emp_id));

-- テーブルにデータを挿入
Insert into db_test.t_bu(bu) values('システム部');
Insert into db_test.t_bu(bu) values('人事部');
Insert into db_test.t_unit(bu_id,unit) values(1,'1課');
Insert into db_test.t_unit(bu_id,unit) values(1,'2課');
Insert into db_test.t_unit(bu_id,unit) values(2,'人事課');
Insert into db_test.t_emp(unit_id,emp) values(1,'川崎');
Insert into db_test.t_emp(unit_id,emp) values(1,'川口');
Insert into db_test.t_emp(unit_id,emp) values(2,'石川');
Insert into db_test.t_emp(unit_id,emp) values(3,'大崎');
Insert into db_test.t_emp(unit_id,emp) values(3,'大川');

\q
でmysqlプロンプトから抜けます


> mysqlcheck -o -u user -ppassword --auto-repair db_test
で最適化(テーブルの再作成+解析)を行ってくれます。

db_test.t_bu
note     : Table does not support optimize, doing recreate + analyze instead
status   : OK
db_test.t_emp
note     : Table does not support optimize, doing recreate + analyze instead
status   : OK
db_test.t_unit
note     : Table does not support optimize, doing recreate + analyze instead
status   : OK