<?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>The Magic Position &#187; mysql</title>
	<atom:link href="http://www.magicposition.com/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.magicposition.com</link>
	<description>Don't just build websites: develop the web.</description>
	<lastBuildDate>Tue, 17 Jan 2012 02:01:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Getting the total number of matching rows from MySQL when using LIMIT</title>
		<link>http://www.magicposition.com/2010/05/21/getting-the-total-number-of-matching-rows-from-mysql-when-using-limit/</link>
		<comments>http://www.magicposition.com/2010/05/21/getting-the-total-number-of-matching-rows-from-mysql-when-using-limit/#comments</comments>
		<pubDate>Sat, 22 May 2010 01:04:19 +0000</pubDate>
		<dc:creator>Laurie</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.magicposition.com/?p=32</guid>
		<description><![CDATA[It&#8217;s an extremely common requirement, when designing an API or GUI that includes pagination, to provide 10 rows at a time, but also include a figure of the total number of matching rows. Traditionally people do this by running two requests: first the query, then the same conditions with a COUNT(*) instead. This however is [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s an extremely common requirement, when designing an API or GUI that includes pagination, to provide 10 rows at a time, but also include a figure of the total number of matching rows. Traditionally people do this by running two requests: first the query, then the same conditions with a COUNT(*) instead. This however is two potentially heavy DB hits. There is a quicker and easier way, and it&#8217;s laughably simple.</p>
<p>First, in your SQL, add the <a href="http://dev.mysql.com/doc/refman/5.0/en/select.html">SQL_CALC_FOUND_ROWS</a> modifier in front of your SELECT:</p>
<p><code>SELECT SQL_CALC_FOUND_ROWS name, price FROM fruits LIMIT 10;</code></p>
<p>Then, after running your query, immediately run the <a href="http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows">FOUND_ROWS()</a> query:</p>
<p><code>SELECT FOUND_ROWS();</code></p>
<p>Done! The second query is returned instantly using the metadata returned from the first, with no additional heavy lifting on the DB&#8217;s part. Brilliant!</p>
<p>FOUND_ROWS() works no matter how complicated your SELECT statement. Note that with a GROUP_BY it will return the number of rows in the <i>result</i>, not the number of rows matched before they were grouped. This behaviour is usually going to be more useful to you anyway.</p>
<p>Thanks to <a href="http://www.arraystudio.com/as-workshop/mysql-get-total-number-of-rows-when-using-limit.html">ArrayStudio</a> for the well-written tip.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.magicposition.com/2010/05/21/getting-the-total-number-of-matching-rows-from-mysql-when-using-limit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

