0

我正在使用Django来实现一个工程管理系统。我在某处犯了一些非常错误的事情,并且我的 SQL 查询计数在某些页面上非常高。

例如,我在一个简单的 ListView 中获得了 95 到 98 个 SQL 查询。所有的查询都是一样的:

SELECT * FROM "syncoor_codification" LIMIT 21

它们总是返回相同的对象。我怀疑查询是由我的模型的 get_queryset() 函数触发的。

如果我使用 Django 调试工具栏,我可以看到查询是在模板内部触发的,如下所示:

{% extends 'syncoor/base.html' %}
{% extends 'syncoor/docs/base.html' %}
{% extends 'syncoor/docs/codifications/base.html' %}
{% include 'syncoor/js/jsp.js' %}

我怎样才能摆脱这种额外的开销?

编辑:这是一个截图:

针对 CSS 文件触发的查询

4

2 回答 2

1

In the picture you posted, the stack trace shows 2 things of note.

  1. The SQL calls are being performed inside a signal handler associated with django-debug-toolbar, specifically the TEMPLATE panel.

  2. The SQL call has it's limit clause set to REPR_OUTPUT_SIZE, Indicating that the template panel is trying to render a representation of a queryset.

This leads me to believe that you have passed a function or lazy object that returns a new queryset into the context at some point and it is this object that is being evaluated by the template panel. See if you can spot a bunch of Entity Objects in the context in the templates panel.

To confirm, try setting your settings like this.

DEBUG_TOOLBAR_PANELS = (
    'debug_toolbar.panels.version.VersionDebugPanel',
    'debug_toolbar.panels.timer.TimerDebugPanel',
    'debug_toolbar.panels.settings_vars.SettingsVarsDebugPanel',
    'debug_toolbar.panels.headers.HeaderDebugPanel',
    'debug_toolbar.panels.request_vars.RequestVarsDebugPanel',
   # 'debug_toolbar.panels.template.TemplateDebugPanel',
    'debug_toolbar.panels.sql.SQLDebugPanel',
    'debug_toolbar.panels.signals.SignalDebugPanel',
    'debug_toolbar.panels.logger.LoggingPanel',
)
于 2013-02-13T05:29:37.820 回答
-1

看起来您的base.html模板中正在运行正在访问数据库的循环。这就是为什么从这些模板继承会生成查询。如果没有看到模板本身,很难说出确切的问题是什么,但这肯定是您可以找到答案的地方。

于 2013-02-04T11:28:58.340 回答