Postgres类似的查询花费不同的时间,不确定出什么问题

时间:2018-07-09 14:01:15

标签: sql postgresql performance

我们有一个查询,该查询在每小时创建的几个不同的子表上运行,并从基表继承,例如tab_name1和tab_name2。该查询工作正常,但突然开始,自特定日期以来,所有Child的查询效果均不佳。

这是直到tab_name_20180621*都有效的查询,不确定之后会发生什么。

SELECT
    *
FROM
    tab_name_201806220300
WHERE
    id = 201806220300
    AND col1 IN (
        SELECT
            col1
        FROM
            tab_name2_201806220300
        WHERE
            uid = 5452
            AND id = 201806220300
    );

分析输出显示如下内容,执行时间差异很大。

#1
Nested Loop Semi Join  (cost=0.00..84762.11 rows=1 width=937) (actual time=117.599..117.599 rows=0 loops=1)
  Join Filter: (tab_name_201806210100.col1 = tab_name2_201806210100.col1)
  ->  Seq Scan on tab_name_201806210100  (cost=0.00..31603.56 rows=1 width=937) (actual time=117.596..117.596 rows=0 loops=1)
        Filter: (log_id = '201806220100'::bigint)
        Rows Removed by Filter: 434045
  ->  Materialize  (cost=0.00..53136.74 rows=1454 width=41) (never executed)
        ->  Seq Scan on tab_name2_201806210100  (cost=0.00..53129.47 rows=1454 width=41) (never executed)
              Filter: ((uid = 5452) AND (log_id = '201806210100'::bigint))
Planning time: 1.490 ms
Execution time: 117.723 ms


#2
Nested Loop Semi Join  (cost=0.00..10299.31 rows=48 width=1476) (actual time=1082.255..47041.945 rows=671 loops=1)
  Join Filter: (tab_name_201806220100.col1 = tab_name2_201806220100.col1)
  Rows Removed by Join Filter: 252444174
  ->  Seq Scan on tab_name_201806220100  (cost=0.00..4023.69 rows=95 width=1476) (actual time=0.008..36.292 rows=64153 loops=1)
        Filter: (log_id = '201806220100'::bigint)
  ->  Materialize  (cost=0.00..6274.19 rows=1 width=32) (actual time=0.000..0.264 rows=3935 loops=64153)
        ->  Seq Scan on tab_name2_201806220100  (cost=0.00..6274.19 rows=1 width=32) (actual time=0.464..55.475 rows=3960 loops=1)
              Filter: ((log_id = '201806220100'::bigint) AND (uid = 5452))
              Rows Removed by Filter: 140592
Planning time: 1.024 ms
Execution time: 47042.234 ms

我不知道从中可以推断出什么,以及如何从这里开始。你能帮我吗?

0 个答案:

没有答案