{"title":"性能和优化","version":"4.0","locale":"zh-hans","docname":"topics/performance","url":"/zh-hans/4.0/topics/performance/","canonical":"https://djangodocs.dev/zh-hans/4.0/topics/performance/","summary":"本文档概述了一些技术和工具，这些技术和工具可以帮助您更有效地运行Django代码——更快，并且使用更少的系统资源。 介绍 Link to this heading # 通常，首先要考虑的是编写 能工作的 代码，它的逻辑功能与产生预期输出所需的逻辑功能相同。然而，有时，这并不足以使代码像人们所希望的那样 高效地 工作。…","html":"<h1>性能和优化<a class=\"heading-anchor\" href=\"#performance-and-optimization\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>本文档概述了一些技术和工具，这些技术和工具可以帮助您更有效地运行Django代码——更快，并且使用更少的系统资源。</p>\n<section id=\"introduction\">\n<h2>介绍<a class=\"heading-anchor\" href=\"#introduction\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>通常，首先要考虑的是编写 <em>能工作的</em> 代码，它的逻辑功能与产生预期输出所需的逻辑功能相同。然而，有时，这并不足以使代码像人们所希望的那样 <em>高效地</em> 工作。</p>\n<p>在这种情况下，需要的是一些东西——在实践中，通常是一组东西——以提高代码的性能，而不影响或只影响其行为。</p>\n</section>\n<section id=\"general-approaches\">\n<h2>一般方法<a class=\"heading-anchor\" href=\"#general-approaches\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"what-are-you-optimizing-for\">\n<h3>你在为 <em>什么</em> 而优化？<a class=\"heading-anchor\" href=\"#what-are-you-optimizing-for\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>清楚地理解你所说的“绩效”是什么很重要，因为它不仅仅是一个指标。</p>\n<p>提高速度可能是程序最明显的目标，但有时可能会寻求其他性能改进，例如降低内存消耗或减少对数据库或网络的要求。</p>\n<p>一个领域的改进通常会提高另一个领域的性能，但并不总是如此；有时甚至会牺牲另一个领域的性能。例如，一个程序速度的提高可能会导致它使用更多的内存。更糟糕的是，如果速度提高太过内存不足，以致于系统开始耗尽内存，那么你所做的弊大于利。</p>\n<p>还有其他的权衡。你自己的时间是一个宝贵的资源，比CPU时间更宝贵。一些改进可能太难实现，或者可能影响代码的可移植性或可维护性。并非所有的性能改进都值得付出努力。</p>\n<p>所以，你需要知道你的目标是什么样的性能改进，你也需要知道你有一个很好的理由去瞄准那个方向——而且你需要：</p>\n</section>\n<section id=\"performance-benchmarking\">\n<h3>性能标竿<a class=\"heading-anchor\" href=\"#performance-benchmarking\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>仅仅猜测或假设代码中存在效率低下的原因是没有好处的。</p>\n<section id=\"django-tools\">\n<h4>Django工具<a class=\"heading-anchor\" href=\"#django-tools\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p><a class=\"reference external\" href=\"https://github.com/jazzband/django-debug-toolbar/\">django-debug-toolbar</a> 是一个非常方便的工具，它可以深入了解你的代码正在做什么以及花费了多少时间。特别是它可以显示您的页面生成的所有 SQL 查询，以及每个查询所用的时间。</p>\n<p>第三方面板也可用于工具栏，可以（例如）报告缓存性能和模板呈现时间。</p>\n</section>\n<section id=\"third-party-services\">\n<h4>第三方服务<a class=\"heading-anchor\" href=\"#third-party-services\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>有许多免费服务可以从远程 HTTP 客户端的角度分析并报告你站点页面的性能，从而有效地模拟实际用户的体验。</p>\n<p>它们不能报告代码的内部情况，但可以提供站点总体性能的有用见解，包括在 Django 环境中无法充分衡量的方面。示例包括：</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference external\" href=\"http://yslow.org/\">Yahoo's Yslow</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://developers.google.com/speed/\">Google PageSpeed</a></p></li>\n</ul>\n<p>还有一些付费服务可以执行类似的分析，包括一些支持 Django 的服务，可以与你的代码库集成以更全面地分析其性能。</p>\n</section>\n</section>\n<section id=\"get-things-right-from-the-start\">\n<h3>从一开始就把事情做好<a class=\"heading-anchor\" href=\"#get-things-right-from-the-start\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>优化中的一些工作涉及到解决性能缺陷，但有些工作可以包括在你将要做的事情中，作为甚至在你开始考虑提高性能之前就应该采用的良好实践的一部分。</p>\n<p>在这方面，Python 是一种优秀的语言，因为外观优美且感觉正确的解决方案通常是性能最好的解决方案。 与大多数技能一样，学习“看起来正确”的内容需要练习，但是最有用的准则之一是：</p>\n<section id=\"work-at-the-appropriate-level\">\n<h4>在适当的级别工作<a class=\"heading-anchor\" href=\"#work-at-the-appropriate-level\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Django 提供了许多不同的方法来处理事情，但仅仅因为它可以用某种方式来做某件事，并不意味着它是最合适的方式。例如，你可能会发现可以在 <code class=\"docutils literal notranslate\"><span class=\"pre\">QuerySet</span></code>、Python 或模板中计算相同的东西——集合中的项目数。</p>\n<p>但是，在较低级别而不是较高级别进行此工作几乎总是会更快。 在更高级别上，系统必须通过多层抽象和更多机器层来处理对象。</p>\n<p>也就是说，数据库通常比 Python 快，Python 比模板语言快：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"c1\"># QuerySet operation on the database</span>\n<span class=\"c1\"># fast, because that&#39;s what databases are good at</span>\n<span class=\"n\">my_bicycles</span><span class=\"o\">.</span><span class=\"n\">count</span><span class=\"p\">()</span>\n\n<span class=\"c1\"># counting Python objects</span>\n<span class=\"c1\"># slower, because it requires a database query anyway, and processing</span>\n<span class=\"c1\"># of the Python objects</span>\n<span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">my_bicycles</span><span class=\"p\">)</span>\n\n<span class=\"c1\"># Django template filter</span>\n<span class=\"c1\"># slower still, because it will have to count them in Python anyway,</span>\n<span class=\"c1\"># and because of template language overheads</span>\n<span class=\"p\">{{</span> <span class=\"n\">my_bicycles</span><span class=\"o\">|</span><span class=\"n\">length</span> <span class=\"p\">}}</span>\n</code></pre></div>\n<p>一般来说，最适合工作的级别是能够舒服编写代码的最低级别。</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>上面的示例仅是说明性的。</p>\n<p>首先，在实际案例中你需要考虑你的计数前后发生了什么，以确定 <em>在特定情况下</em> 的最佳做法。数据库优化文档描述了 <a class=\"reference internal\" href=\"/zh-hans/4.0/topics/db/optimization/#overuse-of-count-and-exists\"><span class=\"std std-ref\">在模板中计数会更好的一种情况</span></a>。</p>\n<p>其次，还有其他的选择可以考虑：在现实生活中，<code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">my_bicycles.count</span> <span class=\"pre\">}}</span></code>，它直接从模板调用 <code class=\"docutils literal notranslate\"><span class=\"pre\">QuerySet</span></code> 的 <code class=\"docutils literal notranslate\"><span class=\"pre\">count()</span></code> 方法，可能是最合适的选择。</p>\n</aside>\n</section>\n</section>\n</section>\n<section id=\"caching\">\n<h2>缓存<a class=\"heading-anchor\" href=\"#caching\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>通常情况下，计算一个值是很昂贵的（也就是耗费资源，而且速度很慢），所以把这个值保存到快速访问的缓存中，为下次需要时做好准备，会有巨大的好处。</p>\n<p>Django 具有完善的缓存框架以及其他较小的缓存功能，这是一项非常重要且功能强大的技术。</p>\n<section id=\"the-caching-framework\">\n<h3><a class=\"reference internal\" href=\"/zh-hans/4.0/topics/cache/\"><span class=\"doc\">缓存框架</span></a><a class=\"heading-anchor\" href=\"#the-caching-framework\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Django 的 <a class=\"reference internal\" href=\"/zh-hans/4.0/topics/cache/\"><span class=\"doc\">缓存框架</span></a> 通过保存动态内容使其不需要为每个请求计算，从而为性能提升提供了非常重要的机会。</p>\n<p>为了方便起见，Django 提供了不同级别的缓存粒度：你可以缓存特定视图的输出，或者只缓存难以生成的部分，甚至可以缓存整个站点。</p>\n<p>实施缓存不应该被看作是改进那些因为写得不好而表现不佳的代码的一种替代方法。它是产生性能良好的代码的最终步骤之一，而不是一条捷径。</p>\n</section>\n<section id=\"cached-property\">\n<h3><a class=\"reference internal\" href=\"/zh-hans/4.0/ref/utils/#django.utils.functional.cached_property\" title=\"django.utils.functional.cached_property\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">cached_property</span></code></a><a class=\"heading-anchor\" href=\"#cached-property\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>通常必须多次调用一个类实例的方法。 如果该方法很昂贵，那么这样做会很浪费。</p>\n<p>使用 <a class=\"reference internal\" href=\"/zh-hans/4.0/ref/utils/#django.utils.functional.cached_property\" title=\"django.utils.functional.cached_property\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">cached_property</span></code></a> 装饰器保存属性返回的值； 下次在该实例上调用该函数时，它将返回保存的值，而不是重新计算它。 请注意，这仅适用于将 <code class=\"docutils literal notranslate\"><span class=\"pre\">self</span></code> 作为唯一参数的方法，并将该方法更改为属性。</p>\n<p>某些 Django 组件也具有自己的缓存功能； 这些将在下面与那些组件相关的部分中讨论。</p>\n</section>\n</section>\n<section id=\"understanding-laziness\">\n<h2>理解惰性<a class=\"heading-anchor\" href=\"#understanding-laziness\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p><em>惰性</em> 是一种与缓存互补的策略。 缓存通过保存结果来避免重新计算； 惰性会延迟计算，直到真正需要它为止。</p>\n<p>惰性允许我们在事物被实例化之前，甚至在有可能实例化之前，就对其进行引用。这有很多用途。</p>\n<p>例如，<a class=\"reference internal\" href=\"/zh-hans/4.0/topics/i18n/translation/#lazy-translations\"><span class=\"std std-ref\">惰性翻译</span></a> 可以在甚至不知道目标语言之前就使用，因为它直到真正需要翻译后的字符串（例如在渲染的模板中）时才发生。</p>\n<p>惰性也是一种省力的方法，它首先要避免工作。也就是说，惰性的一个方面是在必须做的时候才做任何事情，因为它可能最终不是必须的。因此，惰性可能有性能影响，相关工作的成本越高，从惰性中获得的收益就越多。</p>\n<p>Python 提供了许多用于惰性求值的工具，特别是通过 <a class=\"reference external\" href=\"https://docs.python.org/3/glossary.html#term-generator\" title=\"(in Python v3.14)\"><span class=\"xref std std-term\">generator</span></a> 和 <a class=\"reference external\" href=\"https://docs.python.org/3/glossary.html#term-generator-expression\" title=\"(in Python v3.14)\"><span class=\"xref std std-term\">generator expression</span></a> 构造。值得阅读 Python 的惰性，以发现在代码中利用惰性模式的机会。</p>\n<section id=\"laziness-in-django\">\n<h3>Django 中的惰性<a class=\"heading-anchor\" href=\"#laziness-in-django\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Django 本身就很惰性。一个很好的例子可以在 <code class=\"docutils literal notranslate\"><span class=\"pre\">QuerySets</span></code> 的计算中找到。<a class=\"reference internal\" href=\"/zh-hans/4.0/topics/db/queries/#querysets-are-lazy\"><span class=\"std std-ref\">QuerySets 是懒惰的</span></a>。因此，一个 <code class=\"docutils literal notranslate\"><span class=\"pre\">QuerySet</span></code> 以被创建、传递，并与其他 <code class=\"docutils literal notranslate\"><span class=\"pre\">QuerySets</span></code> 组合，而不需要实际到数据库中去获取它所描述的项目。被传来传去的是 <code class=\"docutils literal notranslate\"><span class=\"pre\">QuerySet</span></code> 对象，而不是最终需要从数据库中获取的项目集合。</p>\n<p>另一方面，<a class=\"reference internal\" href=\"/zh-hans/4.0/ref/models/querysets/#when-querysets-are-evaluated\"><span class=\"std std-ref\">某些操作将强制计算 QuerySet</span></a>。避免过早地对 <code class=\"docutils literal notranslate\"><span class=\"pre\">QuerySet</span></code> 进行计算，可以节省对数据库进行一次昂贵而不必要的访问。</p>\n<p>Django 还提供了一个 <a class=\"reference internal\" href=\"/zh-hans/4.0/ref/utils/#django.utils.functional.keep_lazy\" title=\"django.utils.functional.keep_lazy\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">keep_lazy()</span></code></a> 这允许使用惰性参数调用的函数本身表现为惰性，仅在需要时才进行计算。因此，懒惰参数——可能是一个昂贵的参数——在真正需要的时候才会被调用计算。</p>\n</section>\n</section>\n<section id=\"databases\">\n<h2>数据库<a class=\"heading-anchor\" href=\"#databases\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"database-optimization\">\n<h3>数据库优化<a class=\"heading-anchor\" href=\"#database-optimization\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Django 的数据库层提供了多种方法来帮助开发者从其数据库中获得最佳性能。<a class=\"reference internal\" href=\"/zh-hans/4.0/topics/db/optimization/\"><span class=\"doc\">数据库优化文档</span></a> 收集了相关文档的链接，并添加了各种技巧，概述了尝试优化数据库使用率时要采取的步骤。</p>\n</section>\n<section id=\"other-database-related-tips\">\n<h3>其他与数据库相关的技巧<a class=\"heading-anchor\" href=\"#other-database-related-tips\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>启用 <span class=\"xref std std-ref\">持久数据库连接</span> 可以在大部分请求处理时间中加快与数据库帐户的连接。</p>\n<p>例如，这对网络性能有限的虚拟主机有很大帮助。</p>\n</section>\n</section>\n<section id=\"http-performance\">\n<h2>HTTP 性能<a class=\"heading-anchor\" href=\"#http-performance\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"middleware\">\n<h3>中间件<a class=\"heading-anchor\" href=\"#middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Django自带了一些有用的 <a class=\"reference internal\" href=\"/zh-hans/4.0/ref/middleware/\"><span class=\"doc\">中间件</span></a> ，可以帮助优化你的网站的性能。它们包括：</p>\n<section id=\"conditionalgetmiddleware\">\n<h4><a class=\"reference internal\" href=\"/zh-hans/4.0/ref/middleware/#django.middleware.http.ConditionalGetMiddleware\" title=\"django.middleware.http.ConditionalGetMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ConditionalGetMiddleware</span></code></a><a class=\"heading-anchor\" href=\"#conditionalgetmiddleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>增加对现代浏览器的支持，根据 <code class=\"docutils literal notranslate\"><span class=\"pre\">ETag</span></code> 和 <code class=\"docutils literal notranslate\"><span class=\"pre\">Last-Modified</span></code> 头有条件的 GET 响应。如果需要，它还可以计算和设置 ETag。</p>\n</section>\n<section id=\"gzipmiddleware\">\n<h4><a class=\"reference internal\" href=\"/zh-hans/4.0/ref/middleware/#django.middleware.gzip.GZipMiddleware\" title=\"django.middleware.gzip.GZipMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">GZipMiddleware</span></code></a><a class=\"heading-anchor\" href=\"#gzipmiddleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>压缩对所有现代浏览器的响应，节省带宽和传输时间。请注意，GZipMiddleware 目前被认为是一个安全风险，容易受到攻击，使TLS／SSL提供的保护失效。更多信息请参见 <a class=\"reference internal\" href=\"/zh-hans/4.0/ref/middleware/#django.middleware.gzip.GZipMiddleware\" title=\"django.middleware.gzip.GZipMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">GZipMiddleware</span></code></a> 中的警告。</p>\n</section>\n</section>\n<section id=\"sessions\">\n<h3>会话<a class=\"heading-anchor\" href=\"#sessions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<section id=\"using-cached-sessions\">\n<h4>使用缓存会话<a class=\"heading-anchor\" href=\"#using-cached-sessions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p><a class=\"reference internal\" href=\"/zh-hans/4.0/topics/http/sessions/#cached-sessions-backend\"><span class=\"std std-ref\">使用缓存会话</span></a> 可能是一种提高性能的方法，因为不需要从数据库等较慢的存储源加载会话数据，而是将经常使用的会话数据存储在内存中。</p>\n</section>\n</section>\n<section id=\"static-files\">\n<h3>静态文件<a class=\"heading-anchor\" href=\"#static-files\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>静态文件，根据定义不是动态的，是优化增益的理想目标。</p>\n<section id=\"manifeststaticfilesstorage\">\n<h4><a class=\"reference internal\" href=\"/zh-hans/4.0/ref/contrib/staticfiles/#django.contrib.staticfiles.storage.ManifestStaticFilesStorage\" title=\"django.contrib.staticfiles.storage.ManifestStaticFilesStorage\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ManifestStaticFilesStorage</span></code></a><a class=\"heading-anchor\" href=\"#manifeststaticfilesstorage\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>通过利用网络浏览器的缓存功能，你可以在初始下载后完全消除对给定文件的网络访问。</p>\n<p><a class=\"reference internal\" href=\"/zh-hans/4.0/ref/contrib/staticfiles/#django.contrib.staticfiles.storage.ManifestStaticFilesStorage\" title=\"django.contrib.staticfiles.storage.ManifestStaticFilesStorage\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ManifestStaticFilesStorage</span></code></a> 为 <a class=\"reference internal\" href=\"/zh-hans/4.0/ref/contrib/staticfiles/\"><span class=\"doc\">静态文件</span></a> 的文件名后附加一个与内容相关的标记，使浏览器可以安全地长期缓存这些文件，而不会丢失将来的更改——当文件更改时，标记也会更改，因此浏览器将自动重新加载文件。</p>\n</section>\n<section id=\"minification\">\n<h4>“最小化”<a class=\"heading-anchor\" href=\"#minification\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>一些第三方 Django 工具和包提供了“最小化”HTML、CSS 和 JavaScript 的能力。它们删除不必要的空白、换行符和注释，并缩短变量名，从而减小站点发布的文档的大小。</p>\n</section>\n</section>\n</section>\n<section id=\"template-performance\">\n<h2>模板性能<a class=\"heading-anchor\" href=\"#template-performance\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>注意：</p>\n<ul class=\"simple\">\n<li><p>使用 <code class=\"docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">block</span> <span class=\"pre\">%}</span></code> 比使用 <code class=\"docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">include</span> <span class=\"pre\">%}</span></code> 快</p></li>\n<li><p>由许多小块组装而成的严重碎片化的模板会影响性能</p></li>\n</ul>\n<section id=\"the-cached-template-loader\">\n<h3>缓存的模板加载器<a class=\"heading-anchor\" href=\"#the-cached-template-loader\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>启用 <a class=\"reference internal\" href=\"/zh-hans/4.0/ref/templates/api/#django.template.loaders.cached.Loader\" title=\"django.template.loaders.cached.Loader\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">缓存的模板加载器</span></code></a> 通常会大幅提高性能，因为它避免了每次需要渲染时都编译模板。</p>\n</section>\n</section>\n<section id=\"using-different-versions-of-available-software\">\n<h2>使用现有软件的不同版本<a class=\"heading-anchor\" href=\"#using-different-versions-of-available-software\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>有时不妨检查一下你所使用的软件是否有不同的、性能更好的版本。</p>\n<p>这些技术是针对那些想要提升已经充分优化的 Django 网站性能的高级用户。</p>\n<p>然而，它们并不是解决性能问题的灵丹妙药，它们也不太可能给那些还没有正确完成更基本的事情的网站带来比边际收益更好的收益。</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>值得重复的是：<strong>寻找你已经在使用的软件的替代品从来不是解决性能问题的第一个办法</strong>。当达到此优化级别时，你需要一个正式的基准测试解决方案。</p>\n</aside>\n<section id=\"newer-is-often-but-not-always-better\">\n<h3>较新的往往是——但并不总是——更好。<a class=\"heading-anchor\" href=\"#newer-is-often-but-not-always-better\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>维护良好的软件的新版本效率较低是相当罕见的，但维护者不可能预料到每一个可能的用例——所以要注意新版本可能会有更好的表现，但不要假设它们总是会有更好的表现。</p>\n<p>Django 本身也是如此。 后续的发行版对整个系统进行了许多改进，但是你仍应检查应用程序的实际性能，因为在某些情况下，你可能会发现更改意味着性能较差而不是更好。</p>\n<p>Python 的新版本，以及 Python 包的新版本，通常也会有更好的表现——但要衡量，而不是假设。</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>除非你在特定版本中遇到了异常的性能问题，否则你通常会在新版本中找到更好的特性、可靠性和安全性，而且这些好处远比你可能赢得或失去的任何性能都重要。</p>\n</aside>\n</section>\n<section id=\"alternatives-to-django-s-template-language\">\n<h3>Django 模板语言的替代方案<a class=\"heading-anchor\" href=\"#alternatives-to-django-s-template-language\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>对于几乎所有的情况来说，Django 内置的模板语言是完全足够的。然而，如果你的 Django 项目中的瓶颈似乎在于模板系统，而你已经用尽了其他机会来补救，那么第三方的替代方案可能就是答案。</p>\n<p><a class=\"reference external\" href=\"https://jinja.palletsprojects.com/\">Jinja2</a> can offer performance improvements, particularly when it comes to\nspeed.</p>\n<p>其他模板系统在共享 Django 模板语言的程度上有所不同。</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p><em>如果</em> 你在模板中遇到性能问题，首先要做的是了解具体原因。使用替代的模板系统可能会更快，但同样的收益也可能不费吹灰之力，例如，模板中昂贵的处理和逻辑可以在视图中更有效地完成。</p>\n</aside>\n</section>\n<section id=\"alternative-software-implementations\">\n<h3>替代软件实现<a class=\"heading-anchor\" href=\"#alternative-software-implementations\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>也许值得检查一下你正在使用的 Python 软件是否已经提供了不同的实现，可以更快地执行同样的代码。</p>\n<p>然而：大多数写得好的 Django 网站的性能问题并不在 Python 执行层面，而是在低效的数据库查询、缓存和模板上。如果你依赖于编写得不好的 Python 代码，你的性能问题不可能通过让它执行得更快来解决。</p>\n<p>使用替代实现可能会带来兼容性、部署、可移植性或维护问题。不言而喻，在采用非标准实现之前，你应该确保它为你的应用程序提供足够的性能收益，以抵消潜在的风险。</p>\n<p>有了这些注意事项，你就应该知道：</p>\n<section id=\"id1\">\n<h4><a class=\"reference external\" href=\"https://www.pypy.org/\">PyPy</a><a class=\"heading-anchor\" href=\"#id1\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p><a class=\"reference external\" href=\"https://www.pypy.org/\">PyPy</a> 是 Python 本身的一个实现（“标准” Python 实现是用 C 语言）。PyPy 可以提供可观的性能提升，通常用于重量级应用。</p>\n<p>PyPy 项目的一个关键目标是 <a class=\"reference external\" href=\"https://www.pypy.org/compat.html\">与现有的 Python API 和库兼容</a> 。Django 是兼容的，但你需要检查你依赖的其他库的兼容性。</p>\n</section>\n<section id=\"c-implementations-of-python-libraries\">\n<h4>Python 库的 C 语言实现<a class=\"heading-anchor\" href=\"#c-implementations-of-python-libraries\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>有些 Python 库也是用 C 语言实现的，速度可以快得多。他们的目标是提供相同的 API。请注意，兼容性问题和行为差异并非未知（也并非总是立竿见影的）。</p>\n</section>\n</section>\n</section>","rootId":"performance-and-optimization","toc":[{"title":"介绍","anchor":"introduction","children":[]},{"title":"一般方法","anchor":"general-approaches","children":[{"title":"你在为 什么 而优化？","anchor":"what-are-you-optimizing-for","children":[]},{"title":"性能标竿","anchor":"performance-benchmarking","children":[{"title":"Django工具","anchor":"django-tools","children":[]},{"title":"第三方服务","anchor":"third-party-services","children":[]}]},{"title":"从一开始就把事情做好","anchor":"get-things-right-from-the-start","children":[{"title":"在适当的级别工作","anchor":"work-at-the-appropriate-level","children":[]}]}]},{"title":"缓存","anchor":"caching","children":[{"title":"缓存框架","anchor":"the-caching-framework","children":[]},{"title":"cached_property","anchor":"cached-property","children":[]}]},{"title":"理解惰性","anchor":"understanding-laziness","children":[{"title":"Django 中的惰性","anchor":"laziness-in-django","children":[]}]},{"title":"数据库","anchor":"databases","children":[{"title":"数据库优化","anchor":"database-optimization","children":[]},{"title":"其他与数据库相关的技巧","anchor":"other-database-related-tips","children":[]}]},{"title":"HTTP 性能","anchor":"http-performance","children":[{"title":"中间件","anchor":"middleware","children":[{"title":"ConditionalGetMiddleware","anchor":"conditionalgetmiddleware","children":[]},{"title":"GZipMiddleware","anchor":"gzipmiddleware","children":[]}]},{"title":"会话","anchor":"sessions","children":[{"title":"使用缓存会话","anchor":"using-cached-sessions","children":[]}]},{"title":"静态文件","anchor":"static-files","children":[{"title":"ManifestStaticFilesStorage","anchor":"manifeststaticfilesstorage","children":[]},{"title":"“最小化”","anchor":"minification","children":[]}]}]},{"title":"模板性能","anchor":"template-performance","children":[{"title":"缓存的模板加载器","anchor":"the-cached-template-loader","children":[]}]},{"title":"使用现有软件的不同版本","anchor":"using-different-versions-of-available-software","children":[{"title":"较新的往往是——但并不总是——更好。","anchor":"newer-is-often-but-not-always-better","children":[]},{"title":"Django 模板语言的替代方案","anchor":"alternatives-to-django-s-template-language","children":[]},{"title":"替代软件实现","anchor":"alternative-software-implementations","children":[{"title":"PyPy","anchor":"id1","children":[]},{"title":"Python 库的 C 语言实现","anchor":"c-implementations-of-python-libraries","children":[]}]}]}],"breadcrumbs":[{"docname":"topics/index","title":"使用 Django","url":"/zh-hans/4.0/topics/"}],"prev":{"docname":"topics/security","title":"Django 的安全性","url":"/zh-hans/4.0/topics/security/"},"next":{"docname":"topics/serialization","title":"序列化 Django 对象","url":"/zh-hans/4.0/topics/serialization/"},"formats":{"html":"/zh-hans/4.0/topics/performance/","markdown":"/zh-hans/4.0/topics/performance.md","json":"/zh-hans/4.0/topics/performance.json"},"source":"https://github.com/django/django/blob/stable/4.0.x/docs/topics/performance.txt","official":"https://docs.djangoproject.com/zh-hans/4.0/topics/performance/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}