简体   繁体   English

Ruby on Rails,加快数据库搜索速度的方法

[英]Ruby on rails, the ways to speed up search in database

I have the table products with following fields: code - string, id - integer 我的表产品具有以下字段:代码-字符串,id-整数

My table has over 5 million rows. 我的表有超过500万行。

In the model I use code: 在模型中,我使用代码:

find(:all, :conditions => ['code = ?', "#{search}"])

It takes 2 seconds to find the result. 需要2秒钟才能找到结果。
Is there any way to reduce search time? 有什么办法可以减少搜索时间?

I'm using rails 3.2.11, ruby 1.9.3 and sqlite3 as db 我正在使用Rails 3.2.11,ruby 1.9.3和sqlite3作为数据库

UPD UPD
Changed model code 更改型号代码

I've managed to find answer. 我设法找到答案。 Just added 刚刚添加

add_index :products, :code

to migration change and now it works really fast. 迁移更改,现在它运行非常快。 You will find more information here 您可以在这里找到更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM