我的查询未获取创建的索引

时间:2019-06-22 17:27:36

标签: postgresql ruby-on-rails-4

我创建了一个名为user_type_index的索引

我的查询:

User.where("user_type IN (:user_types)", user_types: user_types)
user_types = ['super_admin', 'system_admin']

即使user_type具有索引,该查询也会按顺序运行。

尝试删除并创建索引,仍然没有用

class User < ActiveRecord::Base

def matching_with_user_types
   where("user_type IN (:user_types)", user_types: user_types)
end

end

user_types = ['super_admin', 'system_admin']
User.matching_with_user_types(user_types).explain

下面是输出:

Seq Scan on users  (cost=0.00..11.75 rows=2 width=537)
   Filter: ((user_type)::text = ANY ('{super_admin,system_admin}'::text[]))
(2 rows)

预期:使用索引查询 实际:依序查询

PostgreSQL查询

获取与所有记录匹配的记录

EXPLAIN SELECT id FROM "users" WHERE (user_type IN ('super_admin','system_admin','network','super')); 
QUERY PLAN 
-------------------------------------------------------------------------------------- 
Seq Scan on users (cost=0.00..3039.28 rows=75228 width=377) 
  Filter: ((user_type)::text = ANY ('{'super_admin','system_admin','network','super'}'::text[])) 
(2 rows)

获取与以下项匹配的记录:

EXPLAIN SELECT id FROM "users" WHERE (user_type IN ('super_admin')); 
QUERY PLAN 
-------------------------------------------------------------------------------------- 
Seq Scan on users (cost=0.00..2789.24 rows=25418 width=377) 
  Filter: ((user_type)::text = ANY ('{'super_admin'}'::text[])) 
(2 rows)

0 个答案:

没有答案