search
HomeDatabaseMysql Tutorial在MySQL中使用Sphinx实现多线程搜索的方法_MySQL

 MySQL、Sphinx及许多数据库和搜索引擎中的查询是单线程的。比如说,在一台32个CPU核心、16个磁盘的R910服务器上执行一个查询,它最多只会用到一个核心和一个磁盘。没错,只会使用一个。

如果查询是CPU密集型作业,那么会使用大约3%的整机CPU能力(以上述32核机器为例)。如果是磁盘密集型,则大约会使用6%的整机IO能力(也是与上例同样的配置,16个磁盘组成RAID10或RAID0)。

我再换个说法吧。如果你在一台单核单磁盘的机器上执行了某个查询,花了10秒,那么把同样的查询放到一台32核16磁盘的机器上去跑,同样需要10秒,不会有丝毫改善。

你早就知道这一点了,对吧?那么,我的问题是——有没有办法可以改善呢?

如果是Sphinx,太棒了,答案是有!而且不需要花上太多的工夫。你甚至不需要修改应用和数据库,只需要稍微改下Sphinx的配置。

计划

首先,我来说明一下我们的目标。

Sphinx本身就支持分布式搜索,在很久以前就已经朝着水平扩展的目标来设计。如果索引在一台机器上放不下,可以让多台机器分别对不同的部分进行索引,设置一个聚合节点,负责从应用接收请求,然后把请求再同时发给所有的数据节点,最后将它们返回的结果合并起来,返回给应用。在应用看起来,就好像只有一台服务器在为它服务。


好,下面你猜怎么着?哈,我们可以把这个功能应用到单台机器上,让我们的查询快上n多倍。而且,现在Sphinx已经支持这种做法了,所以我们根本不用再假装查询哪些远程节点。

还有另外一个好处,配置分布式搜索以后,索引是可以并行建的!

还是有一点需要注意,虽然这种做法可以加速绝大多数的查询,但还是有一些例外的情况。因为,并行的查询结果仍然需要合并起来,而这个合并过程是单线程的。而且,合并包括一些CPU密集的操作,如分级、排序,甚至用GROUP BY进行COUNT,如果数据量很大,合并过程就会变成瓶颈。

要确认这一点也很简单,只要查看Sphinx的查询日志,看看每个查询匹配的记录数有多少,我们就心里有数了。

执行

假设在服务器上一个索引配置如下 (很多细节都省略了):
 

代码如下:


source src1
{
    type = mysql
    sql_query = SELECT id, text FROM table
}
 
index idx1
{
    type = plain
    source = src1
}
 
searchd
{
    dist_threads = 0 # default
}


现在我们使用有3个CPU核心和磁盘的机器来做这个索引--就是这个idx1.下面是我们更改的配置文件 :

 

代码如下:


source src1
{
    type = mysql
    sql_query = SELECT id, text FROM table
}
 
source src1p0 : src1
{
    sql_query = SELECT id, text FROM table WHERE id % 3 = 0;
}
 
source src1p1 : src1
{
    sql_query = SELECT id, text FROM table WHERE id % 3 = 1;
}
 
source src1p2 : src1
{
    sql_query = SELECT id, text FROM table WHERE id % 3 = 2;
}
 
index idx1_template
{
    type = plain
    source = src1
}
 
index idx1p0 : idx1_template
{
    source = src0
}
 
index idx1p1 : idx1_template
{
    source = src1
}
 
index idx1p2 : idx1_template
{
    source = src2
}
 
index idx1
{
    type = distributed
    local = idx1p0
    local = idx1p1
    local = idx1p2
}
 
searchd
{
    dist_threads = 3
}

做完这些后,你需要重建索引. 但是现在idx1p0到idx1p2的索引indexer命令可以同步进行.

另外,用不同的操作来分离数据不是最好的办法, 你可以在MYSQL中用一个辅助表来区分它们的范围, 配合 sql_query_range使用或是别的什么, 具体根据你的数据来决定.

写在最后

我一直都很喜欢 Sphinx,Sphinx可以如此容易的扩展到你所需要的足够多的机器上,并且这种方式在很多年前就已经在被使用了。然后,我想,我并没有和我往常一样,利用这个特性来使得在一台机器上的查询变得更快。嗯,这并不是在说它很慢或者其实什么,只是,查询永远不会太快,不是吗?

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQL: String Data Types and ENUMs?MySQL: String Data Types and ENUMs?May 13, 2025 am 12:05 AM

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

MySQL BLOB: how to optimize BLOBs requestsMySQL BLOB: how to optimize BLOBs requestsMay 13, 2025 am 12:03 AM

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),