<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Death of a Gremmie &#187; debug</title>
	<atom:link href="http://deathofagremmie.com/tag/debug/feed/" rel="self" type="application/rss+xml" />
	<link>http://deathofagremmie.com</link>
	<description>by Brian Neal</description>
	<lastBuildDate>Wed, 14 Jul 2010 02:31:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Displaying a Page&#8217;s SQL Queries</title>
		<link>http://deathofagremmie.com/2009/01/25/displaying-a-pages-sql-queries/</link>
		<comments>http://deathofagremmie.com/2009/01/25/displaying-a-pages-sql-queries/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 21:14:29 +0000</pubDate>
		<dc:creator>gremmie</dc:creator>
				<category><![CDATA[SG101 2.0]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://deathofagremmie.com/?p=146</guid>
		<description><![CDATA[I&#8217;ve been meaning to start paying attention to the SQL queries that Django is generating for me. I&#8217;d like to know if there are any dumb things I am doing. And down the road I am going to start employing low-level caching, and seeing what queries I am doing will help me decide what things [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been meaning to start paying attention to the SQL queries that Django is generating for me. I&#8217;d like to know if there are any dumb things I am doing. And down the road I am going to start employing low-level caching, and seeing what queries I am doing will help me decide what things to cache. I finally broke down and figured out how you can display the SQL queries used during an HTTP request on the rendered web page. Django makes this quite easy and it is fully documented.</p>
<p>If your TEMPLATE_CONTEXT_PROCESSORS in your settings.py file contains &#8220;django.core.context_processors.debug&#8221;, and DEBUG is set to True, you have access to a variable in your templates called sql_queries. This is an ordered list of <tt class="docutils literal"><span class="pre">{'sql':</span> <span class="pre">...,</span> <span class="pre">'time':</span> <span class="pre">...}</span></tt> dictionaries that represent every SQL query and how long each one took during the processing of the request.</p>
<p>It was quite easy to add the following to the very bottom of my site&#8217;s base template:</p>
<pre class="brush: html">{% if debug %}
&lt;div id=&quot;debug&quot;&gt;
&lt;ol&gt;
{% for s in sql_queries %}
&lt;li&gt;{{ s.sql }} : &lt;b&gt;({{ s.time }})&lt;/b&gt;&lt;/li&gt;
{% endfor %}
&lt;/ol&gt;
&lt;/div&gt;
{% endif %}
</pre>
<p>Now I can keep an eye on what&#8217;s happening at the database level. I&#8217;ve already added a few key &#8220;<a href="http://docs.djangoproject.com/en/dev/ref/models/querysets/#id4">select_related</a>&#8221; clauses to some of my Django ORM queries that have saved 10-30% on the number of trips to the database per page. Cool!</p>
]]></content:encoded>
			<wfw:commentRss>http://deathofagremmie.com/2009/01/25/displaying-a-pages-sql-queries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
