简体   繁体   English

如何减少优化的查询执行时间?

[英]How to reduce Query execution time for Optimization?

Hi this current Query is taking too long to execute, what changes should be made to reduce the execution time?您好,当前的查询执行时间过长,应进行哪些更改以减少执行时间?

EXPLAIN ANALYZE
SELECT  date_trunc({{period}}, wb.timestamp),
        CAST(COUNT(DISTINCT(org.id)) AS float) / CAST(COUNT(DISTINCT(wb.anonymous_id)) AS float) AS "marketing-lead conversion rate"
FROM website_prod.pages wb
FULL JOIN core_prod.organizations_organization org ON CAST(wb.timestamp AS DATE) = CAST(org.created_at AS DATE)
WHERE   NOT org.is_internal 
        AND wb.timestamp > date_trunc('month', CURRENT_DATE) - INTERVAL '1 year'
        AND org.created_at > date_trunc('month', CURRENT_DATE) - INTERVAL '1 year'
GROUP BY 1

QUERY PLAN

Sort Key: ((org.created_at)::date)
Sort Method: quicksort Memory: 608kB
-> Seq Scan on organizations_organization org (cost=0.00..259.02 rows=5080 width=19) (actual time=0.017..6.266 rows=5316 loops=1)
Filter: ((NOT is_internal) AND (created_at > (date_trunc('month'::text, (('now'::cstring)::date)::timestamp with time zone) - '1 year'::interval)))
Rows Removed by Filter: 1540
-> Materialize (cost=69882.44..71012.63 rows=226038 width=45) (actual time=541.950..849.952 rows=3917712 loops=1)
-> Sort (cost=69882.44..70447.54 rows=226038 width=45) (actual time=541.945..592.666 rows=232720 loops=1)
Sort Key: ((wb."timestamp")::date)
Sort Method: external merge Disk: 14104kB
-> Seq Scan on pages wb (cost=0.00..42826.15 rows=226038 width=45) (actual time=40.801..358.687 rows=232720 loops=1)
Filter: ("timestamp" > (date_trunc('month'::text, (('now'::cstring)::date)::timestamp with time zone) - '1 year'::interval))
Rows Removed by Filter: 72766
Planning time: 0.214 ms
Execution time: 26614.737 ms

Your join looks extremely suspect to me...你的加入在我看来非常可疑......

  website_prod.pages wb
FULL JOIN
  core_prod.organizations_organization org
    ON CAST(wb.timestamp AS DATE) = CAST(org.created_at AS DATE)

In this case, you're joining all the rows in wb to all the rows in org , provided that they're "for" the same date.在这种情况下,您将wb中的所有行连接到org中的所有行,前提是它们“用于”同一日期。

So, for example, if on a particular date (say, 2021-04-01 ), there are 10 wb records and 10 org records, the join will give all 100 combinations.因此,例如,如果在特定日期(例如2021-04-01 )有 10 条wb记录和 10 条org记录,则连接将提供所有 100 种组合。

  • Is that REALLY what you want???这真的是你想要的吗???

I would "expect" something more like...我会“期待”更像...

  website_prod.pages wb
FULL JOIN
  core_prod.organizations_organization org
    ON  wb.some_key = org.some_key
    AND CAST(wb.timestamp AS DATE) = CAST(org.created_at AS DATE)

Also, to make the join effective, the plan shows that it's having to sort the data in to time order.此外,为了使连接有效,该计划表明它必须按时间顺序对数据进行排序。

You haven't specifically shown what indexes exist, but for the current query you would benefit from indexes on the columns being joined... wb.timestamp and org.created_at .您没有具体显示存在哪些索引,但对于当前查询,您将从正在连接的列上的索引中受益...... wb.timestamporg.created_at Though that would change if you changed the join predicate as described above.尽管如果您如上所述更改连接谓词,那将会改变。

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

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