Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 优化大数组搜索ruby_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 优化大数组搜索ruby

Ruby on rails 优化大数组搜索ruby,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在创建一个友谊功能。 但我有个问题。当一个用户向一个特定的用户发送请求时,它会遍历3k多个用户以找到合适的人。 我想知道是否有一种ruby方法可以提高搜索速度 我用的是has_友谊宝石 这是我的密码: 查看好友:index.html.erb <div class="info d-flex flex-column"> <header class="ui basic segment"> <h3 cla

我正在创建一个友谊功能。 但我有个问题。当一个用户向一个特定的用户发送请求时,它会遍历3k多个用户以找到合适的人。 我想知道是否有一种ruby方法可以提高搜索速度

我用的是has_友谊宝石

这是我的密码: 查看好友:index.html.erb


<div class="info  d-flex  flex-column">
    <header class="ui basic segment">
      <h3 class="typo text-center mt-2">Ajoutes tes amis I think you can try save 
user.pseudo
to db and search by that field. You can improve speed with db index.

As you're filtering records by their
pseudo
column, in its entirety, you could use
find_by
, which implements the
WHERE
clause plus a
LIMIT
of 1:

User.find_by(pseudo: @search)


我认为您可以尝试将
user.pseudo
保存到db并按该字段搜索。您可以使用db索引提高速度。

当您通过记录的
列筛选记录时,您可以使用
find\u by
,它实现
WHERE
子句加上
限制
1:


你到底是什么意思?我应该在哪里尝试呢?您应该在这里使用LIKE查询。User.where(“users.pseudo like#{%@search%}”)我尝试了这个方法,但出现了这个错误,可以解释一下吗PG::UndefinedColumn:错误:列“brzny”不存在第1行:…选择“users”。*从“users”中选择(users.pseudo like brzny)`其中brzny是用户提交的伪用户
用户。其中(“users.pseudo like?”,“%#{@search}%”
,Vishal的建议中有一个输入错误。非常感谢,它运行得非常快!但是它没有通过伪数组找到用户,我最终得到了一个空数组。你有什么想法吗?我已经用过了,但它确实减慢了一切。它几乎在查看3K记录您是指
find
还是
find_by
?3k行对博士后来说似乎不是问题。也许您可以添加有关表结构和查询结果的信息,以深入了解详细信息。
explain
。当您执行包含
where column=…
的查询时,该列上的索引将被使用。@SebastianPalma这不是唯一有问题的查询。还有
当前用户。不是朋友。包括?(@results)
。乍一看,这确实会让所有非好友进行线性搜索。@SergioTulentsev如果你是对的,我更改了这一部分,一切正常。多谢各位