postgresql:" select"中没有使用哪个索引。查询搜索

时间:2018-05-31 08:21:26

标签: sql postgresql query-performance

我在不同的机器上有两个数据库服务器,它们拥有相同的配置文件。我把他们命名为A& B,A(在线)的600 + M记录,B(测试)的600M记录。

问题:

我在A和B上创建相同的索引,所有索引都在表tasks,列time上。当我在A和B中进行相同的选择搜索(explain analyze select * from tasks order by time desc limit 1)时,A中的搜索从不用户索引扫描,但B使用,即使我禁止在A上使用enable_seqscan。

就像A上的索引永远不存在!但\d tasks显示时间索引存在于其中。

表格信息:

                                Table "public.tasks"
     Column     |     Type      |                     Modifiers                      
----------------+---------------+----------------------------------------------------
 id             | integer       | not null default nextval('tasks_id_seq'::regclass)
 taskid         | character(32) | not null
 time           | integer       | 
 threat         | integer       | 
Indexes:
    "tasks_pkey" PRIMARY KEY, btree (id)
    "tasks_taskid_key" UNIQUE CONSTRAINT, btree (taskid)
    "tasks_taskid_index" btree (taskid)
    "tasks_time_index" btree ("time")
Referenced by:
    TABLE "alert_detail" CONSTRAINT "alert_detail_taskid_fkey" FOREIGN KEY (taskid) REFERENCES tasks(taskid) ON DELETE CASCADE
Triggers:
    after_del AFTER DELETE ON tasks FOR EACH ROW EXECUTE PROCEDURE fileref_auto_decrease()

查询:(在#34; SET enable_seqscan = Off;""分析任务;")

# explain (analyze,buffers)  select * from tasks order by time desc limit 2;
                                                                   QUERY PLAN                                                                

---------------------------------------------------------------------------------------------------------------------------------------------
----
 Limit  (cost=10000729745.02..10000729745.02 rows=2 width=668) (actual time=20602.895..20602.896 rows=2 loops=1)
   Buffers: shared hit=457 read=592336
   ->  Sort  (cost=10000729745.02..10000746864.02 rows=6847601 width=668) (actual time=20602.894..20602.895 rows=2 loops=1)
         Sort Key: "time"
         Sort Method: top-N heapsort  Memory: 27kB
         Buffers: shared hit=457 read=592336
         ->  Seq Scan on tasks  (cost=10000000000.00..10000661269.01 rows=6847601 width=668) (actual time=0.003..18939.268 rows=6847881 loops
=1)
               Buffers: shared hit=457 read=592336
 Planning time: 0.094 ms
 Execution time: 20602.930 ms

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题,原因可能是A中的postgres没有足够的内存,重启postgresql服务后就可以了。我认为B的负荷会好于A