{"title":"パフォーマンスと最適化","version":"6.0","locale":"ja","docname":"topics/performance","url":"/ja/6.0/topics/performance/","canonical":"https://djangodocs.dev/ja/6.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>最適化する「パフォーマンス」という言葉で何を意味しているのか、はっきりと考えておくことが大切です。パフォーマンスを測る指標は1つではないからです。</p>\n<p>高速化というものがプログラムのために最も明らかな目的かもしれません。しかし、他の観点からパフォーマンスを向上することもできます。たとえば、メモリー消費量を少なくするとか、データベースやネットワークのアクセス量を削減するといったことが考えられます。</p>\n<p>1つの領域を改善すると、別の領域の性能も向上することがよくありますが、常にそうとは限らず、一方が他方を犠牲にすることさえあります。たとえば、プログラムの速度の改善がメモリ使用量の増加を引き起こすかもしれません。さらに悪いことに、それが自滅的になるかもしれません。もし速度の改善がメモリを大幅に消費してしまったら、システム上のメモリが足りなくなり、結果的に利益よりも害を与えることになってしまうでしょう。</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=\"extlink-pypi reference external\" href=\"https://pypi.org/project/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<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</code></pre></div>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</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=\"Django template code\"><code><span class=\"cm\">&lt;!--</span>\n<span class=\"cm\">Django template filter</span>\n<span class=\"cm\">slower still, because it will have to count them in Python anyway,</span>\n<span class=\"cm\">and because of template language overheads</span>\n<span class=\"cm\">--&gt;</span>\n<span class=\"cp\">{{</span> <span class=\"nv\">my_bicycles</span><span class=\"o\">|</span><span class=\"nf\">length</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>通常、その仕事に最も適したレベルは、コードを書くのに快適な最も低いレベルのものです。</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">注釈</p>\n<p>上記の例は単なる例示に過ぎません</p>\n<p>実際のケースでは、まず数をカウントする前後で何が起こっているかを考慮し、 <em>その文脈において</em> 最適な方法を見つける必要があります。データベース最適化に関するドキュメントでは、 <a class=\"reference internal\" href=\"/ja/6.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\">QuerySet</span></code> の <code class=\"docutils literal notranslate\"><span class=\"pre\">count()</span></code> メソッドを呼び出す <code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">my_bicycles.count</span> <span class=\"pre\">}}</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=\"/ja/6.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=\"/ja/6.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=\"/ja/6.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=\"/ja/6.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>遅延 (laziness)</em> とは、キャッシュ機能を補完するもう1つの戦略です。キャッシュは結果を保存することで再計算を防ぎますが、遅延は実際に値が必要になるまで、計算の実行を遅らせます。</p>\n<p>遅延によりインスタンス化される前に、またはインスタンス化が可能になる以前であっても参照できるようになります。これにはさまざまな用途があります。</p>\n<p>たとえば、<a class=\"reference internal\" href=\"/ja/6.0/topics/i18n/translation/#lazy-translations\"><span class=\"std std-ref\">遅延翻訳</span></a> は対象となる言語自体が判別される前に使用できます。翻訳後の文字列がレンダリングされたテンプレートなどで実際に必要になるまで使用されなくなるためです。</p>\n<p>そもそも怠惰（Laziness）とは、仕事を避けることで労力を節約することでもあります。つまり、怠け癖の一面は、やらなければならないことがあるまで何もしないことです。そのため、怠け癖はパフォーマンスにも影響し、高価な仕事であればあるほど、怠け癖によって得られるものは大きくなります。</p>\n<p>Python provides a number of tools for lazy evaluation, particularly through the\n<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> and <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> constructs. It's worth\nreading up on laziness in Python to discover opportunities for making use of\nlazy patterns in your code.</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\">QuerySet</span></code> の評価が挙げられます。<a class=\"reference internal\" href=\"/ja/6.0/topics/db/queries/#querysets-are-lazy\"><span class=\"std std-ref\">QuerySet は遅延評価されます</span></a>。そのため、<code class=\"docutils literal notranslate\"><span class=\"pre\">QuerySet</span></code> は作成後、記述したアイテムを取得するためにデータベースへのアクセスを一切行うことなく、さまざまな場所に渡したり、他の <code class=\"docutils literal notranslate\"><span class=\"pre\">QuerySet</span></code> インスタンスと組み合わせることができます。渡されるのは <code class=\"docutils literal notranslate\"><span class=\"pre\">QuerySet</span></code> オブジェクトであって、最終的にはデータベースから取得されるアイテムのコレクションではないのです。</p>\n<p>他方で、<a class=\"reference internal\" href=\"/ja/6.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=\"/ja/6.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=\"/ja/6.0/topics/db/optimization/\"><span class=\"doc\">データベースアクセスの最適化ドキュメント</span></a> では、データベースの利用を最適化する際にとるべき手順を、大まかにいくつかの見出しとしてまとめています。それぞれの見出しの下に、関連するドキュメントへのリンクを集約した上で、さまざまなヒントを追加しています。</p>\n</section>\n<section id=\"other-database-related-tips\">\n<h3>その他のデータベース関連の tips<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><a class=\"reference internal\" href=\"/ja/6.0/ref/databases/#persistent-database-connections\"><span class=\"std std-ref\">持続的 (persistent) な接続</span></a> を有効化すると、リクエストの処理時間のかなりの部分を占めるデータベースへのコネクションが高速化できます。</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>ミドルウェア (Middleware)<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=\"/ja/6.0/ref/middleware/\"><span class=\"doc\">ミドルウェア</span></a> を用意しています。以下のようなものがあります。</p>\n<section id=\"conditionalgetmiddleware\">\n<h4><a class=\"reference internal\" href=\"/ja/6.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 するためのサポートを追加します。</p>\n</section>\n<section id=\"gzipmiddleware\">\n<h4><a class=\"reference internal\" href=\"/ja/6.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=\"/ja/6.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=\"/ja/6.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=\"/ja/6.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=\"/ja/6.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=\"/ja/6.0/ref/contrib/staticfiles/\"><span class=\"doc\">静的ファイル</span></a> のファイル名に内容依存のタグを追加します。ファイルが変更されるとタグも変更されるため、ブラウザは自動的にアセットをリロードします。</p>\n</section>\n<section id=\"minification\">\n<h4>&quot;Minification&quot;<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 を最小化する (&quot;minify&quot;) 機能を提供します。これらは不要なホワイトスペースや改行、コメントを取り除き、変数名を短縮することで、サイトが公開するドキュメントのサイズを削減してくれます。</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=\"/ja/6.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\">cached</span> <span class=\"pre\">template</span> <span class=\"pre\">loader</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\">注釈</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\">注釈</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> を使うと、パフォーマンス、特に速度の向上が得られる可能性があります。</p>\n<p>代替テンプレートシステムは、Django のテンプレート言語をどの程度共有するかによって異なります。</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">注釈</p>\n<p>テンプレートでパフォーマンスの問題が発生した場合、最初に行うべきことは、その理由を正確に理解することです。代替のテンプレートシステムを使用することで速度が向上するかもしれませんが、その手間をかけずに同じ利益を得ることも可能です。たとえば、テンプレート内の高コストな処理やロジックは、ビュー内でより効率的に行うことができます。</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 実装です ('標準の' Python 実装は C で書かれています)。heavyweight なアプリケーションの場合、PyPy を使用するとパフォーマンスが大幅に向上する可能性があります。</p>\n<p>A key aim of the PyPy project is <a class=\"reference external\" href=\"https://www.pypy.org/compat.html\">compatibility</a> with existing Python APIs and libraries.\nDjango is compatible with versions of PyPy corresponding to the supported\nPython versions, but you will need to check the compatibility of other\nlibraries you rely on.</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":"その他のデータベース関連の tips","anchor":"other-database-related-tips","children":[]}]},{"title":"HTTP のパフォーマンス","anchor":"http-performance","children":[{"title":"ミドルウェア (Middleware)","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":"\"Minification\"","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":"/ja/6.0/topics/"}],"prev":{"docname":"topics/security","title":"Django におけるセキュリティ","url":"/ja/6.0/topics/security/"},"next":{"docname":"topics/serialization","title":"Django オブジェクトのシリアライズ","url":"/ja/6.0/topics/serialization/"},"formats":{"html":"/ja/6.0/topics/performance/","markdown":"/ja/6.0/topics/performance.md","json":"/ja/6.0/topics/performance.json"},"source":"https://github.com/django/django/blob/stable/6.0.x/docs/topics/performance.txt","official":"https://docs.djangoproject.com/ja/6.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","1.11","1.10","1.9"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}