简体   繁体   English

Postgres 9.4 SQL查询超时

[英]Postgres 9.4 sql query timeout

Below query times out after adding these two lines or any single one of it 添加这两行或其中任何一行后,下面的查询超时

AND final IS NOT NULL 
ORDER BY tmodified ASC

Query keeps running more then 10 min.... and time out. 查询继续运行超过10分钟。。。并超时。

if I remove above two lines, it return the results within 1 milliseconds, which works Ok. 如果我删除以上两行,它将在1毫秒内返回结果,可以。

Any idea how can I make above two lines work with below query? 任何想法如何使上面两行与下面的查询一起工作?

Table table_h has 36 million records and in this table 表table_h具有3600万条记录,并且该表中

column final is  numeric(10,5)
column tmodified is bigint, Timestamp

I am using Postgres 9.4 我正在使用Postgres 9.4

Here is the complete query. 这是完整的查询。

SELECT DISTINCT t.cid, h.a, am.b, u2.c, u.d, h.e, ie.f, im.g FROM table_am as am
  INNER JOIN table_t as t on (t.id = am.id AND t.type = am.type)
  INNER JOIN table_h AS h on h.iid = t.id
  INNER JOIN table_u AS u ON u.id = h.uid
  INNER JOIN table_u AS u2 ON u2.id = h.lu
  INNER JOIN table_im AS im ON im.asid = am.sid
  INNER JOIN table_ie AS ie ON ie.uid = u.uid
  INNER JOIN table_g AS g ON g.id = h.oldid
WHERE h.final >= 0 
  AND h.final IS NOT NULL
  AND h.tmodified >= 1499903419
  AND ie.p = im.p
  AND h.sr IN ('x', 'y', 'z')
  AND h.id = (SELECT id FROM table_h WHERE oldid = h.oldid AND final >= 0 
              AND final IS NOT NULL -- Issue is here and
              ORDER BY tmodified ASC -- Issue is here
              LIMIT 1)
  AND h.id NOT IN (SELECT id FROM table_m  WHERE tmodified > 1499903419) 

Try replacing the correlated subquery with a join, something like this: 尝试用联接替换相关的子查询,如下所示:

SELECT ...
FROM table_am as am
...
INNER JOIN table_g AS g ON g.id = h.oldid
INNER JOIN
(
    SELECT id, oldid, MIN(tmodified) AS min_mod
    FROM table_h
    WHERE final >= 0 AND final IS NOT NULL
    GROUP BY id, oldid
) t
    ON h.id     = t.id    AND
       h.oldid  = t.oldid AND
       h.tmodified = t.min_mo

Note that we could have also expressed this using row number or another analytical function, but hopefully this gives you a place to start. 请注意,我们也可以使用行号或其他分析函数来表示,但是希望这可以为您提供一个起点。

Well, I can solve half your problem. 好吧,我可以解决您一半的问题。 The condition: 条件:

AND h.final IS NOT NULL

is not needed. 不需要。 The condition: 条件:

h.final >= 0

already takes this into account. 已经考虑到这一点。

If the remaining query returns so quickly, then use a subquery or cte and then order by: 如果其余查询返回的速度如此之快,请使用子查询或cte,然后按以下顺序排序:

with cte as (
      select . . ., t.modified
     )
select cte.*
from cte
order by modified;

Vacuuming all involve table will fix the issue. 清理所有涉及表将解决此问题。

VACUUM ANALYZE table_am;
VACUUM ANALYZE table_t;
VACUUM ANALYZE table_h;
VACUUM ANALYZE table_u;
VACUUM ANALYZE table_im;
VACUUM ANALYZE table_ie;
VACUUM ANALYZE table_g;
VACUUM ANALYZE table_m;

Ref: https://www.postgresql.org/docs/9.4/static/sql-vacuum.html 参考: https : //www.postgresql.org/docs/9.4/static/sql-vacuum.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM