I have been doing some heavy reading the last couple days on indexing and I'm trying to figure out the right way to index a query I have with a lot of constraints. I am using the postgres_ext gem to support array datatypes and GIN and GIST index types.
I have a two queries
.where("a_id IN (?) and b = ? and active = ? and ? != ALL(c) and ? = ANY(d)")
.where("a_id =? and active =? and ? != ALL(c)")
c and d are integer arrays
The indexes I plan on adding:
add_index :deals, [:a, :b], :where => "active = true"
add_index :deals [:c, :d], :index_type => :gin, :where => "active = true"
Will postgres use both of these multicolumn indexes in the first query?
Should array datatypes always be in "gin" index types? or can you also put them in a b-tree index?
and finally will the first index be used for 'a' in both of the queries?
Additional Information:
I am using 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