初めてのシステムと日記

システムも日記も初めてです。

groongaストレージエンジンを試してみる

http://dev.mysql.com/doc/refman/5.1/ja/storage-engines.html

MySQLではいくつかのストレージエンジンをサポートしてますが、

今回は全文検索エンジンであるgroongaを継承したgroongaストレージエンジンを試してみます。

この記事では使い方や検証を行っています。ダウンロードやインストールはこちらを参考にしてみてください。


■使い方

http://github.com/mroonga/mroonga/tarball/v0.1

ここに現在利用できるSQLのサンプルが置かれていたのでこれに基本沿っています。

基本的にgroongaの全文検索を利用するには

・tableのengineがgroongaになっている。
・検索対象のカラムにFULLTEXT INDEXがかかっている。

を設定する必要があります。


・create table

mysql> set storage_engine=groonga;
Query OK, 0 rows affected (0.00 sec)

// DEFAULTがgroongaになっていればOK
mysql> show engines;

mysql> create table t1 (c1 int primary key, c2 varchar(255), c3 text, fulltext index(c2), fulltext index(c3));
Query OK, 0 rows affected (0.43 sec)

groonga を storage_engine にセットした上で、通常通り create table をすれば table が出来ます。

その際に検索対象のカラムには fulltext index をかけます。


・select fulltext

mysql> insert into t1 values(1, "明日の富士山の天気について","あああああああ");
Query OK, 1 row affected (0.01 sec)

mysql> insert into t1 values(2, "いいいいい","明日の富士山の天気は分かりません");
Query OK, 1 row affected (0.00 sec)

mysql> insert into t1 values(3, "dummy", "dummy");
Query OK, 1 row affected (0.00 sec)

mysql> select * from t1;
+----+-----------------------------------------+--------------------------------------------------+
| c1 | c2                                      | c3                                               |
+----+-----------------------------------------+--------------------------------------------------+
|  1 | 明日の富士山の天気について              | あああああああ                                   |
|  2 | いいいいい                              | 明日の富士山の天気は分かりません                 |
|  3 | dummy                                   | dummy                                            |
+----+-----------------------------------------+--------------------------------------------------+
3 rows in set (0.00 sec)

mysql> select * from t1 where match(c2) against("富士山");
+----+-----------------------------------------+-----------------------+
| c1 | c2                                      | c3                    |
+----+-----------------------------------------+-----------------------+
|  1 | 明日の富士山の天気について              | あああああああ        |
+----+-----------------------------------------+-----------------------+
1 row in set (0.00 sec)

match([column_name]) against([search_word]) 形式で検索すれば全文検索が可能です。

MySQL の FULLTEXT INDEX であった以下の制限もgroongaではないです。

・全レコードの50%以上が引っかかる検索語は除外される

・検索語は4文字以上。



■検証

使い方が分かったところで速度とか懸念事項がないか検証していきます。


・速度

storage engine が InnoDB のと groonga の table を作って10万件テストデータ入れて検証してみました。

mysql> select * from test_innodb;
103488 rows in set (9.07 sec)

mysql> select * from test_groonga;
103488 rows in set (3.05 sec)

mysql> flush tables;
Query OK, 0 rows affected (0.00 sec)

mysql> select count(*) from test_innodb;
+----------+
| count(*) |
+----------+
|   103488 |
+----------+
1 row in set (1.93 sec)

mysql> select count(*) from test_groonga;
+----------+
| count(*) |
+----------+
|   103488 |
+----------+
1 row in set (2.15 sec)

mysql> flush tables;
Query OK, 0 rows affected (0.00 sec)

mysql> select count(*) from test_innodb limit 5000;
+----------+
| count(*) |
+----------+
|   103488 |
+----------+
1 row in set (5.06 sec)

mysql> select count(*) from test_groonga limit 5000;
+----------+
| count(*) |
+----------+
|   103488 |
+----------+
1 row in set (2.16 sec)

mysql> flush tables;
Query OK, 0 rows affected (0.00 sec)

mysql> select count(*) from test_innodb where RECIPE_NAME like '%ケーキ%';
+----------+
| count(*) |
+----------+
|     4704 |
+----------+
1 row in set (1.95 sec)

mysql> select count(*) from test_groonga where match(RECIPE_NAME) against('ケーキ');
+----------+
| count(*) |
+----------+
|     4704 |
+----------+
1 row in set (0.02 sec)

全文検索やlimit句などを用いた場合、groongaの方が圧倒的に早いことが分かりました。


・懸念点

AUTO_INCREMENTに対応していない
→INSERT時のIDなどでチェックが必要


まだver0.2ということなのでこれからも定期的にチェックしてみたいと思います。