优化轨道WHERE NOT IN查询这里的查询,因为它是在轨:挣扎在Rails的
User.limit(20).
where.not(id: to_skip, number_of_photos: 0).
where(age: @[email protected]_age_max).
tagged_with(@user.seeking_traits, on: :trait, any: true).
tagged_with(@user.seeking_gender, on: :trait, any: true).ids
而这里的EXPLAIN ANALYZE
输出。注意id <> ALL(...)
部分缩短了。其中大约有10K个ID。
Limit (cost=23.32..5331.16 rows=20 width=1698) (actual time=2237.871..2243.709 rows=20 loops=1)
-> Nested Loop Semi Join (cost=23.32..875817.48 rows=3300 width=1698) (actual time=2237.870..2243.701 rows=20 loops=1)
-> Merge Semi Join (cost=22.89..857813.95 rows=8311 width=1702) (actual time=463.757..2220.691 rows=1351 loops=1)
Merge Cond: (users.id = users_trait_taggings_356a192.taggable_id)
-> Index Scan using users_pkey on users (cost=0.29..834951.51 rows=37655 width=1698) (actual time=455.122..2199.322 rows=7866 loops=1)
Index Cond: (id IS NOT NULL)
Filter: ((number_of_photos <> 0) AND (age >= 18) AND (age <= 99) AND (id <> ALL ('{7066,7065,...,15624,23254}'::integer[])))
Rows Removed by Filter: 7652
-> Index Only Scan using taggings_idx on taggings users_trait_taggings_356a192 (cost=0.42..22767.59 rows=11393 width=4) (actual time=0.048..16.009 rows=4554 loops=1)
Index Cond: ((tag_id = 2) AND (taggable_type = 'User'::text) AND (context = 'trait'::text))
Heap Fetches: 4554
-> Index Scan using index_taggings_on_taggable_id_and_taggable_type_and_context on taggings users_trait_taggings_5df4b2a (cost=0.42..2.16 rows=1 width=4) (actual time=0.016..0.016 rows=0 loops=1351)
Index Cond: ((taggable_id = users.id) AND ((taggable_type)::text = 'User'::text) AND ((context)::text = 'trait'::text))
Filter: (tag_id = ANY ('{4,6}'::integer[]))
Rows Removed by Filter: 2
Total runtime: 2243.913 ms
Index Scan using users_pkey on users
好像有什么问题,索引扫描需要很长时间。即使有上age
,number_of_photos
索引和id
:
add_index "users", ["age"], name: "index_users_on_age", using: :btree
add_index "users", ["number_of_photos"], name: "index_users_on_number_of_photos", using: :btree
to_skip
是用户ID的数组不要跳过。 A user
有很多skips
。每个skip
有一个partner_id
。
所以下载to_skip
我做:
to_skip = @user.skips.pluck(:partner_id)
我试图查询隔离只是:
sql = User.limit(20).
where.not(id: to_skip, number_of_photos: 0).
where(age: @[email protected]_age_max).to_sql
而且还与讲解分析得到了同样的问题。同样,用户ID列表被剪切:
Limit (cost=0.00..435.34 rows=20 width=1698) (actual time=0.219..4.844 rows=20 loops=1)
-> Seq Scan on users (cost=0.00..819629.38 rows=37655 width=1698) (actual time=0.217..4.838 rows=20 loops=1)
Filter: ((id IS NOT NULL) AND (number_of_photos <> 0) AND (age >= 18) AND (age <= 99) AND (id <> ALL ('{7066,7065,...,15624,23254}'::integer[])))
Rows Removed by Filter: 6
Total runtime: 5.044 ms
有关如何在rails + postgres中优化此查询的任何想法?
编辑:下面是相关机型:
class User < ActiveRecord::Base
acts_as_messageable required: :body, # default [:topic, :body]
dependent: :destroy
has_many :skips, :dependent => :destroy
acts_as_taggable # Alias for acts_as_taggable_on :tags
acts_as_taggable_on :seeking_gender, :trait, :seeking_race
scope :by_updated_date, -> {
order("updated_at DESC")
}
end
# schema
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "skips", array: true
t.integer "number_of_photos", default: 0
t.integer "age"
end
add_index "users", ["age"], name: "index_users_on_age", using: :btree
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["number_of_photos"], name: "index_users_on_number_of_photos", using: :btree
add_index "users", ["updated_at"], name: "index_users_on_updated_at", order: {"updated_at"=>:desc}, using: :btree
class Skip < ActiveRecord::Base
belongs_to :user
end
# schema
create_table "skips", force: :cascade do |t|
t.integer "user_id"
t.integer "partner_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "skips", ["partner_id"], name: "index_skips_on_partner_id", using: :btree
add_index "skips", ["user_id"], name: "index_skips_on_user_id", using: :btree
请发布所有相关型号的代码。 –
添加了它们。请让我知道他们是否足够。 –
你有一个专门的'Skip'模型和'Users.skips'数组字段。后者的原因是什么? –