【Sphinx】MySQL+Sphinx 全文檢索索引的使用和測試

來源:互聯網
上載者:User

標籤:sphinx mysql 累加式更新索引文

    導讀:最近線上上由於某些不可名狀的原因需要在資料庫中過濾一些詞語,所以線上比較多的語句都是 select c1,c2 fron tb1 where c1 like '%name%'類似與這個樣子的模糊查詢。開始想到了全文索引來進行分詞,但是又想到這些都是線上資料庫,並且表中的資料都是幾千萬行的資料,線上online ddl 或者說使用第三方的工具的話對於在修改期間會阻塞線上的DML操作,並且針對全文索引的維護也需要消耗一定的資源,所以暫時打消了這個想法。在和開發進行討論並且結合實際業務,最後還是打算選型第三方的全文檢索索引工具,在這裡選擇了全文檢索索引工具sphinx。

    sphinx的安裝和測試:

  1. sphinx的安裝

yum install sphinx -y


  2. 設定檔

#
# Minimal Sphinx configuration sample (clean, simple, functional)
#

source sbtest_sbtest1
{
type= mysql

sql_host= localhost 
sql_user= root
sql_pass= redhat
sql_db= sbtest
sql_port= 3306# optional, default is 3306

sql_query= \
SELECT userid as  id, \
                k, \
                c, \
                pad \
FROM sbtest1 \
                where c like '%03679578678%' \

sql_attr_uint   = k  #資料類型int或者timestamp
        sql_field_string         = c  #資料類型text或者string
        sql_attr_string         = pad #資料類型string
}

index sbtest1
{
source= sbtest_sbtest1
path= /var/lib/sphinx/sbtest1 #索引檔案的路徑
}

index testrt
{
type= rt
rt_mem_limit= 128M

path= /var/lib/sphinx/testrt

rt_field= title
rt_field= content
rt_attr_uint= gid
}

indexer
{
mem_limit= 128M
}

searchd
{
listen= 9312 
listen= 9306:mysql41    #對外提供服務的連接埠,類似與mysql中的3306連接埠
log= /var/log/sphinx/searchd.log
query_log= /var/log/sphinx/query.log
read_timeout= 5
max_children= 30
pid_file= /var/run/sphinx/searchd.pid
seamless_rotate= 1
preopen_indexes= 1
unlink_old= 1
workers= threads # for RT to work
binlog_path= /var/lib/sphinx/
}

    注意:在設定檔中需要注意的是:

    a. 在sql_query參數後面是sql語句,但是這個sql語句的第一個欄位必須是id欄位,在後面的參數類型定義的時候不需要定義,否則的話sphinx是不能識別這個屬性的。

    b. 在屬性的定義的時候必須包含一個sql_filed_string類型的欄位,否則sphinx認為你沒有必須使用全文檢索索引這個功能。

  3.建立索引

indexer sbtest1

  4.啟動searchd 服務

service searchd start

  5.查看searchd服務狀態

[[email protected] data]# searchd --status
Sphinx 2.3.2-id64-beta (4409612)
Copyright (c) 2001-2016, Andrew Aksyonoff
Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com)

using config file '/etc/sphinx/sphinx.conf'...

searchd status
--------------
uptime: 80403
connections: 11
maxed_out: 0
command_search: 7
command_excerpt: 0
command_update: 0
command_delete: 0
command_keywords: 0
command_persist: 0
command_status: 1
command_flushattrs: 0
agent_connect: 0
agent_retry: 0
queries: 7
dist_queries: 0
query_wall: 0.010
query_cpu: OFF
dist_wall: 0.000
dist_local: 0.000
dist_wait: 0.000
query_reads: OFF
query_readkb: OFF
query_readtime: OFF
avg_query_wall: 0.001
avg_query_cpu: OFF
avg_dist_wall: 0.000
avg_dist_local: 0.000
avg_dist_wait: 0.000
avg_query_reads: OFF
avg_query_readkb: OFF
avg_query_readtime: OFF
qcache_max_bytes: 16777216
qcache_thresh_msec: 3000
qcache_ttl_sec: 60
qcache_cached_queries: 0
qcache_used_bytes: 0
qcache_hits: 0

  6.測試使用:mysql 用戶端

[[email protected] data]# mysql -uroot -predhat -h127.0.0.1 -P9306 -e "select id,k,c,pad from sbtest1 where match('03679578678')"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+----------+-------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------+
| id   | k        | c                                                                                                                       | pad                                                         |
+------+----------+-------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------+
|    1 | 15324329 | 64733237507-56788752464-03679578678-53343296505-31167207241-10603050901-64148678956-82738243332-73912678420-24566188603 | 78326593386-76411244360-77646817949-14319822046-41963083322 |
+------+----------+-------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------+

  7.python測試使用

#!/usr/bin/env python
#coding:utf-8

import pymysql

con = pymysql.connect(host='127.0.0.1', port=9306, user="", passwd="", db="")
with con.cursor(pymysql.cursors.DictCursor) as cur:
    cur.execute("select * from sbtest1 where match('%03679578678%')")
    print(cur.fetchall())
    
[[email protected] ~]# python sphinx.py
[{u'c': '64733237507-56788752464-03679578678-53343296505-31167207241-10603050901-64148678956-82738243332-73912678420-24566188603', u'k': 15324329, u'pad': '78326593386-76411244360-77646817949-14319822046-41963083322', u'id': 1}]

  8.表結構:

[email protected] 18:06:  [sbtest]> show create table sbtest1 \G
*************************** 1. row ***************************
       Table: sbtest1
Create Table: CREATE TABLE `sbtest1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `k` int(11) NOT NULL DEFAULT '0',
  `c` char(120) NOT NULL DEFAULT '',
  `pad` char(60) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  KEY `k_1` (`k`)
) ENGINE=InnoDB AUTO_INCREMENT=25000000 DEFAULT CHARSET=utf8mb4

    測試環境中表sbtest1的資料有2500萬行,測試sql是select * from sbtest1 where c like '%03679578678%'。在MySQL執行這個sql的時候由於使用不了索引只能進行全表掃描,花費了將近4分鐘,但是在sphinx建立索引的時候也花費了70多秒的樣子。在索引建立成功之後基本上面執行python指令碼或者使用mysql用戶端擷取得到的花費基本是在毫秒層級的。效能提升了很多,並且sphinx和MySQL的在建立索引成功之後之間就沒有連結了。並不會影響MySQL的操作,sphinx可以獨立部署到一台伺服器上面。

  9.更新索引檔案:累加式更新索引檔案

indexer --rotate test1

【Sphinx】MySQL+Sphinx 全文檢索索引的使用和測試

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.