MyNETSの日記検索高速化(Tritonn導入)2009年6月にUsagi-Projectに寄稿したもの。

日記データが膨大になってきて日記検索のI/Oがサーバ全体の足を引っ張るようになってきたので、検索の高速化をしてみました。
方法はmysqlを日本語全文検索対応のTritonnプロジェクト版に変更し、それに合わせて検索部分を修正します。

手元の日記データ37万件400Mくらいのc_diaryテーブルをPentiumM1.6GHzのマシンで検索したところ(検索クエリは後述)検索速度が15倍以上になりました。

 LIKE検索 平均5.3秒
 Tritonn検索 平均0.3秒

以下手順。

1)Tritonnインストール(CentOS5サーバの場合

まずは既存のDBを念のためバックアップ

  1. # mysqldump -u root -p mynets_db > dump.sql

mysqlを置き換えるためmysqlのインストールを確認

  1. # rpm -qa | grep -i mysql
  2. mysql-5.0.45-7.el5

mysqlをremove
通常dovecotで利用されているので、dovecotを停止してから作業

  1. # service dovecot stop
  2. # yum remove mysql

perl-DBIのインストールを確認

  1. # rpm -qa | grep -i perl-DBI
  2. perl-DBI-1.52-1.fc6

最新のtritonn-1.0.12-mysql-5.0.67パッケージ群をダウンロード

  1. # mkdir download
  2. # cd download/
  3. # wget http://iij.dl.sourceforge.jp/tritonn/36448/MySQL-client-5.0.67-tritonn.1.0.12.i386.rpm
  4. # wget http://iij.dl.sourceforge.jp/tritonn/36448/MySQL-devel-5.0.67-tritonn.1.0.12.i386.rpm
  5. # wget http://iij.dl.sourceforge.jp/tritonn/36448/MySQL-server-5.0.67-tritonn.1.0.12.i386.rpm
  6. # wget http://iij.dl.sourceforge.jp/tritonn/36448/MySQL-shared-5.0.67-tritonn.1.0.12.i386.rpm
  7. # wget http://iij.dl.sourceforge.jp/tritonn/36448/mecab-0.97-tritonn.1.0.12.i386.rpm
  8. # wget http://iij.dl.sourceforge.jp/tritonn/36448/mecab-ipadic-2.7.0.20070801-tritonn.1.0.12.i386.rpm
  9. # wget http://iij.dl.sourceforge.jp/tritonn/36448/senna-1.1.4-tritonn.1.0.12.i386.rpm

以下の順番でインストール

  1. # rpm -ivh mecab-0.97-tritonn.1.0.12.i386.rpm
  2. # rpm -ivh mecab-ipadic-2.7.0.20070801-tritonn.1.0.12.i386.rpm
  3. # rpm -ivh senna-1.1.4-tritonn.1.0.12.i386.rpm
  4. # rpm -ivh MySQL-shared-5.0.67-tritonn.1.0.12.i386.rpm
  5. # rpm -ivh MySQL-client-5.0.67-tritonn.1.0.12.i386.rpm
  6. # rpm -ivh MySQL-server-5.0.67-tritonn.1.0.12.i386.rpm
  7. # rpm -ivh MySQL-devel-5.0.67-tritonn.1.0.12.i386.rpm

MySQLはインストール後すぐに起動するのでrootのパスを設定

  1. # /usr/bin/mysqladmin -u root password 'rootpassword'

MySQLへのログイン確認

  1. # mysql -u root -p
  2. Enter password:
  3.  
  4. Welcome to the MySQL monitor.  Commands end with ; or \g.
  5. Your MySQL connection id is 7
  6. Server version: 5.0.67-modified MySQL Community Server (GPL) (portions (c) Tritonn Project)
  7.  
  8. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
  9.  
  10. mysql>

これでTritonnのインストール完了

入れ替え前のDBもたぶん大丈夫なはずだが、もし使えない時には新規にDB作成して、バックアップデータをリストアする

  1. # mysql -u root -p
  2. mysql> CREATE DATABASE mynets_db DEFAULT CHARACTER SET utf8;
  3. mysql> quit
  4. # mysql -u root -p mynets_db < dump.sql[/code]
  5.  
  6. <strong>2)Tritonn用のindex作成</strong>
  7. [code]# mysql -u root -p
  8. mysql> use mynets_db;
  9. mysql> ALTER TABLE c_diary ADD FULLTEXT fullindex USING NGRAM,SECTIONALIZE (subject, body);

件数が多いと相当時間がかかるのでじっと待つ
完了したらテスト検索してみる(testで検索)

  1. mysql> SELECT * FROM c_diary WHERE (public_flag = 'public' or public_flag = 'open') AND MATCH (subject, body) AGAINST ("test") ORDER BY r_datetime DESC;

LIKE検索と時間を比較してみる

  1. mysql> SELECT * FROM c_diary WHERE (public_flag = 'public' or public_flag = 'open') AND (subject LIKE '%test%' or body LIKE '%test%') ORDER BY r_datetime DESC;

3)MyNETSの検索部分の修正

webapp/lib/db/read/diary.phpの新規日記検索の項目を以下のように修正

$s

  1. elect = 'SELECT *';
  2. $from = ' FROM ' . MYNETS_PREFIX_NAME . 'c_diary';
  3. $where = " WHERE (public_flag = 'public' or public_flag = 'open')";
  4.  
  5. if ($keyword) {
  6. //全角空白を半角に統一
  7. $keyword = str_replace(' ', ' ', $keyword);
  8. //SQL Injection対策
  9. $keyword = str_replace('\\', '', $keyword);
  10. $keyword = str_replace('"', '', $keyword);
  11. $keyword = str_replace(';', '', $keyword);
  12. $keyword = str_replace(')', '', $keyword);
  13. $keyword = check_search_word($keyword);
  14.  
  15. $where .= ' AND MATCH (subject, body) AGAINST ("' .$keyword. '" IN BOOLEAN MODE)';
  16.  
  17. }
  18. $order = " ORDER BY r_datetime DESC";

, , , ,
とりあえず付けておく無駄ではなかったなまぁまぁ読めたちょっと役に立ったかなかなり良かったかも (まだ評価されていません)
Loading...
Trackback

no comment untill now

Sorry, comments closed.