Sql 慢速postgres查询

Sql 慢速postgres查询,sql,postgresql,indexing,Sql,Postgresql,Indexing,OSX上的Postgres 10.3 为什么在#1和#3中扫描“事件”中的所有行 为什么将DISTINCT添加到#2(下面的视图#4查询)会导致截然不同的(快速)执行和执行计划 我对博士后的知识非常肤浅(一般来说,DB理论还可以),所以我不知道其中的怪癖,索引是下一级的 最初,我使用Node+Sequelize进行joins#3,速度很慢,所以在中尝试过。如果“集合项目”的计数为1,则快速执行 如果可以避免的话,我更喜欢使用Sequelize生成的查询,而不是手工创建的查询 谢谢你的帮助 1-慢

OSX上的Postgres 10.3

为什么在#1和#3中扫描“事件”中的所有行

为什么将DISTINCT添加到#2(下面的视图#4查询)会导致截然不同的(快速)执行和执行计划

我对博士后的知识非常肤浅(一般来说,DB理论还可以),所以我不知道其中的怪癖,索引是下一级的

最初,我使用Node+Sequelize进行joins#3,速度很慢,所以在中尝试过。如果“集合项目”的计数为1,则快速执行

如果可以避免的话,我更喜欢使用Sequelize生成的查询,而不是手工创建的查询

谢谢你的帮助

1-慢进: 分析:

Merge Semi Join  (cost=2.08..12.60 rows=4 width=1870) (actual time=0.079..6294.739 rows=7 loops=1)                                                |
  Merge Cond: (event.id = collection_item.item_id)                                                                                                |
  ->  Index Scan using idx_event_id on event  (cost=0.43..1066819.90 rows=10000215 width=1870) (actual time=0.012..5525.787 rows=9109726 loops=1) |
  ->  Sort  (cost=1.62..1.64 rows=4 width=4) (actual time=0.028..0.029 rows=7 loops=1)                                                            |
        Sort Key: collection_item.item_id                                                                                                         |
        Sort Method: quicksort  Memory: 25kB                                                                                                      |
        ->  Seq Scan on collection_item  (cost=0.00..1.58 rows=4 width=4) (actual time=0.018..0.022 rows=7 loops=1)                               |
              Filter: ((collection_id = 2) AND ((item_type)::text = 'event'::text))                                                               |
              Rows Removed by Filter: 33                                                                                                          |
Planning time: 0.412 ms                                                                                                                           |
Execution time: 6296.260 ms                                                                                                                       |
Limit  (cost=2.90..16.42 rows=4 width=3676) (actual time=0.100..6207.552 rows=7 loops=1)                                                                                  |
  ->  Nested Loop Left Join  (cost=2.90..16.42 rows=4 width=3676) (actual time=0.100..6207.550 rows=7 loops=1)                                                            |
        ->  Nested Loop Left Join  (cost=2.76..15.74 rows=4 width=3664) (actual time=0.094..6207.502 rows=7 loops=1)                                                      |
              ->  Nested Loop Left Join  (cost=2.22..13.24 rows=4 width=3359) (actual time=0.092..6207.492 rows=7 loops=1)                                                |
                    ->  Merge Join  (cost=2.08..12.60 rows=4 width=1836) (actual time=0.089..6207.012 rows=7 loops=1)                                                     |
                          Merge Cond: (event.id = "collectionItem".item_id)                                                                                               |
                          ->  Index Scan using idx_event_id on event  (cost=0.43..1066819.90 rows=10000215 width=1832) (actual time=0.018..5455.081 rows=9109726 loops=1) |
                          ->  Sort  (cost=1.62..1.64 rows=4 width=8) (actual time=0.027..0.028 rows=7 loops=1)                                                            |
                                Sort Key: "collectionItem".item_id                                                                                                        |
                                Sort Method: quicksort  Memory: 25kB                                                                                                      |
                                ->  Seq Scan on collection_item "collectionItem"  (cost=0.00..1.58 rows=4 width=8) (actual time=0.014..0.019 rows=7 loops=1)              |
                                      Filter: (((item_type)::text = 'event'::text) AND (collection_id = 2))                                                               |
                                      Rows Removed by Filter: 33                                                                                                          |
                    ->  Index Scan using location_pkey on location "primaryLocation"  (cost=0.14..0.16 rows=1 width=1523) (actual time=0.067..0.067 rows=0 loops=7)       |
                          Index Cond: (event.primary_location_id = id)                                                                                                    |
              ->  Index Scan using event_date_pkey on event_date "primaryEventDate"  (cost=0.55..0.63 rows=1 width=305) (actual time=0.000..0.000 rows=0 loops=7)         |
                    Index Cond: (event.primary_event_date_id = id)                                                                                                        |
        ->  Index Scan using idx_usereventtrack_event on user_event_track "userEventTrack"  (cost=0.14..0.16 rows=1 width=16) (actual time=0.005..0.006 rows=1 loops=7)   |
              Index Cond: (event.id = event_id)                                                                                                                           |
              Filter: (user_id = 1)                                                                                                                                       |
