为什么不是';我的PostgreSQL数组索引没有被使用(Rails 4)?
我有一个PostgreSQL字符串数组作为表中的一列。我使用GIN方法创建了一个索引。但是任何查询都不会使用索引(相反,它们使用过滤器对整个表进行顺序扫描)。我错过了什么 以下是我的迁移:为什么不是';我的PostgreSQL数组索引没有被使用(Rails 4)?,sql,ruby-on-rails,postgresql,indexing,query-performance,Sql,Ruby On Rails,Postgresql,Indexing,Query Performance,我有一个PostgreSQL字符串数组作为表中的一列。我使用GIN方法创建了一个索引。但是任何查询都不会使用索引(相反,它们使用过滤器对整个表进行顺序扫描)。我错过了什么 以下是我的迁移: class CreateDocuments < ActiveRecord::Migration def up create_table :documents do |t| t.string :title t.string :tags, array: true, def
class CreateDocuments < ActiveRecord::Migration
def up
create_table :documents do |t|
t.string :title
t.string :tags, array: true, default: []
t.timestamps
end
add_index :documents, :tags, using: 'gin'
(1..100000).each do |i|
tags = []
tags << 'even' if (i % 2) == 0
tags << 'odd' if (i % 2) == 1
tags << 'divisible by 3' if (i % 3) == 0
tags << 'divisible by 4' if (i % 4) == 0
tags << 'divisible by 5' if (i % 5) == 0
Document.create(
title: i,
tags: tags
)
end
end
def down
drop_table :documents
end
end
要使用GIN索引,请使用而不是 默认GIN索引当前仅支持这些运算符(扩展附带了附加功能):
=
&&
因此,请尝试以下查询:
Document.where("'{divisible by 5}' <@ tags").explain
Document.where(“{可被5整除”)“表products
有多少行?该列的基数是多少?报告行数的唯一记录数是多少?@IgorRomanchenko该表的行数刚刚超过100000rows@Mihai有超过100000个唯一行。该列有超过18000个唯一值。任何关键字都可能使优化器choose将顺序扫描作为最佳选项。该查询返回多少行?
<@
@>
=
&&
Document.where("'{divisible by 5}' <@ tags").explain