【发布时间】: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