Planning time: 6.875 ms                                                                                                                                                   |
Execution time: 6208.728 ms                                                                                                                                               |
2-快进 分析

Index Scan using idx_event_id on event  (cost=2.03..76.86 rows=10 width=1870) (actual time=0.092..0.119 rows=7 loops=1) |
  Index Cond: (id = ANY ($1))                                                                                           |
  InitPlan 2 (returns $1)                                                                                               |
    ->  Result  (cost=1.58..1.59 rows=1 width=32) (actual time=0.043..0.043 rows=1 loops=1)                             |
          InitPlan 1 (returns $0)                                                                                       |
            ->  Seq Scan on collection_item  (cost=0.00..1.58 rows=4 width=4) (actual time=0.035..0.039 rows=7 loops=1) |
                  Filter: ((collection_id = 2) AND ((item_type)::text = 'event'::text))                                 |
                  Rows Removed by Filter: 33                                                                            |
Planning time: 0.204 ms                                                                                                 |
Execution time: 0.147 ms                                                                                                |
Nested Loop  (cost=2.03..27.01 rows=3 width=1870) (actual time=0.088..0.143 rows=7 loops=1)                                 
  ->  HashAggregate  (cost=1.59..1.62 rows=3 width=4) (actual time=0.054..0.056 rows=7 loops=1)                             
        Group Key: collection_item.item_id                                                                                  
        ->  Seq Scan on collection_item  (cost=0.00..1.58 rows=4 width=4) (actual time=0.042..0.047 rows=7 loops=1)         
              Filter: ((collection_id = 2) AND ((item_type)::text = 'event'::text))                                         
              Rows Removed by Filter: 33                                                                                    
  ->  Index Scan using idx_event_id on event  (cost=0.43..8.45 rows=1 width=1870) (actual time=0.012..0.012 rows=1 loops=7) 
        Index Cond: (id = collection_item.item_id)                                                                          
Planning time: 0.388 ms                                                                                                     
Execution time: 0.220 ms                                                                                                    
3-慢速连接 分析:

Merge Semi Join  (cost=2.08..12.60 rows=4 width=1870) (actual time=0.079..6294.739 rows=7 loops=1)                                                |
  Merge Cond: (event.id = collection_item.item_id)                                                                                                |
  ->  Index Scan using idx_event_id on event  (cost=0.43..1066819.90 rows=10000215 width=1870) (actual time=0.012..5525.787 rows=9109726 loops=1) |
  ->  Sort  (cost=1.62..1.64 rows=4 width=4) (actual time=0.028..0.029 rows=7 loops=1)                                                            |
        Sort Key: collection_item.item_id                                                                                                         |
        Sort Method: quicksort  Memory: 25kB                                                                                                      |
        ->  Seq Scan on collection_item  (cost=0.00..1.58 rows=4 width=4) (actual time=0.018..0.022 rows=7 loops=1)                               |
              Filter: ((collection_id = 2) AND ((item_type)::text = 'event'::text))                                                               |
              Rows Removed by Filter: 33                                                                                                          |
