【问题标题】:Does loops=1 mean caching in PostgreSQL's analyze output?loops=1 是否意味着在 PostgreSQL 的分析输出中进行缓存?
【发布时间】:2013-10-30 11:45:43
【问题描述】:

我想优化一个慢速查询。该查询与以下示例非常相似:

DROP TABLE IF EXISTS test;
CREATE TABLE test (i integer);

DROP TABLE IF EXISTS test2;
CREATE TABLE test2 (i integer);
INSERT INTO test2 VALUES (1);
INSERT INTO test2 VALUES (2);
INSERT INTO test2 VALUES (3);
INSERT INTO test2 VALUES (4);
INSERT INTO test2 VALUES (5);
INSERT INTO test2 VALUES (6);

DROP TABLE IF EXISTS test3;
CREATE TABLE test3 (i integer, c1 varchar(100), c2 varchar(100));
INSERT INTO test3 VALUES (4, 'hello', 'world');
INSERT INTO test3 VALUES (7, 'hello', 'world');
INSERT INTO test3 VALUES (2, 'world', 'hello');
INSERT INTO test3 VALUES (120, 'world', 'hello');

EXPLAIN ANALYZE INSERT INTO test 
SELECT *
FROM test2
WHERE test2.i NOT IN 
    (SELECT min(i) FROM test3 GROUP BY c1, c2 HAVING COUNT(*) > 1);

Analyze 打印以下内容:

                                                       QUERY PLAN                                                       
------------------------------------------------------------------------------------------------------------------------
 Insert on test  (cost=15.95..55.95 rows=1200 width=4) (actual time=0.098..0.098 rows=0 loops=1)
   ->  Seq Scan on test2  (cost=15.95..55.95 rows=1200 width=4) (actual time=0.037..0.039 rows=4 loops=1)
         Filter: (NOT (hashed SubPlan 1))
         SubPlan 1
           ->  HashAggregate  (cost=13.40..15.52 rows=170 width=440) (actual time=0.017..0.018 rows=2 loops=1)
                 Filter: (count(*) > 1)
                 ->  Seq Scan on test3  (cost=0.00..11.70 rows=170 width=440) (actual time=0.001..0.001 rows=4 loops=1)
 Total runtime: 0.200 ms
(8 rows)

(真正的查询要处理更多的数据。)

loops=1 是否意味着 PostgreSQL 缓存了子查询的结果?请注意,子查询适用于表test3,因此它独立于查询的其他部分。 (有没有其他方法可以判断 PostgreSQL 是否缓存了这个子查询?)

【问题讨论】:

  • 真正的查询可能有不同的查询计划。 (取决于大小、可用的键/索引、调整和统计信息)。 “loops=1”表示扫描只执行一次。 (对于嵌套循环,内部循环必须一遍又一遍地重新启动)

标签: postgresql explain


【解决方案1】:

不,PostgreSQL 不使用子查询进行缓存。它能够物化一些数据,但你可以在计划中看到它。子查询调用只有一个请求。

【讨论】:

    猜你喜欢
    • 2013-02-04
    • 2015-03-12
    • 1970-01-01
    • 1970-01-01
    • 2014-12-07
    • 2016-05-13
    • 2019-08-24
    • 2010-12-20
    • 2018-06-14
    相关资源
    最近更新 更多