5

我在PostgreSQL 9.3。這應該在任何有100,000行以上的表上重現。 EXPLAIN ANALYZE顯示更多的行使用LIMIT 2進行掃描,但我無法弄清楚原因。爲什麼在這個查詢中LIMIT 2的數量級比LIMIT 1要長?

限制1:

EXPLAIN ANALYZE WITH base AS (
    SELECT *, ROW_NUMBER() OVER() AS rownum FROM a_big_table 
), filter AS (
    SELECT rownum, true AS thing FROM base 
) SELECT * FROM base LEFT JOIN filter USING (rownum) WHERE filter.thing LIMIT 1 

結果:

Limit (cost=283512.19..283517.66 rows=1 width=2114) (actual time=0.019..0.019 rows=1 loops=1) 
    CTE base 
    -> WindowAgg (cost=0.00..188702.69 rows=4740475 width=101) (actual time=0.008..0.008 rows=1 loops=1) 
      -> Seq Scan on a_big_table (cost=0.00..129446.75 rows=4740475 width=101) (actual time=0.003..0.003 rows=1 loops=1) 
    CTE filter 
    -> CTE Scan on base base_1 (cost=0.00..94809.50 rows=4740475 width=8) (actual time=0.000..0.000 rows=1 loops=1) 
    -> Nested Loop (cost=0.00..307677626611.24 rows=56180269915 width=2114) (actual time=0.018..0.018 rows=1 loops=1) 
     Join Filter: (base.rownum = filter.rownum) 
     -> CTE Scan on base (cost=0.00..94809.50 rows=4740475 width=2113) (actual time=0.011..0.011 rows=1 loops=1) 
     -> CTE Scan on filter (cost=0.00..94809.50 rows=2370238 width=9) (actual time=0.002..0.002 rows=1 loops=1) 
       Filter: thing 
Total runtime: 0.057 ms 

極限2:

EXPLAIN ANALYZE WITH base AS (
    SELECT *, ROW_NUMBER() OVER() AS rownum FROM a_big_table 
), filter AS (
    SELECT rownum, true AS thing FROM base 
) SELECT * FROM base LEFT JOIN filter USING (rownum) WHERE filter.thing LIMIT 2 

結果:

Limit (cost=283512.19..283523.14 rows=2 width=2114) (actual time=0.018..14162.283 rows=2 loops=1) 
    CTE base 
    -> WindowAgg (cost=0.00..188702.69 rows=4740475 width=101) (actual time=0.008..4443.359 rows=4714243 loops=1) 
      -> Seq Scan on a_big_table (cost=0.00..129446.75 rows=4740475 width=101) (actual time=0.002..1421.622 rows=4714243 loops=1) 
    CTE filter 
    -> CTE Scan on base base_1 (cost=0.00..94809.50 rows=4740475 width=8) (actual time=0.001..10214.684 rows=4714243 loops=1) 
    -> Nested Loop (cost=0.00..307677626611.24 rows=56180269915 width=2114) (actual time=0.018..14162.280 rows=2 loops=1) 
     Join Filter: (base.rownum = filter.rownum) 
     Rows Removed by Join Filter: 4714243 
     -> CTE Scan on base (cost=0.00..94809.50 rows=4740475 width=2113) (actual time=0.011..0.028 rows=2 loops=1) 
     -> CTE Scan on filter (cost=0.00..94809.50 rows=2370238 width=9) (actual time=0.009..6595.770 rows=2357122 loops=2) 
       Filter: thing 
Total runtime: 14247.374 ms 
+1

CTE就像PostgreSQL中的優化柵欄一樣。嘗試用子選擇來重新編寫查詢。 – vyegorov 2014-10-11 19:35:47

+0

圍欄行爲是否取決於LIMIT的值?如果這在任何地方都有記錄,我找不到它。 – rcrogers 2014-10-11 19:42:02

回答

1

發動機首先運行,然後限制。所以,你可以看到更多的行。

+1

你能解釋一下嗎? 'explain'輸出表示(對我)重複掃描產生用LIMIT指定的行數。 – mabi 2014-10-23 13:12:45