使用Postgresql全文搜索查询速度太慢

时间:2016-03-11 03:28:21

标签: ruby-on-rails postgresql full-text-search

我在数据库中播放了10,000条记录,以创建更真实的条件。我正在使用postgresql全文搜索,查询时间在3-4秒之间。我正在寻找模型和协会。设置关联,其中用户具有多个记分板并且每个记分板具有用户。

以下是服务器查询的代码。

/login/register

下面给出了型号代码。

CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 3]]
  CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 3]]
  CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 3]]
  CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 3]]
  Rendered search/index.html.erb within layouts/application (3315.1ms)
  Rendered layouts/_shim.html.erb (0.1ms)
  Rendered layouts/_header.html.erb (1.1ms)
  Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 3583ms (Views: 287.1ms | ActiveRecord: 3290.1ms)

下面给出了控制器和视图代码。

include PgSearch
   pg_search_scope :search_by_scoreboard, 
                    :against => [:name_of_scoreboard, :name_of_organization, :name_of_activity],
                    associated_against: {user: [:name, :email]},

                    :using => {
                        :tsearch => {
                            :prefix => true,
                            :dictionary => "english",
                            :any_word => true
                        }

                    }

我已经阅读了pg_search gem的文档,并看到了它上面的rails。该文档说,如果您正在搜索关联,则无法使用索引加快搜索速度。以下文本来自pg_search文档。

def index
   @scoreboard_search = Scoreboard.search_by_scoreboard(params[:search]).paginate(:page => params[:page], :per_page => 4)
end 



<div id="search">
<%= form_tag search_path, :method => 'get' do %>
  <%= text_field_tag :search, params[:search], placeholder: "search scorecliq!" %>
  <div id="searchbutton">
  <%= submit_tag "Search", :name => nil %>
  </div>
<% end %>
<div>


<% @scoreboard_search.each do |scoreboard_search| %>
    <%= div_for scoreboard_search, :class => "inner-disp" do %> 
        Scoreboard: <%= link_to "#{scoreboard_search.name_of_scoreboard}", scoreboard_path(scoreboard_search.id) %><br>
        Organization: <%= scoreboard_search.name_of_organization %><br>
        Activity: <%= scoreboard_search.name_of_activity %><br>
        Admin: <%= link_to "#{scoreboard_search.user.name}", user_path(scoreboard_search.user.id)  %>
    <% end %>
<% end %>

<%= will_paginate @scoreboard_search, :page_links => false %>

是否可以加快搜索速度。我已经看到了轨道投射,他使用索引来加速搜索。考虑到文档说不可能,我不确定这是否适用于我的情况。但是,我确信必须有一些方法可以将查询速度提高到500毫秒以下。我不太清楚如何去做。任何有关如何使用pg_search full_text加速查询的帮助都将受到高度赞赏。谢谢!

ddl表,包含用户和记分表的索引定义。

It is possible to search columns on associated models. Note that if you do this, it will be impossible to speed up searches with database indexes. However, it is supported as a quick way to try out cross-model searching. 

0 个答案:

没有答案