在數據庫數據很是龐大的時候,並且實時有新的數據插入,若是咱們不更新索引,新的數據就search不到,所有從新創建索引又很消耗資源,在這種狀況下咱們就須要使用「主索引+增量索引」的思路來實現實時更新的功能。 mysql
由於這時咱們有了主索引和增量索引,主索引只需在天天凌晨更新,而增量索引的更新頻率設置的很短,這樣用戶在搜索的時候,能夠同時在這兩個索引裏查找。sql
首先建立一個計數器:shell
1.先在mysql中插入一個計數表 數據庫
CREATE TABLE sph_counter(工具
counter_id INTEGER PRIMARY KEY NOT NULL,spa
max_doc_id INTEGER NOT NULL繼承
);索引
2.再次修改配置文件,在主數據源裏面咱們要改預查詢語句:資源
Vi /usr/local/coreseek/etc/csft.confit
source main{
sql_query_pre = SET NAMES utf8
sql_query_pre = SET SESSION query_cache_type=OFF
sql_query_pre = REPLACE INTO sph_counter SELECT 1, MAX(id) FROM documents
sql_query = SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content FROM documents /
WHERE id<=( SELECT max_doc_id FROM sph_counter WHERE counter_id=1 )
… //其餘能夠默認
}
source delta : main //繼承數據源
{
sql_query_pre = SET NAMES utf8
sql_query_pre = SET SESSION query_cache_type=OFF
sql_query_pre =
sql_query = SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content FROM documents /
WHERE id>( SELECT max_doc_id FROM sph_counter WHERE counter_id=1 )
}
index mian //主索引
{
source = main
Path=/usr/local/coreseek/var/data/main
}
index delta : main //增量索引
{
source = delta
Path=/usr/local/coreseek/var/data/delta
}
3.重建增量索引
/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft.conf delta
用/usr/local/coreseek/bin/search工具來檢索,查詢主索引中檢索結果爲0,而新數據庫在增量索引中檢索到。
4.實時更新索引
建2個shell腳本,一個主索引腳本、一個增量索引腳本
Vi main.sh
#!/bin/sh
/usr/local/coreseek/bin/indexer main –c /usr/local/coreseek/etc/csft.conf >> /usr/local/coreseek/var/log/main.log
Vi delta.sh
#!/bin/sh
/usr/local/coreseek/bin/indexer delta –c /usr/local/coreseek/etc/csft.conf –rotate>> /usr/local/coreseek/var/log/delta.log
加入到計劃任務裏面:每5分鐘從新增量索引;天天凌晨2點重建主索引
*/5 * * * * /usr/local/coreseek/etc/delta.sh > /dev/null 2>&1
0 2 * * * /usr/local/coreseek/etc/main.sh > /dev/null 2>&1