9

我的 mysql 数据库中有数百万条记录。我在 Rails 3 中为 iPhone 应用程序实现了一个普通的 REST api,但是 SAYT 功能响应真的很慢。搜索数据库并返回结果需要花费大量时间。我怎样才能提高性能?

我已经索引了我的表。我应该更多地关注哪些方面,比如 MySQL 调优,还是应该使用 rails sphinx 或 sunspot?这会有所帮助吗?请帮助我提供所有专家建议。

4

6 回答 6

2

I agree with the general answer: use a search engine like Sphinx (and limit the number of results returned); they are designed to do exactly what you want.

However, while millions of records may sound like a lot, you should first determine what is taking a long time. I have major love for Sphinx and ThinkingSphinx -- they take what is a rather complex process and make it pretty simple and easy. But, in the end, a search engine it's another system to manage, configure, learn and know. If you don't have to go there, it's easier not to, right?

It might be the query, it might be time spent returning the data (limit is your friend!). Or it might be that you're getting hundreds of requests per second, perhaps because the delay on auto-complete is too short -- if a lookup occurs at every character, fast typists or multiple users can easily overrun the server with queries that provide no utility for the user.

Watch the Rails logs and see what's really going on. If it's a simple query performance issue, doing a complicated full-text search, then, yeah, that's going to be slow and Sphinx is going to be worth the effort. Your database has an explain tool that, with some work, can help you understand what the database is doing to get the result. It's not uncommon that an index doesn't get used.

What about caching? Memcached is a fantastic tool. Or maybe even just your buffer size settings for the database can allow it to use more memory for caching.

于 2012-03-14T19:28:15.940 回答
1

我还建议使用像 Sphinx 这样的全文搜索引擎。

有一个很好的关于使用 Sphinx 和 rails 和 thinking_sphinx gem 的截屏视频:

Railscast thinking_sphinx gem

使用该 gem,您还可以通过添加字段权重来影响有关重要性的搜索结果:

思考 Sphinx 文档

由于它是一个移动设备,我也希望将发送到移动设备的结果数量保持在最低限度,正如 madi 已经提到的那样。

玩得开心

于 2012-03-01T17:12:34.030 回答
1

我不确定添加更快的搜索是什么意思,但最好将搜索结果限制为 100,因为它涉及可用性。没有多少用户会通过 100 条记录进行搜索。

为了实现这样的搜索,我建议你包括关键字表。关键字表应包含记录 id 和与之关联的关键字,以及关键字在数据库中的交易次数。

因此它将帮助您确定前百名记录和最准确的搜索。

也有许多算法搜索作为 Map Reduce,它们同时运行。我不认为您的移动设备技术可以处理地图缩减。

于 2012-02-25T18:24:25.827 回答
1

为了快速搜索数百万条记录,您可能需要使用 trie 类型的数据结构。如果您需要帮助,http ://en.wikipedia.org/wiki/Trie 有 ruby​​ 示例代码。

稍微简化一下,trie 是一种存储效率很高的方法,用于跟踪哪些孩子属于哪些初始字符列表。

本质上,您的 SAYT 技术将输入一个字符串,并从该字符串的 trie 条目中返回前 15 个左右的结果。

当然,根据您的行的自相似程度,这将对您的 RAM 使用产生影响。

于 2012-03-13T17:50:43.320 回答
0

根据您所做的LIKE查询,在列开头匹配的查询可能会使用索引(在 Postgres 中我确定它们会这样做;在 MySQL 中我不确定)。

所以,

Widget.where('name LIKE ?', "#{search_term}%").all

将使用数据库索引(至少在 Postgres 中),而

Widget.where('name LIKE ?', "%#{search_term}%").all

将不会。请注意%搜索词开头的 。您的里程也可能因ILIKE(不区分大小写)和LIKE(区分大小写)条件而异。阅读数据库文档。这可能是最容易实现的目标。

另一个答复建议的搜索引擎是另一种选择。如果你部署在 Heroku 上,有一些云搜索插件很容易集成,但它可能仍然比稍微调整查询多一个数量级。

于 2012-03-11T05:15:47.273 回答
0

您可以为大多数搜索创建一个表格,然后以这种方式优先搜索,希望这会有所帮助。

于 2012-03-14T13:34:08.593 回答