{"title":"Optamú ar rochtain bunachar","version":"6.0","locale":"ga","docname":"topics/db/optimization","url":"/ga/6.0/topics/db/optimization/","canonical":"https://djangodocs.dev/ga/6.0/topics/db/optimization/","summary":"Soláthraíonn ciseal bunachar sonraí Django bealaí éagsúla chun cabhrú le forbróirí an leas is fearr a bhaint as a mbunachar Bailíonn an doiciméad seo naisc…","html":"<h1>Optamú ar rochtain bunachar<a class=\"heading-anchor\" href=\"#database-access-optimization\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Soláthraíonn ciseal bunachar sonraí Django bealaí éagsúla chun cabhrú le forbróirí an leas is fearr a bhaint as a mbunachar Bailíonn an doiciméad seo naisc leis an doiciméad ábhartha le chéile, agus cuireann sé leideanna éagsúla leis, eagraithe faoi roinnt ceannteidil a thugann cur síos ar na céimeanna atá le glacadh agus tú ag iarraidh úsáid do bhunachar sonraí a</p>\n<section id=\"profile-first\">\n<h2>Próifíl ar dtús<a class=\"heading-anchor\" href=\"#profile-first\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Mar chleachtas cláraithe ginearálta, téann sé seo gan rá. &lt;faq-see-raw-sql-queries&gt;Faigh amach: ref: cad iad na ceisteanna atá á dhéanamh agat agus cad a chosnaíonn siad duit `. Úsáid: Meth: `.querySet.explain chun tuiscint a fháil ar cé chomh sonrach a fhorghníomhaíonn do bhunachar sonraí <code class=\"docutils literal notranslate\">QuerySet</code>s. B'fhéidir gur mhaith leat tionscadal seachtrach a úsáid freisin cosúil:pypi: django-debug-toolbar, nó uirlis a dhéanann monatóireacht díreach ar do bhunachar sonraí.</p>\n<p>Cuimhnigh go bhféadfadh tú a bheith ag barrfheabhsú le haghaidh luas nó cuimhne nó araon, ag brath ar do riachtanais Uaireanta beidh barrfheabhsú do cheann amháin díobhálach don cheann eile, ach uaireanta cabhróidh siad lena chéile. Chomh maith leis sin, b'fhéidir nach mbeadh an costas céanna (duitse) ag an obair a dhéanann an próiseas bunachar sonraí leis an méid céanna oibre a dhéantar i do phróiseas Python. Is fútsa atá sé cinneadh a dhéanamh cad iad do thosaíochtaí, cá gcaithfidh an t-iarmhéid a bheith, agus iad seo go léir a phróifíliú de réir mar is gá ós rud é go mbeidh sé seo ag brath ar d'iarratas agus ar d'fhre</p>\n<p>Le gach rud a leanann, cuimhnigh próifíl a dhéanamh tar éis gach athraithe chun a chinntiú gur sochar é an t-athrú, agus sochar mór go leor i bhfianaise an laghdú ar inléiteacht do chód. ** Tagann gach ceann de na moltaí thíos leis an rabhadh go bhféadfadh an prionsabal ginearálta a bheith i bhfeidhm i do chúinsí, nó go bhféadfadh sé a aisiompú fiú.</p>\n</section>\n<section id=\"use-standard-db-optimization-techniques\">\n<h2>Bain úsáid as teicnící caighdeánacha<a class=\"heading-anchor\" href=\"#use-standard-db-optimization-techniques\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>... lena n-áirítear:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference external\" href=\"https://en.wikipedia.org/wiki/Database_index\">Indexes</a>. This is a number one priority, <em>after</em> you have determined from\nprofiling what indexes should be added. Use\n<a class=\"reference internal\" href=\"/ga/6.0/ref/models/options/#django.db.models.Options.indexes\" title=\"django.db.models.Options.indexes\"><code class=\"xref py py-attr docutils literal notranslate\">Meta.indexes</code></a> or\n<a class=\"reference internal\" href=\"/ga/6.0/ref/models/fields/#django.db.models.Field.db_index\" title=\"django.db.models.Field.db_index\"><code class=\"xref py py-attr docutils literal notranslate\">Field.db_index</code></a> to add these from\nDjango. Consider adding indexes to fields that you frequently query using\n<a class=\"reference internal\" href=\"/ga/6.0/ref/models/querysets/#django.db.models.query.QuerySet.filter\" title=\"django.db.models.query.QuerySet.filter\"><code class=\"xref py py-meth docutils literal notranslate\">filter()</code></a>,\n<a class=\"reference internal\" href=\"/ga/6.0/ref/models/querysets/#django.db.models.query.QuerySet.exclude\" title=\"django.db.models.query.QuerySet.exclude\"><code class=\"xref py py-meth docutils literal notranslate\">exclude()</code></a>,\n<a class=\"reference internal\" href=\"/ga/6.0/ref/models/querysets/#django.db.models.query.QuerySet.order_by\" title=\"django.db.models.query.QuerySet.order_by\"><code class=\"xref py py-meth docutils literal notranslate\">order_by()</code></a>, etc. as indexes may help\nto speed up lookups. Note that determining the best indexes is a complex\ndatabase-dependent topic that will depend on your particular application.\nThe overhead of maintaining an index may outweigh any gains in query speed.</p></li>\n</ul>\n<ul class=\"simple\">\n<li><p>Úsáid chuí de chineálacha réimse.</p></li>\n</ul>\n<p>Glacfaimid leis go ndearna tú na rudaí atá liostaithe thuas. Díríonn an chuid eile den doiciméad seo ar conas Django a úsáid sa chaoi nach bhfuil tú ag déanamh obair gan ghá. Ní thugann an doiciméad seo aghaidh freisin ar theicnící optamaithe eile a bhaineann le gach oibríocht daor, mar shampla:doc: taiscáil cuspóir ginear&lt;/topics/cache&gt; álta.</p>\n</section>\n<section id=\"understand-querysets\">\n<h2>Tuiscint a fháil ar <code class=\"docutils literal notranslate\">QuerySet</code>s<a class=\"heading-anchor\" href=\"#understand-querysets\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Tuiscint: doc: Tá QuerySets &lt;/ref/models/querysets&gt; ríthábhachtach chun feidhmíocht mhaith a fháil le cód simplí. Go háirithe:</p>\n<section id=\"understand-queryset-evaluation\">\n<h3>Meastóireacht <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">``</span></a>QuerySet <a href=\"#id3\"><span class=\"problematic\" id=\"id4\">``</span></a>a thuiscint<a class=\"heading-anchor\" href=\"#understand-queryset-evaluation\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Chun fadhbanna feidhmíochta a sheachaint, tá sé tábhachtach tuiscint:</p>\n<ul class=\"simple\">\n<li><p>&lt;querysets-are-lazy&gt;that:ref: <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">`</span></a>Tá ceisteanna leisciúil <a href=\"#id3\"><span class=\"problematic\" id=\"id4\">`</span></a>.</p></li>\n<li><p>&lt;when-querysets-are-evaluated&gt;when: ref: <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">`</span></a>déantar iad a mheas <a href=\"#id3\"><span class=\"problematic\" id=\"id4\">`</span></a>.</p></li>\n<li><p>&lt;caching-and-querysets&gt;cona:ref: <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">`</span></a>coinnítear na sonraí i gcuimhne <a href=\"#id3\"><span class=\"problematic\" id=\"id4\">`</span></a>.</p></li>\n</ul>\n</section>\n<section id=\"understand-cached-attributes\">\n<h3>Tréithe taiscéadta a thuiscint<a class=\"heading-anchor\" href=\"#understand-cached-attributes\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Chomh maith le caistriú an <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">``</span></a>QuerySet <a href=\"#id3\"><span class=\"problematic\" id=\"id4\">``</span></a>iomlán, tá toradh tréithe ar rudaí ORM a chaitheamh a chaitheamh. Go ginearálta, déanfar tréithe nach féidir glaoch a stóráil. &lt;queryset-model-example&gt;Mar shampla, ag glacadh leis na:ref: <a href=\"#id5\"><span class=\"problematic\" id=\"id6\">`</span></a>samplaí blag samplacha <a href=\"#id7\"><span class=\"problematic\" id=\"id8\">`</span></a>:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>entry <span class=\"o\">=</span> Entry<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"nb\">id</span><span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>entry<span class=\"o\">.</span>blog  <span class=\"c1\"># Blog object is retrieved at this point</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>entry<span class=\"o\">.</span>blog  <span class=\"c1\"># cached version, no DB access</span>\n</code></pre></div>\n<p>Ach go ginearálta, is cúis le tréithe inghlaonta cuardach DB gach uair:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>entry <span class=\"o\">=</span> Entry<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"nb\">id</span><span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>entry<span class=\"o\">.</span>authors<span class=\"o\">.</span>all<span class=\"p\">()</span>  <span class=\"c1\"># query performed</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>entry<span class=\"o\">.</span>authors<span class=\"o\">.</span>all<span class=\"p\">()</span>  <span class=\"c1\"># query performed again</span>\n</code></pre></div>\n<p>Bí cúramach agus tú ag léamh cód teimpléid - ní cheadaíonn an córas teimpléid lúibíní a úsáid, ach glaoifidh sé glaonna go huathoibríoch, ag folach an t-idirdhealú thuas.</p>\n<p>Bí cúramach le d'airíonna saincheaptha féin - is fútsa atá sé caching a chur i bhfeidhm nuair is gá, mar shampla ag baint úsáide as an maisitheoir: class: ~django.utils.functional.cached_property.</p>\n</section>\n<section id=\"use-the-with-template-tag\">\n<h3>Úsáid an chlib teimpléad <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">``</span></a>with`<a class=\"heading-anchor\" href=\"#use-the-with-template-tag\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Chun úsáid a bhaint as iompar caching <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">``</span></a>QuerySet <a href=\"#id3\"><span class=\"problematic\" id=\"id4\">``</span></a>, b'fhéidir go mbeidh ort an chlib teimpléad:ttag: with a úsáid.</p>\n</section>\n<section id=\"use-iterator\">\n<h3>Úsáid <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">``</span></a>iterator () ``<a class=\"heading-anchor\" href=\"#use-iterator\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>When you have a lot of objects, the caching behavior of the <code class=\"docutils literal notranslate\">QuerySet</code> can\ncause a large amount of memory to be used. In this case,\n<a class=\"reference internal\" href=\"/ga/6.0/ref/models/querysets/#django.db.models.query.QuerySet.iterator\" title=\"django.db.models.query.QuerySet.iterator\"><code class=\"xref py py-meth docutils literal notranslate\">iterator()</code></a> may help.</p>\n</section>\n<section id=\"use-explain\">\n<h3>Úsáid <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">``</span></a>mhínigh () ``<a class=\"heading-anchor\" href=\"#use-explain\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p><a class=\"reference internal\" href=\"/ga/6.0/ref/models/querysets/#django.db.models.query.QuerySet.explain\" title=\"django.db.models.query.QuerySet.explain\"><code class=\"xref py py-meth docutils literal notranslate\">QuerySet.explain()</code></a> gives you detailed information about how the database\nexecutes a query, including indexes and joins that are used. These details may\nhelp you find queries that could be rewritten more efficiently, or identify\nindexes that could be added to improve performance.</p>\n</section>\n</section>\n<section id=\"do-database-work-in-the-database-rather-than-in-python\">\n<h2>An oibríonn bunachar sonraí sa bhunachar sonraí seachas i Python<a class=\"heading-anchor\" href=\"#do-database-work-in-the-database-rather-than-in-python\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Mar shampla:</p>\n<ul class=\"simple\">\n<li><p>Ag an leibhéal is bunúsaí, bain úsáid as: ref: <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">`</span></a>scagadh agus eisiamh &lt;queryset-api&gt;`chun scagadh a dhéanamh sa bhunachar sonraí.</p></li>\n<li><p>Úsáid: aicme: abairtí <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">`</span></a>F &lt;django.db.models.F&gt;`chun scagadh bunaithe ar réimsí eile laistigh den tsamhail chéanna.</p></li>\n<li><p>Úsáid:doc: <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">`</span></a>anótáil chun comhiomlánú a dhéanamh sa bhunachar sonraithe' &lt;/topics/db/aggregation&gt;.</p></li>\n</ul>\n<p>Mura leor iad seo chun an SQL a theastaíonn uait a ghiniúint:</p>\n<section id=\"use-rawsql\">\n<h3>Úsáid <code class=\"docutils literal notranslate\">Rawsql</code><a class=\"heading-anchor\" href=\"#use-rawsql\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Modh nach bhfuil chomh iniompartha ach níos cumhachtaí ná an léir:class: ~Django.db.Models.Expressions.RawSql, a ligeann roinnt SQL a chur go sainráite leis an bhfiosrúchán. Mura bhfuil sé sin cumhachtach go leor fós:</p>\n</section>\n<section id=\"use-raw-sql\">\n<h3>Úsáid SQL amh<a class=\"heading-anchor\" href=\"#use-raw-sql\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Scríobh do chuid féin: doc: SQL saincheaptha chun sonraí a aisghabháil nó samhlacha a dhaonrú '&lt;/topics/db/sql&gt;. Úsáid ``django.db.connection.queries` chun a fháil amach cad atá ag scríobh Django duit agus tosú as sin.</p>\n</section>\n</section>\n<section id=\"retrieve-individual-objects-using-a-unique-indexed-column\">\n<h2>Aisghabháil rudaí aonair ag úsáid colún uathúil, innéacsaithe<a class=\"heading-anchor\" href=\"#retrieve-individual-objects-using-a-unique-indexed-column\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Tá dhá chúis ann le colún a úsáid le:attr: ~django.db.models.field.unique nó:attr: ~django.db.models.field.db_index agus tú ag úsáid: meth: ~django.db.models.queryset.get chun rudaí aonair a aisghabháil. Ar dtús, beidh an cheist níos gasta mar gheall ar an innéacs bunachar sonraí bunúsach. Chomh maith leis sin, d'fhéadfadh an cheist rith i bhfad níos moille má mheaitseálann ilrudaí leis an lorg; ráthaíonn srian uathúil a bheith agat ar an gcolún nach dtarlóidh sé seo riamh.</p>\n<p>&lt;queryset-model-example&gt;Mar sin ag baint úsáide as an: ref: <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">`</span></a>sampla samhlacha blag <a href=\"#id3\"><span class=\"problematic\" id=\"id4\">`</span></a>:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>entry <span class=\"o\">=</span> Entry<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"nb\">id</span><span class=\"o\">=</span><span class=\"mi\">10</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>beidh sé níos gasta ná:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>entry <span class=\"o\">=</span> Entry<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span>headline<span class=\"o\">=</span><span class=\"s2\">&quot;News Item Title&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>toisc go ndéanann an bunachar sonraí innéacsaithe <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">``</span></a>id <a href=\"#id3\"><span class=\"problematic\" id=\"id4\">``</span></a>agus ráthaítear go mbeidh sé uathúil.</p>\n<p>D'fhéadfadh sé go mbeadh sé mall go leor na nithe seo a leanas</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>entry <span class=\"o\">=</span> Entry<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span>headline__startswith<span class=\"o\">=</span><span class=\"s2\">&quot;News&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Ar an gcéad dul síos, ní innéacsaítear <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">``</span></a>ceannlíne <a href=\"#id3\"><span class=\"problematic\" id=\"id4\">``</span></a>, rud a fhágfaidh go bhfaighidh an bunachar sonraí bunúsach níos moille.</p>\n<p>Ar an dara dul síos, ní ráthaíonn an cuardach nach gcuirfear ach rud amháin ar ais. Má mheaitseálann an ceist níos mó ná réad amháin, aisghabharfaidh sé agus aistríonn sé iad go léir ón mbunachar sonraí. D'fhéadfadh an pionós seo a bheith suntasach má chuirtear na céadta nó na mílte taifid ar ais. Cuirfear an pionós i measc má tá an bunachar sonraí ina gcónaí ar fhreastalaí ar leithligh, áit a mbíonn fachtóir ag lasbhreas agus latency líonra freisin.</p>\n</section>\n<section id=\"retrieve-everything-at-once-if-you-know-you-will-need-it\">\n<h2>Aisghabháil gach rud ag an am céanna má tá a fhios agat go mbeidh sé uait<a class=\"heading-anchor\" href=\"#retrieve-everything-at-once-if-you-know-you-will-need-it\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Go ginearálta, níl sé chomh héifeachtach an bunachar sonraí a bhualadh arís agus arís eile do chodanna éagsúla de 'tacar' sonraí amháin a bheidh ag teastáil uait gach cuid díobh ná iad go léir a aisghabháil in aon cheist amháin. Tá sé seo tábhachtach go háirithe má tá ceist agat a fhorghníomhaítear i lúb, agus dá bhrí sin d'fhéadfadh go leor ceisteanna bunachar sonraí a dhéanamh, nuair nach raibh ach ceann amháin ag teastáil. Mar sin:</p>\n<section id=\"use-queryset-select-related-and-prefetch-related\">\n<h3>Úsáid <code class=\"docutils literal notranslate\">QuerySet.Select_related () ``agus ``prefetch_related ()</code><a class=\"heading-anchor\" href=\"#use-queryset-select-related-and-prefetch-related\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Tuiscint: Meth: ~django.db.models.query.queryset.select_related agus:meth: ~django.db.models.query.queryset.prefetch_related go maith, agus bain úsáid as iad:</p>\n<ul class=\"simple\">\n<li><p>in:doc: <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">`</span></a>bainisteoirí agus bainisteoirí réamhshocraithe' nuair is cuí. &lt;/topics/db/managers&gt; Bí ar an eolas nuair a úsáidtear agus nach n-úsáidtear do bhainisteoir; uaireanta bíonn sé seo deacair mar sin ná déan toimhde.</p></li>\n<li><p>i gcód amharc nó sraitheanna eile, b'fhéidir ag baint úsáide as: func: ~django.db.models.prefetch_related_objects nuair is gá.</p></li>\n</ul>\n</section>\n</section>\n<section id=\"don-t-retrieve-things-you-don-t-need\">\n<h2>Ná aisghabháil rudaí nach dteastaíonn uait<a class=\"heading-anchor\" href=\"#don-t-retrieve-things-you-don-t-need\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"use-queryset-values-and-values-list\">\n<h3>Úsáid <code class=\"docutils literal notranslate\">QuerySet.values () ``agus ``values_list ()</code><a class=\"heading-anchor\" href=\"#use-queryset-values-and-values-list\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>When you only want a <code class=\"docutils literal notranslate\">dict</code> or <code class=\"docutils literal notranslate\">list</code> of values, and don't need ORM model\nobjects, make appropriate usage of\n<a class=\"reference internal\" href=\"/ga/6.0/ref/models/querysets/#django.db.models.query.QuerySet.values\" title=\"django.db.models.query.QuerySet.values\"><code class=\"xref py py-meth docutils literal notranslate\">values()</code></a>.\nThese can be useful for replacing model objects in template code - as long as\nthe dicts you supply have the same attributes as those used in the template,\nyou are fine.</p>\n</section>\n<section id=\"use-queryset-defer-and-only\">\n<h3>Úsáid <code class=\"docutils literal notranslate\">QuerySet.defer () ``agus ``only ()</code><a class=\"heading-anchor\" href=\"#use-queryset-defer-and-only\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Use <a class=\"reference internal\" href=\"/ga/6.0/ref/models/querysets/#django.db.models.query.QuerySet.defer\" title=\"django.db.models.query.QuerySet.defer\"><code class=\"xref py py-meth docutils literal notranslate\">defer()</code></a> and\n<a class=\"reference internal\" href=\"/ga/6.0/ref/models/querysets/#django.db.models.query.QuerySet.only\" title=\"django.db.models.query.QuerySet.only\"><code class=\"xref py py-meth docutils literal notranslate\">only()</code></a> if there are database columns\nyou know that you won't need (or won't need in most cases) to avoid loading\nthem. Note that if you <em>do</em> use them, the ORM will have to go and get them in\na separate query, making this a pessimization if you use it inappropriately.</p>\n<p>Ná bí ró-ionsaitheach agus réimsí a chur ar iarchur gan próifíliú mar caithfidh an bunachar sonraí an chuid is mó de na sonraí neamh-téacs, neamh-Varchar a léamh ón diosca ar feadh sraith amháin sna torthaí, fiú mura gcríochnaíonn sé ach cúpla colún á úsáid. Tá na modhanna <code class=\"docutils literal notranslate\">defer () ``agus ``only ()</code> is úsáideach nuair is féidir leat a lán sonraí téacs a luchtú a sheachaint nó do réimsí a d'fhéadfadh go leor próiseála a thógáil chun tiontú ar ais go Python. Mar is gnách, próifíl ar dtús, ansin barrfheabhsú.</p>\n</section>\n<section id=\"use-queryset-contains-obj\">\n<h3>Úsáid <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">``</span></a>QuerySet.contents (obj) ``<a class=\"heading-anchor\" href=\"#use-queryset-contains-obj\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>... mura dteastaíonn uait ach a fháil amach an bhfuil <code class=\"docutils literal notranslate\">obj ``sa tacar fiosrúcháin, seachas ``if obj in iarratas</code>.</p>\n</section>\n<section id=\"use-queryset-count\">\n<h3>Úsáid <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">``</span></a>QuerySet.count () ``<a class=\"heading-anchor\" href=\"#use-queryset-count\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>... mura dteastaíonn uait ach an comhaireamh, seachas <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">`</span></a>len (queryset) <a href=\"#id3\"><span class=\"problematic\" id=\"id4\">``</span></a>a dhéanamh.</p>\n</section>\n<section id=\"use-queryset-exists\">\n<h3>Úsáid <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">``</span></a>QuerySet.exists () ``<a class=\"heading-anchor\" href=\"#use-queryset-exists\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>... mura dteastaíonn uait ach a fháil amach an bhfuil toradh amháin ar a laghad ann, seachas <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">``</span></a>if queryset <a href=\"#id3\"><span class=\"problematic\" id=\"id4\">``</span></a>.</p>\n<p>Ach:</p>\n</section>\n<section id=\"don-t-overuse-contains-count-and-exists\">\n<span id=\"overuse-of-count-and-exists\"></span><h3>Ná ró-úsáid <code class=\"docutils literal notranslate\">tá () ``, `count ()</code>, agus <code class=\"docutils literal notranslate\">ann ()</code><a class=\"heading-anchor\" href=\"#don-t-overuse-contains-count-and-exists\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Má tá sonraí eile ag teastáil uait ón QuerySet, déan iad a mheas láithreach.</p>\n<p>Mar shampla, ag glacadh le samhail <code class=\"docutils literal notranslate\">Grúpa</code> a bhfuil gaol go leor aige le <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">``</span></a>Úsáideoir <a href=\"#id3\"><span class=\"problematic\" id=\"id4\">``</span></a>, is fearr an cód seo a leanas:</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>members <span class=\"o\">=</span> group<span class=\"o\">.</span>members<span class=\"o\">.</span>all<span class=\"p\">()</span>\n\n<span class=\"k\">if</span> display_group_members<span class=\"p\">:</span>\n    <span class=\"k\">if</span> members<span class=\"p\">:</span>\n        <span class=\"k\">if</span> current_user <span class=\"ow\">in</span> members<span class=\"p\">:</span>\n            <span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"s2\">&quot;You and&quot;</span><span class=\"p\">,</span> <span class=\"nb\">len</span><span class=\"p\">(</span>members<span class=\"p\">)</span> <span class=\"o\">-</span> <span class=\"mi\">1</span><span class=\"p\">,</span> <span class=\"s2\">&quot;other users are members of this group.&quot;</span><span class=\"p\">)</span>\n        <span class=\"k\">else</span><span class=\"p\">:</span>\n            <span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"s2\">&quot;There are&quot;</span><span class=\"p\">,</span> <span class=\"nb\">len</span><span class=\"p\">(</span>members<span class=\"p\">),</span> <span class=\"s2\">&quot;members in this group.&quot;</span><span class=\"p\">)</span>\n\n        <span class=\"k\">for</span> member <span class=\"ow\">in</span> members<span class=\"p\">:</span>\n            <span class=\"nb\">print</span><span class=\"p\">(</span>member<span class=\"o\">.</span>username<span class=\"p\">)</span>\n    <span class=\"k\">else</span><span class=\"p\">:</span>\n        <span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"s2\">&quot;There are no members in this group.&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Tá sé is fearr mar gheall ar:</p>\n<ol class=\"arabic simple\">\n<li><p>Ós rud é go bhfuil QuerySets leisciúil, ní dhéanann sé seo aon cheisteanna bunachar sonraí má tá <code class=\"docutils literal notranslate\">display_group_members</code> False`.</p></li>\n<li><p>Trí stóráil <code class=\"docutils literal notranslate\">group.members.all () ``sa athróg ``members</code> is féidir a taisce torthaí a athúsáid.</p></li>\n<li><p>Tá an líne <code class=\"docutils literal notranslate\">if comhaltaí: ``is cúis le ``QuerySet. __bool__ () ``le glaoch, rud a fhágann go ndéanfar an ceist ``group.members.all ()</code> a reáchtáil ar an mbunachar sonraí. Mura bhfuil aon torthaí ann, cuirfidh sé ar ais <code class=\"docutils literal notranslate\">False</code>, ar shlí eile <code class=\"docutils literal notranslate\">True</code>.</p></li>\n<li><p>Seiceálann an líne <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">``</span></a>if current_user i gcomhaltaí: <a href=\"#id3\"><span class=\"problematic\" id=\"id4\">``</span></a>an bhfuil an t-úsáideoir sa taisce torthaí, mar sin ní eisítear aon cheisteanna breise bunachar sonraí.</p></li>\n<li><p>Glaonn úsáid <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">``</span></a>len (comhaltaí) <a href=\"#id3\"><span class=\"problematic\" id=\"id4\">``</span></a>QuerySet. __len__ () `, ag athúsáid taisce na dtorthaí, mar sin arís, ní eisítear aon cheisteanna bunachar sonraí.</p></li>\n<li><p>Athraíonn an lúb for ball` thar taisce na torthaí.</p></li>\n</ol>\n<p>San iomlán, déanann an cód seo ceisteanna bunachar sonraí amháin nó nialas. Is é an t-aon bharrfheabhsú d'aon ghnó a dhéantar ná an athróg <code class=\"docutils literal notranslate\">baill ``a úsáid. Ag baint úsáide as ``QuerySet.exists () ``don `if</code>, QuerySet.contents () ```, <code class=\"docutils literal notranslate\">`in</code>, nó <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">``</span></a>QuerySet.count () <a href=\"#id3\"><span class=\"problematic\" id=\"id4\">``</span></a>don chomhaireamh bheadh ceisteanna breise ina chúis le ceisteanna breise.</p>\n</section>\n<section id=\"use-queryset-update-and-delete\">\n<h3>Úsáid <code class=\"docutils literal notranslate\">QuerySet.Update () ``agus ``delete ()</code><a class=\"heading-anchor\" href=\"#use-queryset-update-and-delete\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>&lt;topics-db-queries-update&gt;Seachas ualach rudaí a aisghabháil, socraigh roinnt luachanna, agus iad a shábháil aonair, bain úsáid as ráiteas mórchóir SQL UPDATE, trí:ref: <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">`</span></a>QuerySet.update () <a href=\"#id3\"><span class=\"problematic\" id=\"id4\">`</span></a>. &lt;topics-db-queries-delete&gt;Ar an gcaoi chéanna, déan:ref: <a href=\"#id5\"><span class=\"problematic\" id=\"id6\">`</span></a>bulk scriosann <a href=\"#id7\"><span class=\"problematic\" id=\"id8\">`</span></a>nuair is féidir.</p>\n<p>Tabhair faoi deara, áfach, nach féidir leis na modhanna mór-nuashonraithe seo na modhanna <code class=\"docutils literal notranslate\">save () ``nó ``delete ()</code> cásanna aonair a ghlaoch, rud a chiallaíonn nach gcuirfear aon iompar saincheaptha a chuir tú leis do na modhanna seo a fhorghníomhú, lena n-áirítear aon rud atá tiomáinte ón ngnáthréad bunachar sonraí: doc: signals &lt;/ref/signals&gt;.</p>\n</section>\n<section id=\"use-foreign-key-values-directly\">\n<h3>Úsáid luachanna eachtracha go díreach<a class=\"heading-anchor\" href=\"#use-foreign-key-values-directly\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Mura dteastaíonn uait ach luach eochair eachtrach, bain úsáid as an eochairluach eachtrach atá ar an réad atá agat cheana féin, seachas an réad gaolmhar iomlán a fháil agus a phríomh-eochair a ghlacadh. ie déan:</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>entry<span class=\"o\">.</span>blog_id\n</code></pre></div>\n<p>in ionad:</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>entry<span class=\"o\">.</span>blog<span class=\"o\">.</span>id\n</code></pre></div>\n</section>\n<section id=\"don-t-order-results-if-you-don-t-care\">\n<h3>Ná ordaigh torthaí mura bhfuil cúram ort<a class=\"heading-anchor\" href=\"#don-t-order-results-if-you-don-t-care\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Ordering is not free; each field to order by is an operation the database must\nperform. If a model has a default ordering (<a class=\"reference internal\" href=\"/ga/6.0/ref/models/options/#django.db.models.Options.ordering\" title=\"django.db.models.Options.ordering\"><code class=\"xref py py-attr docutils literal notranslate\">Meta.ordering</code></a>) and you don't need it, remove\nit on a <code class=\"docutils literal notranslate\">QuerySet</code> by calling\n<a class=\"reference internal\" href=\"/ga/6.0/ref/models/querysets/#django.db.models.query.QuerySet.order_by\" title=\"django.db.models.query.QuerySet.order_by\"><code class=\"xref py py-meth docutils literal notranslate\">order_by()</code></a> with no parameters.</p>\n<p>D'fhéadfadh innéacs a chur le do bhunachar sonraí cabhrú le feidhmíocht ordaithe a fheabhsú</p>\n</section>\n</section>\n<section id=\"use-bulk-methods\">\n<h2>Úsáid modhanna mórchóir<a class=\"heading-anchor\" href=\"#use-bulk-methods\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Úsáid modhanna mórchóir chun líon na ráitis SQL a laghdú.</p>\n<section id=\"create-in-bulk\">\n<h3>Cruthaigh ar an mórchóir<a class=\"heading-anchor\" href=\"#create-in-bulk\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>When creating objects, where possible, use the\n<a class=\"reference internal\" href=\"/ga/6.0/ref/models/querysets/#django.db.models.query.QuerySet.bulk_create\" title=\"django.db.models.query.QuerySet.bulk_create\"><code class=\"xref py py-meth docutils literal notranslate\">bulk_create()</code></a> method to reduce the\nnumber of SQL queries. For example:</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>Entry<span class=\"o\">.</span>objects<span class=\"o\">.</span>bulk_create<span class=\"p\">(</span>\n    <span class=\"p\">[</span>\n        Entry<span class=\"p\">(</span>headline<span class=\"o\">=</span><span class=\"s2\">&quot;This is a test&quot;</span><span class=\"p\">),</span>\n        Entry<span class=\"p\">(</span>headline<span class=\"o\">=</span><span class=\"s2\">&quot;This is only a test&quot;</span><span class=\"p\">),</span>\n    <span class=\"p\">]</span>\n<span class=\"p\">)</span>\n</code></pre></div>\n<p>... is fearr ná:</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>Entry<span class=\"o\">.</span>objects<span class=\"o\">.</span>create<span class=\"p\">(</span>headline<span class=\"o\">=</span><span class=\"s2\">&quot;This is a test&quot;</span><span class=\"p\">)</span>\nEntry<span class=\"o\">.</span>objects<span class=\"o\">.</span>create<span class=\"p\">(</span>headline<span class=\"o\">=</span><span class=\"s2\">&quot;This is only a test&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Tabhair faoi deara go bhfuil roinnt de: meth: rabhadh don mhodh seo &lt;django.db.models.query.QuerySet.bulk_create&gt;, mar sin déan cinnte go bhfuil sé oiriúnach do do chás úsáide.</p>\n</section>\n<section id=\"update-in-bulk\">\n<h3>Nuashonrú i mórchóir<a class=\"heading-anchor\" href=\"#update-in-bulk\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>When updating objects, where possible, use the\n<a class=\"reference internal\" href=\"/ga/6.0/ref/models/querysets/#django.db.models.query.QuerySet.bulk_update\" title=\"django.db.models.query.QuerySet.bulk_update\"><code class=\"xref py py-meth docutils literal notranslate\">bulk_update()</code></a> method to reduce the\nnumber of SQL queries. Given a list or queryset of objects:</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>entries <span class=\"o\">=</span> Entry<span class=\"o\">.</span>objects<span class=\"o\">.</span>bulk_create<span class=\"p\">(</span>\n    <span class=\"p\">[</span>\n        Entry<span class=\"p\">(</span>headline<span class=\"o\">=</span><span class=\"s2\">&quot;This is a test&quot;</span><span class=\"p\">),</span>\n        Entry<span class=\"p\">(</span>headline<span class=\"o\">=</span><span class=\"s2\">&quot;This is only a test&quot;</span><span class=\"p\">),</span>\n    <span class=\"p\">]</span>\n<span class=\"p\">)</span>\n</code></pre></div>\n<p>An sampla seo a leanas:</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>entries<span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span><span class=\"o\">.</span>headline <span class=\"o\">=</span> <span class=\"s2\">&quot;This is not a test&quot;</span>\nentries<span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">]</span><span class=\"o\">.</span>headline <span class=\"o\">=</span> <span class=\"s2\">&quot;This is no longer a test&quot;</span>\nEntry<span class=\"o\">.</span>objects<span class=\"o\">.</span>bulk_update<span class=\"p\">(</span>entries<span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"s2\">&quot;headline&quot;</span><span class=\"p\">])</span>\n</code></pre></div>\n<p>... is fearr ná:</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>entries<span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span><span class=\"o\">.</span>headline <span class=\"o\">=</span> <span class=\"s2\">&quot;This is not a test&quot;</span>\nentries<span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span><span class=\"o\">.</span>save<span class=\"p\">()</span>\nentries<span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">]</span><span class=\"o\">.</span>headline <span class=\"o\">=</span> <span class=\"s2\">&quot;This is no longer a test&quot;</span>\nentries<span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">]</span><span class=\"o\">.</span>save<span class=\"p\">()</span>\n</code></pre></div>\n<p>Tabhair faoi deara go bhfuil roinnt de: meth: rabhadh don mhodh seo &lt;django.db.models.query.QuerySet.bulk_update&gt;, mar sin déan cinnte go bhfuil sé oiriúnach do do chás úsáide.</p>\n</section>\n<section id=\"insert-in-bulk\">\n<h3>Cuir isteach sa mórchóir<a class=\"heading-anchor\" href=\"#insert-in-bulk\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Agus rudaí á chur isteach ion:class: ManyTomanyFields &lt;django.db.models.ManyToManyField&gt;, bain úsáid as: meth: ~Django.db.Models.Fields.Related.RelatedManager.Add le rudaí iolracha chun líon na gceisteanna SQL a laghdú. Mar shampla:</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>my_band<span class=\"o\">.</span>members<span class=\"o\">.</span>add<span class=\"p\">(</span>me<span class=\"p\">,</span> my_friend<span class=\"p\">)</span>\n</code></pre></div>\n<p>... is fearr ná:</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>my_band<span class=\"o\">.</span>members<span class=\"o\">.</span>add<span class=\"p\">(</span>me<span class=\"p\">)</span>\nmy_band<span class=\"o\">.</span>members<span class=\"o\">.</span>add<span class=\"p\">(</span>my_friend<span class=\"p\">)</span>\n</code></pre></div>\n<p>...where <code class=\"docutils literal notranslate\">Band</code> and <code class=\"docutils literal notranslate\">Artist</code> are models with a many-to-many relationship.</p>\n<p>When inserting different pairs of objects into\n<a class=\"reference internal\" href=\"/ga/6.0/ref/models/fields/#django.db.models.ManyToManyField\" title=\"django.db.models.ManyToManyField\"><code class=\"xref py py-class docutils literal notranslate\">ManyToManyField</code></a> or when the custom\n<a class=\"reference internal\" href=\"/ga/6.0/ref/models/fields/#django.db.models.ManyToManyField.through\" title=\"django.db.models.ManyToManyField.through\"><code class=\"xref py py-attr docutils literal notranslate\">through</code></a> table is defined, use\n<a class=\"reference internal\" href=\"/ga/6.0/ref/models/querysets/#django.db.models.query.QuerySet.bulk_create\" title=\"django.db.models.query.QuerySet.bulk_create\"><code class=\"xref py py-meth docutils literal notranslate\">bulk_create()</code></a> method to reduce the\nnumber of SQL queries. For example:</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>PizzaToppingRelationship <span class=\"o\">=</span> Pizza<span class=\"o\">.</span>toppings<span class=\"o\">.</span>through\nPizzaToppingRelationship<span class=\"o\">.</span>objects<span class=\"o\">.</span>bulk_create<span class=\"p\">(</span>\n    <span class=\"p\">[</span>\n        PizzaToppingRelationship<span class=\"p\">(</span>pizza<span class=\"o\">=</span>my_pizza<span class=\"p\">,</span> topping<span class=\"o\">=</span>pepperoni<span class=\"p\">),</span>\n        PizzaToppingRelationship<span class=\"p\">(</span>pizza<span class=\"o\">=</span>your_pizza<span class=\"p\">,</span> topping<span class=\"o\">=</span>pepperoni<span class=\"p\">),</span>\n        PizzaToppingRelationship<span class=\"p\">(</span>pizza<span class=\"o\">=</span>your_pizza<span class=\"p\">,</span> topping<span class=\"o\">=</span>mushroom<span class=\"p\">),</span>\n    <span class=\"p\">],</span>\n    ignore_conflicts<span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">,</span>\n<span class=\"p\">)</span>\n</code></pre></div>\n<p>... is fearr ná:</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>my_pizza<span class=\"o\">.</span>toppings<span class=\"o\">.</span>add<span class=\"p\">(</span>pepperoni<span class=\"p\">)</span>\nyour_pizza<span class=\"o\">.</span>toppings<span class=\"o\">.</span>add<span class=\"p\">(</span>pepperoni<span class=\"p\">,</span> mushroom<span class=\"p\">)</span>\n</code></pre></div>\n<p>... áit a bhfuil caidreamh go leor ag Pizza` agus <code class=\"docutils literal notranslate\">Topping</code>. Tabhair faoi deara go bhfuil roinnt de: meth: rabhadh don mhodh seo &lt;django.db.models.query.QuerySet.bulk_create&gt;, mar sin déan cinnte go bhfuil sé oiriúnach do do chás úsáide.</p>\n</section>\n<section id=\"remove-in-bulk\">\n<h3>Bain ar an mórchóir<a class=\"heading-anchor\" href=\"#remove-in-bulk\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Agus rudaí á bhaint as: class: ManyTomanyFields &lt;django.db.models.ManyToManyField&gt;, bain úsáid as: meth: ~Django.db.Models.Fields.Related.RelatedManager.Remove le rudaí iolracha chun líon na gceisteanna SQL a laghdú. Mar shampla:</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>my_band<span class=\"o\">.</span>members<span class=\"o\">.</span>remove<span class=\"p\">(</span>me<span class=\"p\">,</span> my_friend<span class=\"p\">)</span>\n</code></pre></div>\n<p>... is fearr ná:</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>my_band<span class=\"o\">.</span>members<span class=\"o\">.</span>remove<span class=\"p\">(</span>me<span class=\"p\">)</span>\nmy_band<span class=\"o\">.</span>members<span class=\"o\">.</span>remove<span class=\"p\">(</span>my_friend<span class=\"p\">)</span>\n</code></pre></div>\n<p>...where <code class=\"docutils literal notranslate\">Band</code> and <code class=\"docutils literal notranslate\">Artist</code> are models with a many-to-many relationship.</p>\n<p>Agus péirí éagsúla rudaí á bhaint ó:class: ManyTomanyFields &lt;django.db.models.ManyToManyField&gt;, bain úsáid as: meth: ~django.db.models.queryset.delete ar a:class: ~django.db.models.q léiriú le iolr:attr: ~django.db.models.manyToManyField.through cásanna samhail chun líon na gceisteanna SQL a laghdú. Mar shampla:</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=\"kn\">from</span> <span class=\"nn\">django.db.models</span> <span class=\"kn\">import</span> Q\n\nPizzaToppingRelationship <span class=\"o\">=</span> Pizza<span class=\"o\">.</span>toppings<span class=\"o\">.</span>through\nPizzaToppingRelationship<span class=\"o\">.</span>objects<span class=\"o\">.</span>filter<span class=\"p\">(</span>\n    Q<span class=\"p\">(</span>pizza<span class=\"o\">=</span>my_pizza<span class=\"p\">,</span> topping<span class=\"o\">=</span>pepperoni<span class=\"p\">)</span>\n    <span class=\"o\">|</span> Q<span class=\"p\">(</span>pizza<span class=\"o\">=</span>your_pizza<span class=\"p\">,</span> topping<span class=\"o\">=</span>pepperoni<span class=\"p\">)</span>\n    <span class=\"o\">|</span> Q<span class=\"p\">(</span>pizza<span class=\"o\">=</span>your_pizza<span class=\"p\">,</span> topping<span class=\"o\">=</span>mushroom<span class=\"p\">)</span>\n<span class=\"p\">)</span><span class=\"o\">.</span>delete<span class=\"p\">()</span>\n</code></pre></div>\n<p>... is fearr ná:</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>my_pizza<span class=\"o\">.</span>toppings<span class=\"o\">.</span>remove<span class=\"p\">(</span>pepperoni<span class=\"p\">)</span>\nyour_pizza<span class=\"o\">.</span>toppings<span class=\"o\">.</span>remove<span class=\"p\">(</span>pepperoni<span class=\"p\">,</span> mushroom<span class=\"p\">)</span>\n</code></pre></div>\n<p>... áit a bhfuil caidreamh go leor ag Pizza` agus <code class=\"docutils literal notranslate\">Topping</code>.</p>\n</section>\n</section>","rootId":"database-access-optimization","toc":[{"title":"Próifíl ar dtús","anchor":"profile-first","children":[]},{"title":"Bain úsáid as teicnící caighdeánacha","anchor":"use-standard-db-optimization-techniques","children":[]},{"title":"Tuiscint a fháil ar QuerySets","anchor":"understand-querysets","children":[{"title":"Meastóireacht ``QuerySet ``a thuiscint","anchor":"understand-queryset-evaluation","children":[]},{"title":"Tréithe taiscéadta a thuiscint","anchor":"understand-cached-attributes","children":[]},{"title":"Úsáid an chlib teimpléad ``with`","anchor":"use-the-with-template-tag","children":[]},{"title":"Úsáid ``iterator () ``","anchor":"use-iterator","children":[]},{"title":"Úsáid ``mhínigh () ``","anchor":"use-explain","children":[]}]},{"title":"An oibríonn bunachar sonraí sa bhunachar sonraí seachas i Python","anchor":"do-database-work-in-the-database-rather-than-in-python","children":[{"title":"Úsáid Rawsql","anchor":"use-rawsql","children":[]},{"title":"Úsáid SQL amh","anchor":"use-raw-sql","children":[]}]},{"title":"Aisghabháil rudaí aonair ag úsáid colún uathúil, innéacsaithe","anchor":"retrieve-individual-objects-using-a-unique-indexed-column","children":[]},{"title":"Aisghabháil gach rud ag an am céanna má tá a fhios agat go mbeidh sé uait","anchor":"retrieve-everything-at-once-if-you-know-you-will-need-it","children":[{"title":"Úsáid QuerySet.Select_related () ``agus ``prefetch_related ()","anchor":"use-queryset-select-related-and-prefetch-related","children":[]}]},{"title":"Ná aisghabháil rudaí nach dteastaíonn uait","anchor":"don-t-retrieve-things-you-don-t-need","children":[{"title":"Úsáid QuerySet.values () ``agus ``values_list ()","anchor":"use-queryset-values-and-values-list","children":[]},{"title":"Úsáid QuerySet.defer () ``agus ``only ()","anchor":"use-queryset-defer-and-only","children":[]},{"title":"Úsáid ``QuerySet.contents (obj) ``","anchor":"use-queryset-contains-obj","children":[]},{"title":"Úsáid ``QuerySet.count () ``","anchor":"use-queryset-count","children":[]},{"title":"Úsáid ``QuerySet.exists () ``","anchor":"use-queryset-exists","children":[]},{"title":"Ná ró-úsáid tá () ``, `count (), agus ann ()","anchor":"don-t-overuse-contains-count-and-exists","children":[]},{"title":"Úsáid QuerySet.Update () ``agus ``delete ()","anchor":"use-queryset-update-and-delete","children":[]},{"title":"Úsáid luachanna eachtracha go díreach","anchor":"use-foreign-key-values-directly","children":[]},{"title":"Ná ordaigh torthaí mura bhfuil cúram ort","anchor":"don-t-order-results-if-you-don-t-care","children":[]}]},{"title":"Úsáid modhanna mórchóir","anchor":"use-bulk-methods","children":[{"title":"Cruthaigh ar an mórchóir","anchor":"create-in-bulk","children":[]},{"title":"Nuashonrú i mórchóir","anchor":"update-in-bulk","children":[]},{"title":"Cuir isteach sa mórchóir","anchor":"insert-in-bulk","children":[]},{"title":"Bain ar an mórchóir","anchor":"remove-in-bulk","children":[]}]}],"breadcrumbs":[{"docname":"topics/index","title":"Using Django","url":"/ga/6.0/topics/"},{"docname":"topics/db/index","title":"Samhlacha agus bunachair sonraí","url":"/ga/6.0/topics/db/"}],"prev":{"docname":"topics/db/tablespaces","title":"Spásanna tábla","url":"/ga/6.0/topics/db/tablespaces/"},"next":{"docname":"topics/db/instrumentation","title":"Ionstraimíocht bunachar sonraí","url":"/ga/6.0/topics/db/instrumentation/"},"formats":{"html":"/ga/6.0/topics/db/optimization/","markdown":"/ga/6.0/topics/db/optimization.md","json":"/ga/6.0/topics/db/optimization.json"},"source":"https://github.com/django/django/blob/stable/6.0.x/docs/topics/db/optimization.txt","official":"https://docs.djangoproject.com/ga/6.0/topics/db/optimization/","inVersions":["6.1","6.0","5.2"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}