Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 19BCC9F9386 for ; Mon, 11 Dec 2006 01:50:24 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-new, port 10024) with ESMTP id 85357-05 for ; Mon, 11 Dec 2006 01:50:15 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from smtp5.clear.net.nz (smtp5.clear.net.nz [203.97.33.68]) by postgresql.org (Postfix) with ESMTP id D8B8E9F9357 for ; Mon, 11 Dec 2006 01:50:14 -0400 (AST) Received: from [192.168.1.11] (121-72-71-26.dsl.telstraclear.net [121.72.71.26]) by smtp5.clear.net.nz (CLEAR Net Mail) with ESMTP id <0JA300JETHJO7240@smtp5.clear.net.nz> for pgsql-performance@postgresql.org; Mon, 11 Dec 2006 18:50:13 +1300 (NZDT) Date: Mon, 11 Dec 2006 18:49:56 +1300 From: Mark Kirkwood Subject: Re: SQL_CALC_FOUND_ROWS in POSTGRESQL / Some one can In-reply-to: <457CE03C.1060707@gmail.com> To: Chris Cc: "Joshua D. Drake" , marcosborges@mbsi.com.br, pgsql-performance@postgresql.org Message-id: <457CF184.6020702@paradise.net.nz> MIME-version: 1.0 Content-type: text/plain; charset=ISO-8859-1; format=flowed Content-transfer-encoding: 7bit References: <457814D3.1030001@mbsi.com.br> <457CD182.7020004@gmail.com> <1165809878.19970.2.camel@localhost.localdomain> <457CE03C.1060707@gmail.com> User-Agent: Thunderbird 1.5.0.8 (X11/20061129) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/205 X-Sequence-Number: 22261 Chris wrote: > It's the same as doing a select count(*) type query using the same > clauses, but all in one query instead of two. > > It doesn't return any extra rows on top of the limit query so it's > better than using pg_numrows which runs the whole query and returns it > to php (in this example). > > > Their docs explain it: > > http://dev.mysql.com/doc/refman/4.1/en/information-functions.html > > See "FOUND_ROWS()" > Note that from the same page: "If you are using SELECT SQL_CALC_FOUND_ROWS, MySQL must calculate how many rows are in the full result set. However, this is faster than running the query again without LIMIT, because the result set need not be sent to the client." So it is not as cost-free as it would seem - the CALC step is essentially doing "SELECT count(*) FROM (your-query)" in addition to your-query-with-the-limit. I don't buy the "its cheap 'cause nothing is returned to the client" bit, because 'SELECT count(*) ...' returns only 1 tuple of 1 element to the client anyway. On the face of it, it *looks* like you save an extra set of parse, execute, construct (trivially small) resultset calls - but 'SELECT FOUND_ROWS()' involves that set of steps too, so I'm not entirely convinced that doing a 2nd 'SELECT count(*)...' is really any different in impact. Cheers Mark