【发布时间】:2013-03-08 19:56:31
【问题描述】:
在过去的几天里,我一直在阅读有关索引的大量信息,我正在尝试找出正确的方法来索引我有很多限制的查询。我正在使用postgres_ext gem 来支持数组数据类型以及 GIN 和 GIST 索引类型。
我有两个问题
.where("a_id IN (?) and b = ? and active = ? and ? != ALL(c) and ? = ANY(d)")
.where("a_id =? and active =? and ? != ALL(c)")
c 和 d 是整数数组
我打算添加的索引:
add_index :deals, [:a, :b], :where => "active = true"
add_index :deals [:c, :d], :index_type => :gin, :where => "active = true"
postgres 会在第一个查询中同时使用这两个多列索引吗?
数组数据类型应该总是在“gin”索引类型中吗?或者您也可以将它们放入 b-tree 索引中?
最后会在两个查询中将第一个索引用于“a”吗?
其他信息:
我使用的是 PostgreSQL 9.1.3
create_table "table", :force => true do |t|
t.integer "a_id" ##foreign key
t.string "title"
t.text "description", :default => ""
t.boolean "active", :default => true
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "b",
t.integer "c", :limit => 8, :array => true
t.integer "d", :array => true
end
【问题讨论】:
-
请显示您的表定义或等效的 Rails 模型和 PostgreSQL 版本。
-
添加了更多信息@CraigRinger
-
重新数组和 GIN,您可以拥有数组的 b-tree 索引,但它对于“数组包含元素”之类的操作没有用。为此,您需要 GIN,或者将
intarray扩展及其GiST索引类型用于整数数组,在写入负载下性能更好,但在读取负载下性能更差。 -
感谢您的回复。还有一个问题,第一个查询会使用两个索引吗?
-
最好的判断方法是使用
EXPLAIN ANALYZE看看。
标签: ruby-on-rails postgresql ruby-on-rails-3.2 indexing postgres-ext