<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2.3" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: Django snippet &#8212; Template tag: split list to n sublists</title>
	<link>http://herself.movielady.net/2008/07/16/split-list-to-columns-django-template-tag/</link>
	<description>Natterings on the state of my world</description>
	<pubDate>Tue, 06 Jan 2009 13:56:40 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.3</generator>

	<item>
		<title>By: Kjell Magne Fauske</title>
		<link>http://herself.movielady.net/2008/07/16/split-list-to-columns-django-template-tag/#comment-2625</link>
		<dc:creator>Kjell Magne Fauske</dc:creator>
		<pubDate>Mon, 06 Oct 2008 08:41:57 +0000</pubDate>
		<guid>http://herself.movielady.net/2008/07/16/split-list-to-columns-django-template-tag/#comment-2625</guid>
		<description>In an earlier comment I said that the change reduced the number queries from over a hundred to eight. That was not completely true. I did a few denomalization tricks that also reduced the number of queries a lot. 

The reason that you don't see any changes may be that you are already passing an evaluated queryset to the templatetag. If you only use two columns the extra overhead will not be that large either. In my case I split it into four columns. 

The debug toolbar is indeed useful. It can be a bit slow for complex pages, but it is a great tool for spotting bottlenecks.</description>
		<content:encoded><![CDATA[<p>In an earlier comment I said that the change reduced the number queries from over a hundred to eight. That was not completely true. I did a few denomalization tricks that also reduced the number of queries a lot. </p>
<p>The reason that you don&#8217;t see any changes may be that you are already passing an evaluated queryset to the templatetag. If you only use two columns the extra overhead will not be that large either. In my case I split it into four columns. </p>
<p>The debug toolbar is indeed useful. It can be a bit slow for complex pages, but it is a great tool for spotting bottlenecks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Herself</title>
		<link>http://herself.movielady.net/2008/07/16/split-list-to-columns-django-template-tag/#comment-2619</link>
		<dc:creator>Herself</dc:creator>
		<pubDate>Fri, 03 Oct 2008 19:04:21 +0000</pubDate>
		<guid>http://herself.movielady.net/2008/07/16/split-list-to-columns-django-template-tag/#comment-2619</guid>
		<description>Oh wait, I take that back. The debug toolbar does sort of have one of the things I was looking for; it was under templates, which I hadn't looked at yet. (The request context info toggle is what I'm looking at, though it still doesn't give me &lt;em&gt;exactly&lt;/em&gt; what I want, but it's a start. *g*)</description>
		<content:encoded><![CDATA[<p>Oh wait, I take that back. The debug toolbar does sort of have one of the things I was looking for; it was under templates, which I hadn&#8217;t looked at yet. (The request context info toggle is what I&#8217;m looking at, though it still doesn&#8217;t give me <em>exactly</em> what I want, but it&#8217;s a start. *g*)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Herself</title>
		<link>http://herself.movielady.net/2008/07/16/split-list-to-columns-django-template-tag/#comment-2618</link>
		<dc:creator>Herself</dc:creator>
		<pubDate>Fri, 03 Oct 2008 16:52:14 +0000</pubDate>
		<guid>http://herself.movielady.net/2008/07/16/split-list-to-columns-django-template-tag/#comment-2618</guid>
		<description>Thanks for the pointer to Rob's debugging toolbar! (Very nice, though it still doesn't have the one thing I &lt;em&gt;really&lt;/em&gt; need, of course. *g*)

However, I'm not seeing any change in the number of queries performed (one for each try) on the pages I'm using the template tag as written versus the change you've suggested. (Though it makes more sense to me know why you're suggesting that now, thanks. *g*)

Also, I've changed the variables used so list isn't a variable name. Didn't think about that when I started working on this. *g*</description>
		<content:encoded><![CDATA[<p>Thanks for the pointer to Rob&#8217;s debugging toolbar! (Very nice, though it still doesn&#8217;t have the one thing I <em>really</em> need, of course. *g*)</p>
<p>However, I&#8217;m not seeing any change in the number of queries performed (one for each try) on the pages I&#8217;m using the template tag as written versus the change you&#8217;ve suggested. (Though it makes more sense to me know why you&#8217;re suggesting that now, thanks. *g*)</p>
<p>Also, I&#8217;ve changed the variables used so list isn&#8217;t a variable name. Didn&#8217;t think about that when I started working on this. *g*</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kjell Magne Fauske</title>
		<link>http://herself.movielady.net/2008/07/16/split-list-to-columns-django-template-tag/#comment-2605</link>
		<dc:creator>Kjell Magne Fauske</dc:creator>
		<pubDate>Thu, 02 Oct 2008 13:09:56 +0000</pubDate>
		<guid>http://herself.movielady.net/2008/07/16/split-list-to-columns-django-template-tag/#comment-2605</guid>
		<description>The reason for the high number of queries is due to the fact that Querysets are lazy. They are only evaluated when needed. So when you slice your list with code like len(list[i::cols]) you hit the database multiple times. For instance, determining the length of the queryset requires one query. The results are then thrown away and a new query is necessary for the next iteration. Likewise, extracting a slice requires a new query. 

With the code "tmplist=tuple(list)" all items in the list are retrieved from the database with a single query and stored in a list. So writing len(tmplist) now does not require a database query. Neither do slicing. The downside is that all items are stored in memory, but that is usually not a big problem. 

I hope that the above explanation makes things clearer. If you are interested in looking at the SQL-queries I recommend trying the "Django Debug Toolbar" (http://rob.cogit8.org/blog/2008/Sep/19/introducing-django-debug-toolbar/) That's how I found out about the "problem" with your template tag. A page that used your tag required more than hundred queries. After I did the tuple/list trick the number of queries was reduced to eight. 

PS. I used tuple instead of list to avoid confusion with your variable named list.</description>
		<content:encoded><![CDATA[<p>The reason for the high number of queries is due to the fact that Querysets are lazy. They are only evaluated when needed. So when you slice your list with code like len(list[i::cols]) you hit the database multiple times. For instance, determining the length of the queryset requires one query. The results are then thrown away and a new query is necessary for the next iteration. Likewise, extracting a slice requires a new query. </p>
<p>With the code &#8220;tmplist=tuple(list)&#8221; all items in the list are retrieved from the database with a single query and stored in a list. So writing len(tmplist) now does not require a database query. Neither do slicing. The downside is that all items are stored in memory, but that is usually not a big problem. </p>
<p>I hope that the above explanation makes things clearer. If you are interested in looking at the SQL-queries I recommend trying the &#8220;Django Debug Toolbar&#8221; (http://rob.cogit8.org/blog/2008/Sep/19/introducing-django-debug-toolbar/) That&#8217;s how I found out about the &#8220;problem&#8221; with your template tag. A page that used your tag required more than hundred queries. After I did the tuple/list trick the number of queries was reduced to eight. </p>
<p>PS. I used tuple instead of list to avoid confusion with your variable named list.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Herself</title>
		<link>http://herself.movielady.net/2008/07/16/split-list-to-columns-django-template-tag/#comment-2604</link>
		<dc:creator>Herself</dc:creator>
		<pubDate>Thu, 02 Oct 2008 06:32:56 +0000</pubDate>
		<guid>http://herself.movielady.net/2008/07/16/split-list-to-columns-django-template-tag/#comment-2604</guid>
		<description>Glad you found it useful, Kjell. :) I'm trying really hard to document things that took me a while to figure out or find answers to while working on my current projects, 'cuz goodness knows I'd be lost sometimes without other kind souls who have done the same thing. *vbg*

Now on to your suggested change (and this is my lack of knowledge of python showing, as I'm still learning it), how does adding the "tmplist=tuple(list)" bit help reduce the number of queries? 

(I must admit to a great deal of frustration with how the queries and results from django are structured, as they just don't make sense to me yet. Largely because I'm not really comfortable with the ins and outs of OOP, I think. I only just discovered the connection.queries trick to see the raw sql last week, and am still looking for a way that will display the results of the queries and their object names, because that would help me immensely in my debugging. Ah, the joys of a new language/framework. *g*)</description>
		<content:encoded><![CDATA[<p>Glad you found it useful, Kjell. :) I&#8217;m trying really hard to document things that took me a while to figure out or find answers to while working on my current projects, &#8216;cuz goodness knows I&#8217;d be lost sometimes without other kind souls who have done the same thing. *vbg*</p>
<p>Now on to your suggested change (and this is my lack of knowledge of python showing, as I&#8217;m still learning it), how does adding the &#8220;tmplist=tuple(list)&#8221; bit help reduce the number of queries? </p>
<p>(I must admit to a great deal of frustration with how the queries and results from django are structured, as they just don&#8217;t make sense to me yet. Largely because I&#8217;m not really comfortable with the ins and outs of OOP, I think. I only just discovered the connection.queries trick to see the raw sql last week, and am still looking for a way that will display the results of the queries and their object names, because that would help me immensely in my debugging. Ah, the joys of a new language/framework. *g*)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kjell Magne Fauske</title>
		<link>http://herself.movielady.net/2008/07/16/split-list-to-columns-django-template-tag/#comment-2584</link>
		<dc:creator>Kjell Magne Fauske</dc:creator>
		<pubDate>Tue, 30 Sep 2008 06:34:20 +0000</pubDate>
		<guid>http://herself.movielady.net/2008/07/16/split-list-to-columns-django-template-tag/#comment-2584</guid>
		<description>Thanks for sharing. The templatetag is really useful. 

The templatetag has some performance issues when splitting querysets though. A large number of SQL queries are executed. To reduce the number of queries you could evaluate the queryset prior to splitting. I posted one approach on the django-snippets page. 

Thanks again for sharing.</description>
		<content:encoded><![CDATA[<p>Thanks for sharing. The templatetag is really useful. </p>
<p>The templatetag has some performance issues when splitting querysets though. A large number of SQL queries are executed. To reduce the number of queries you could evaluate the queryset prior to splitting. I posted one approach on the django-snippets page. </p>
<p>Thanks again for sharing.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
