思考 Sphinx 索引性能
Thinking Sphinx indexing performance
我有一个大型索引定义,需要很长时间才能编制索引。我怀疑主要问题是由生成的许多 LEFT OUTER JOIN 引起的。
我看到了 this question,但找不到有关使用 source: :query
的文档,这似乎是解决方案的一部分。
我的索引定义和生成的查询可以在这里找到:https://gist.github.com/jonsgold/fdd7660bf8bc98897612
如何在索引期间优化生成的查询以运行更快?
'standard' sphinx 解决方案是使用远程查询。
http://sphinxsearch.com/docs/current.html#ex-ranged-queries
...将查询分成许多小部分,因此数据库服务器更有可能 运行 查询(而不是一个巨大的查询)
但我不知道如何在 Thinking Sphinx 中实际启用它。在文档中看不到任何内容。可以帮你编辑sphinx.conf,但也不知道TS怎么应付你手动编辑config文件。
这是效果最好的解决方案(来自 linked question)。基本上,您可以删除一段主查询 sql_query
并将其单独定义为 sphinx.conf
文件中的 sql_joined_field
。
重要的是向每个 sql_joined_field
添加所有相关的 sql 条件(例如按 ID 取模分片索引)。这是新的定义:
ThinkingSphinx::Index.define(
:incident,
with: :active_record,
delta?: false,
delta_processor: ThinkingSphinx::Deltas.processor_for(ThinkingSphinx::Deltas::ResqueDelta)
) do
indexes "SELECT incidents.id * 51 + 7 AS id, sites.name AS site FROM incidents LEFT OUTER JOIN sites ON sites.id = site_id WHERE incidents.deleted = 0 AND EXISTS (SELECT id FROM accounts WHERE accounts.status = 'enabled' AND incidents.account_id = id) ORDER BY id", as: :site, source: :query
...
has
...
end
ThinkingSphinx::Index.define(
:incident,
with: :active_record,
delta?: true,
delta_processor: ThinkingSphinx::Deltas.processor_for(ThinkingSphinx::Deltas::ResqueDelta)
) do
indexes "SELECT incidents.id * 51 + 7 AS id, sites.name AS site FROM incidents LEFT OUTER JOIN sites ON sites.id = site_id WHERE incidents.deleted = 0 AND incidents.delta = 1 AND EXISTS (SELECT id FROM accounts WHERE accounts.status = 'enabled' AND incidents.account_id = id) ORDER BY id", as: :site, source: :query
...
has
...
end
将字段 site
定义为单独查询的神奇之处在于行尾的选项 source: :query
。
注意核心索引定义有参数delta?: false
,而增量索引定义有参数delta?: true
。这样我就可以在增量索引中使用条件 WHERE incidents.delta = 1
并过滤掉不相关的记录。
我发现分片并没有更好的表现,所以我恢复到一个统一的索引。
在此处查看整个索引定义:https://gist.github.com/jonsgold/05e2aea640320ee9d8b2。
重要的要记住!
必须手动处理 Sphinx 文档 ID 偏移量。也就是说,无论何时添加或删除另一个模型的索引,我计算的文档 ID 都会发生变化。这必须更新。
所以,在我的示例中,如果我为不同的模型添加索引(不是 :incident
),我将不得不 运行 rake ts:configure
来找出我的新偏移量和相应地更改 incidents.id * 51 + 7
。
我有一个大型索引定义,需要很长时间才能编制索引。我怀疑主要问题是由生成的许多 LEFT OUTER JOIN 引起的。
我看到了 this question,但找不到有关使用 source: :query
的文档,这似乎是解决方案的一部分。
我的索引定义和生成的查询可以在这里找到:https://gist.github.com/jonsgold/fdd7660bf8bc98897612
如何在索引期间优化生成的查询以运行更快?
'standard' sphinx 解决方案是使用远程查询。
http://sphinxsearch.com/docs/current.html#ex-ranged-queries
...将查询分成许多小部分,因此数据库服务器更有可能 运行 查询(而不是一个巨大的查询)
但我不知道如何在 Thinking Sphinx 中实际启用它。在文档中看不到任何内容。可以帮你编辑sphinx.conf,但也不知道TS怎么应付你手动编辑config文件。
这是效果最好的解决方案(来自 linked question)。基本上,您可以删除一段主查询 sql_query
并将其单独定义为 sphinx.conf
文件中的 sql_joined_field
。
重要的是向每个 sql_joined_field
添加所有相关的 sql 条件(例如按 ID 取模分片索引)。这是新的定义:
ThinkingSphinx::Index.define(
:incident,
with: :active_record,
delta?: false,
delta_processor: ThinkingSphinx::Deltas.processor_for(ThinkingSphinx::Deltas::ResqueDelta)
) do
indexes "SELECT incidents.id * 51 + 7 AS id, sites.name AS site FROM incidents LEFT OUTER JOIN sites ON sites.id = site_id WHERE incidents.deleted = 0 AND EXISTS (SELECT id FROM accounts WHERE accounts.status = 'enabled' AND incidents.account_id = id) ORDER BY id", as: :site, source: :query
...
has
...
end
ThinkingSphinx::Index.define(
:incident,
with: :active_record,
delta?: true,
delta_processor: ThinkingSphinx::Deltas.processor_for(ThinkingSphinx::Deltas::ResqueDelta)
) do
indexes "SELECT incidents.id * 51 + 7 AS id, sites.name AS site FROM incidents LEFT OUTER JOIN sites ON sites.id = site_id WHERE incidents.deleted = 0 AND incidents.delta = 1 AND EXISTS (SELECT id FROM accounts WHERE accounts.status = 'enabled' AND incidents.account_id = id) ORDER BY id", as: :site, source: :query
...
has
...
end
将字段 site
定义为单独查询的神奇之处在于行尾的选项 source: :query
。
注意核心索引定义有参数delta?: false
,而增量索引定义有参数delta?: true
。这样我就可以在增量索引中使用条件 WHERE incidents.delta = 1
并过滤掉不相关的记录。
我发现分片并没有更好的表现,所以我恢复到一个统一的索引。
在此处查看整个索引定义:https://gist.github.com/jonsgold/05e2aea640320ee9d8b2。
重要的要记住!
必须手动处理 Sphinx 文档 ID 偏移量。也就是说,无论何时添加或删除另一个模型的索引,我计算的文档 ID 都会发生变化。这必须更新。
所以,在我的示例中,如果我为不同的模型添加索引(不是 :incident
),我将不得不 运行 rake ts:configure
来找出我的新偏移量和相应地更改 incidents.id * 51 + 7
。