http://www.zrwm.com/?p=5393
Sphinx多线程分布式搜索
-
Sphinx分布式索引
单机器的分布式索引(单机上同时查询多个索引):
index sitese01 { type = distributed local = chunk1 local = chunk2 local = chunk3 local = chunk4 }
多机器分布式索引(对应用透明,可以让主节点仅聚合索引):
index sitesindex { type = distributed agent = site01:9312:sitese01 agent = site02:9312:sitese02 ... agent = siteN:9312:siteseN }
-
Sphinx多线程搜索
Sphinx默认是使用单线程(dist_threads=0).通过调整Searchd配置的dist_threads参数,就可以充分利用多核CPU/硬盘的性能,使用多线程并行处理索引或查询,从而极大提高Sphinx的性能.其实最重要的一点就是尽可能将索引分区到每个CPU核,即每CPU核配置一部分索引.
dist_threads设置推荐:
- dist_threads = local索引数(dist_threads<=CPU的核数目)
- 最好不要使”local索引数 / dist_threads”为非整数比例
worker模式设置:
- 推荐设置workers = threads(最高性能,中等风险,比fork快)
- 设置workers = fork(默认设置,高性能,低风险)
- 高负载情况下不推荐设置workers = prefork
以下是一台双核CPU的机器,对索引进行分区的简单例子:
source items_core0 { … sql_query = SELECT … FROM items WHERE id % 2 = 0 … } source items_core1 : items_core0 { sql_query = SELECT … FROM items WHERE id % 2 = 1 } index items_core0 { source = items_core0 path = /usr/local/sphinx/var/data/items_core0 } index items_core1 { source = items_core1 path = /usr/local/sphinx/var/data/items_core1 } index items_core { type = distributed local = items_core0 local = items_core1 } searchd { … # workers = threads dist_threads = 2 # 该值不应该大于CPU的核数 … }
实际生产环境中,取模运算未必是最好的分割数据的方式.根据实际数据,可能需要通过辅助数据表来定义分割区间,同时结合sql_query_range来更好进行索引.