【问题标题】:Turn off Postgres parallel query for a single Django query为单个 Django 查询关闭 Postgres 并行查询
【发布时间】:2019-02-14 23:03:21
【问题描述】:

我在 Django 应用程序中有一个需要手动优化的查询。但是让查询快速运行意味着我需要能够告诉 Postgres“不要对此使用并行性”。

我认为可行的是:

from django.db import connection

cursor = connection.cursor()

# start a transaction so that PGBouncer runs the next statements
# on the same connection
cursor.execute("begin")

# turn off parallelism for this next query
cursor.execute("set max_parallel_workers_per_gather = 0")

# run my query
cursor.execute("HAND-TUNED SELECT QUERY GOES HERE")

# process the cursor results

# Put this connection back in the PGBouncer pool, and reset 
# max_parallel_workers_per_gather.
cursor.execute("rollback")

但它似乎不起作用。当我通过 Django 站点运行它时,我的查询继续显示在我的“慢查询”日志中,并且性能仍然很差(并行性为 4+ 秒,没有并行性为 0.5 秒)。

有没有办法做我需要做的事情?

【问题讨论】:

    标签: django postgresql query-optimization


    【解决方案1】:

    首先,您应该使用SET LOCAL,以便将效果限制在事务中。

    然后我建议您使用auto_explain 找出用​​于查询的实际计划。减速可能有不同的原因。

    【讨论】:

      猜你喜欢
      • 2016-10-23
      • 2020-07-13
      • 2018-10-19
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      • 2019-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多