Planning time: 0.412 ms                                                                                                                           |
Execution time: 6296.260 ms                                                                                                                       |
Limit  (cost=2.90..16.42 rows=4 width=3676) (actual time=0.100..6207.552 rows=7 loops=1)                                                                                  |
  ->  Nested Loop Left Join  (cost=2.90..16.42 rows=4 width=3676) (actual time=0.100..6207.550 rows=7 loops=1)                                                            |
        ->  Nested Loop Left Join  (cost=2.76..15.74 rows=4 width=3664) (actual time=0.094..6207.502 rows=7 loops=1)                                                      |
              ->  Nested Loop Left Join  (cost=2.22..13.24 rows=4 width=3359) (actual time=0.092..6207.492 rows=7 loops=1)                                                |
                    ->  Merge Join  (cost=2.08..12.60 rows=4 width=1836) (actual time=0.089..6207.012 rows=7 loops=1)                                                     |
                          Merge Cond: (event.id = "collectionItem".item_id)                                                                                               |
                          ->  Index Scan using idx_event_id on event  (cost=0.43..1066819.90 rows=10000215 width=1832) (actual time=0.018..5455.081 rows=9109726 loops=1) |
                          ->  Sort  (cost=1.62..1.64 rows=4 width=8) (actual time=0.027..0.028 rows=7 loops=1)                                                            |
                                Sort Key: "collectionItem".item_id                                                                                                        |
                                Sort Method: quicksort  Memory: 25kB                                                                                                      |
                                ->  Seq Scan on collection_item "collectionItem"  (cost=0.00..1.58 rows=4 width=8) (actual time=0.014..0.019 rows=7 loops=1)              |
                                      Filter: (((item_type)::text = 'event'::text) AND (collection_id = 2))                                                               |
                                      Rows Removed by Filter: 33                                                                                                          |
                    ->  Index Scan using location_pkey on location "primaryLocation"  (cost=0.14..0.16 rows=1 width=1523) (actual time=0.067..0.067 rows=0 loops=7)       |
                          Index Cond: (event.primary_location_id = id)                                                                                                    |
              ->  Index Scan using event_date_pkey on event_date "primaryEventDate"  (cost=0.55..0.63 rows=1 width=305) (actual time=0.000..0.000 rows=0 loops=7)         |
                    Index Cond: (event.primary_event_date_id = id)                                                                                                        |
        ->  Index Scan using idx_usereventtrack_event on user_event_track "userEventTrack"  (cost=0.14..0.16 rows=1 width=16) (actual time=0.005..0.006 rows=1 loops=7)   |
              Index Cond: (event.id = event_id)                                                                                                                           |
              Filter: (user_id = 1)                                                                                                                                       |
Planning time: 6.875 ms                                                                                                                                                   |
Execution time: 6208.728 ms                                                                                                                                               |
4-快速输入-与#1相同,但具有不同的 分析

Index Scan using idx_event_id on event  (cost=2.03..76.86 rows=10 width=1870) (actual time=0.092..0.119 rows=7 loops=1) |
  Index Cond: (id = ANY ($1))                                                                                           |
  InitPlan 2 (returns $1)                                                                                               |
    ->  Result  (cost=1.58..1.59 rows=1 width=32) (actual time=0.043..0.043 rows=1 loops=1)                             |
          InitPlan 1 (returns $0)                                                                                       |
            ->  Seq Scan on collection_item  (cost=0.00..1.58 rows=4 width=4) (actual time=0.035..0.039 rows=7 loops=1) |
                  Filter: ((collection_id = 2) AND ((item_type)::text = 'event'::text))                                 |
                  Rows Removed by Filter: 33                                                                            |
Planning time: 0.204 ms                                                                                                 |
Execution time: 0.147 ms                                                                                                |
Nested Loop  (cost=2.03..27.01 rows=3 width=1870) (actual time=0.088..0.143 rows=7 loops=1)                                 
  ->  HashAggregate  (cost=1.59..1.62 rows=3 width=4) (actual time=0.054..0.056 rows=7 loops=1)                             
        Group Key: collection_item.item_id                                                                                  
        ->  Seq Scan on collection_item  (cost=0.00..1.58 rows=4 width=4) (actual time=0.042..0.047 rows=7 loops=1)         
              Filter: ((collection_id = 2) AND ((item_type)::text = 'event'::text))                                         
              Rows Removed by Filter: 33                                                                                    
  ->  Index Scan using idx_event_id on event  (cost=0.43..8.45 rows=1 width=1870) (actual time=0.012..0.012 rows=1 loops=7) 
        Index Cond: (id = collection_item.item_id)                                                                          
Planning time: 0.388 ms                                                                                                     
Execution time: 0.220 ms                                                                                                    

多次执行的结果是否相同?最后一个查询根本没有在中使用
。我在帖子顶部说,最初我在做JOIN(#3),速度很慢。然后我在(#1)中进行了测试,速度也出人意料地慢,因为我希望它首先运行子查询,返回一位数的结果,然后在“event”表上执行join/scan。然后我找到了#2的例子,使用任意+转换作为数组。在我看来,这相当于#1,Postgres应该这样做。执行时间是一致的。您的postgresql配置和postgresql版本是什么?看起来像是计划问题。你有新的索引和清空的表吗?10.3在OS X上。我做了一个后期更新-将DISTINCT添加到#1(显示为query#4)会导致完全不同的执行计划和快速。