Re: SQL_CALC_FOUND_ROWS in POSTGRESQL / Some one can

Lists: pgsql-performance
From: Marcos Borges <marcosborges(at)mbsi(dot)com(dot)br>
To: pgsql-performance(at)postgresql(dot)org
Subject: SQL_CALC_FOUND_ROWS in POSTGRESQL / Some one can helpe me.
Date: 2006-12-07 13:19:15
Message-ID: 457814D3.1030001@mbsi.com.br
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
<div
style="padding-left: 20px; font-size: 92%; float: right; padding-bottom: 10px;">07/12/2006&nbsp;04:31
</div>
<b>SQL_CALC_FOUND_ROWS in POSTGRESQL</b><br>
<br>
In mysqln i m using the command SQL_CALC_FOUND_ROWS in follow sintax.<br>
SELECT SQL_CALC_FOUND_ROWS name, email, tel FROM mytable WHERE name
&lt;&gt; '' LIMIT 0, 10<br>
to have the recorset data.<br>
and<br>
SELECT FOUND_ROWS();<br>
to have the total of registers found.<br>
<br>
I dont want to use the command count(*), because the performance will
fall down, depending of the quantyt of tables and "joins".<br>
<br>
The Data base postgresql have something similar ???<br>
<br>
<br>
---------------------------------------------------------------------------------------------------<br>
<br>
<div
style="padding-left: 20px; font-size: 92%; float: right; padding-bottom: 10px;">07/12/2006&nbsp;04:31
</div>
<b>SQL_CALC_FOUND_ROWS no POSTGRESQL</b><br>
D&uacute;vida NINJA no POSTGRESQL<br>
No mysql utilizo o comando SQL_CALC_FOUND_ROWS na seguinte sintax<br>
SELECT SQL_CALC_FOUND_ROWS nome, email, telefone FROM tabela WHERE nome
&lt;&gt; '' LIMIT 0, 10<br>
para obter o meu recordset<br>
e<br>
SELECT FOUND_ROWS();<br>
para obter o total de resgitros que realmente existem em minha tabela
condicionado pelo WHERE, sem ser limitado pelo LIMIT.<br>
<br>
N&atilde;o quero usar o count(*) pois o desempenho cai dependendo da
quantidade de tabelas selecionadas e quantidade de registros.<br>
<br>
<br>
O postgreSQL possui algo similar? Caso sim pode me informar qual e
fornecer um exemplo. <br>
<br>
</body>
</html>

Attachment Content-Type Size
unknown_filename text/html 1.7 KB

From: Chris <dmagick(at)gmail(dot)com>
To: marcosborges(at)mbsi(dot)com(dot)br
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: SQL_CALC_FOUND_ROWS in POSTGRESQL / Some one can helpe
Date: 2006-12-11 03:33:22
Message-ID: 457CD182.7020004@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

Marcos Borges wrote:
> 07/12/2006 04:31
> *SQL_CALC_FOUND_ROWS in POSTGRESQL*
>
> In mysqln i m using the command SQL_CALC_FOUND_ROWS in follow sintax.
> SELECT SQL_CALC_FOUND_ROWS name, email, tel FROM mytable WHERE name <>
> '' LIMIT 0, 10
> to have the recorset data.
> and
> SELECT FOUND_ROWS();
> to have the total of registers found.
>
> I dont want to use the command count(*), because the performance will
> fall down, depending of the quantyt of tables and "joins".
>
> The Data base postgresql have something similar ???

Nope, you're out of luck sorry. That's a mysql-ism and I doubt postgres
will ever include something similar.

--
Postgresql & php tutorials
http://www.designmagick.com/


From: "Joshua D(dot) Drake" <jd(at)commandprompt(dot)com>
To: Chris <dmagick(at)gmail(dot)com>
Cc: marcosborges(at)mbsi(dot)com(dot)br, pgsql-performance(at)postgresql(dot)org
Subject: Re: SQL_CALC_FOUND_ROWS in POSTGRESQL / Some one can
Date: 2006-12-11 04:04:38
Message-ID: 1165809878.19970.2.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

On Mon, 2006-12-11 at 14:33 +1100, Chris wrote:
> Marcos Borges wrote:
> > 07/12/2006 04:31
> > *SQL_CALC_FOUND_ROWS in POSTGRESQL*
> >
> > In mysqln i m using the command SQL_CALC_FOUND_ROWS in follow sintax.
> > SELECT SQL_CALC_FOUND_ROWS name, email, tel FROM mytable WHERE name <>
> > '' LIMIT 0, 10
> > to have the recorset data.
> > and
> > SELECT FOUND_ROWS();
> > to have the total of registers found.
> >
> > I dont want to use the command count(*), because the performance will
> > fall down, depending of the quantyt of tables and "joins".
> >
> > The Data base postgresql have something similar ???
>
> Nope, you're out of luck sorry. That's a mysql-ism and I doubt postgres
> will ever include something similar.

Your language will have a similar binding. Something like pg_numrows.

Sincerely,

Joshua D. Drake

>
--

=== The PostgreSQL Company: Command Prompt, Inc. ===
Sales/Support: +1.503.667.4564 || 24x7/Emergency: +1.800.492.2240
Providing the most comprehensive PostgreSQL solutions since 1997
http://www.commandprompt.com/

Donate to the PostgreSQL Project: http://www.postgresql.org/about/donate


From: Chris <dmagick(at)gmail(dot)com>
To: "Joshua D(dot) Drake" <jd(at)commandprompt(dot)com>
Cc: marcosborges(at)mbsi(dot)com(dot)br, pgsql-performance(at)postgresql(dot)org
Subject: Re: SQL_CALC_FOUND_ROWS in POSTGRESQL / Some one can
Date: 2006-12-11 04:36:12
Message-ID: 457CE03C.1060707@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

Joshua D. Drake wrote:
> On Mon, 2006-12-11 at 14:33 +1100, Chris wrote:
>> Marcos Borges wrote:
>>> 07/12/2006 04:31
>>> *SQL_CALC_FOUND_ROWS in POSTGRESQL*
>>>
>>> In mysqln i m using the command SQL_CALC_FOUND_ROWS in follow sintax.
>>> SELECT SQL_CALC_FOUND_ROWS name, email, tel FROM mytable WHERE name <>
>>> '' LIMIT 0, 10
>>> to have the recorset data.
>>> and
>>> SELECT FOUND_ROWS();
>>> to have the total of registers found.
>>>
>>> I dont want to use the command count(*), because the performance will
>>> fall down, depending of the quantyt of tables and "joins".
>>>
>>> The Data base postgresql have something similar ???
>> Nope, you're out of luck sorry. That's a mysql-ism and I doubt postgres
>> will ever include something similar.
>
> Your language will have a similar binding. Something like pg_numrows.

I guess they are similar but also not really :)

The SQL_CALC_FOUND_ROWS directive in mysql will run the same query but
without the limit.

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()"

--
Postgresql & php tutorials
http://www.designmagick.com/


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Chris <dmagick(at)gmail(dot)com>
Cc: "Joshua D(dot) Drake" <jd(at)commandprompt(dot)com>, marcosborges(at)mbsi(dot)com(dot)br, pgsql-performance(at)postgresql(dot)org
Subject: Re: SQL_CALC_FOUND_ROWS in POSTGRESQL / Some one can
Date: 2006-12-11 04:57:18
Message-ID: 26692.1165813038@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

Chris <dmagick(at)gmail(dot)com> writes:
> Their docs explain it:
> http://dev.mysql.com/doc/refman/4.1/en/information-functions.html
> See "FOUND_ROWS()"

Sounds like a pretty ugly crock ...

The functionality as described is to let you fetch only the first N
rows, and then still find out the total number of rows that could have
been returned. You can do that in Postgres with a cursor:

DECLARE c CURSOR FOR SELECT ... (no LIMIT here);
FETCH n FROM c;
MOVE FORWARD ALL IN c;
-- then figure the sum of the number of rows fetched and the
-- rows-moved count reported by MOVE

regards, tom lane


From: Mark Kirkwood <markir(at)paradise(dot)net(dot)nz>
To: Chris <dmagick(at)gmail(dot)com>
Cc: "Joshua D(dot) Drake" <jd(at)commandprompt(dot)com>, marcosborges(at)mbsi(dot)com(dot)br, pgsql-performance(at)postgresql(dot)org
Subject: Re: SQL_CALC_FOUND_ROWS in POSTGRESQL / Some one can
Date: 2006-12-11 05:49:56
Message-ID: 457CF184.6020702@paradise.net.nz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

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


From: Chris <dmagick(at)gmail(dot)com>
To: Mark Kirkwood <markir(at)paradise(dot)net(dot)nz>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: SQL_CALC_FOUND_ROWS in POSTGRESQL / Some one can
Date: 2006-12-11 06:01:11
Message-ID: 457CF427.30507@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

Mark Kirkwood wrote:
> 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.

Sorry - I created a bit of confusion here. It's not doing the count(*),
it's doing the query again without the limit.

ie:

select SQL_CALC_FOUND_ROWS userid, username, password from users limit 10;

will do:

select userid, username, password from users limit 10;

and calculate this:

select userid, username, password from users;

and tell you how many rows that will return (so you can call
'found_rows()').

the second one does do a lot more because it has to send the results
across to the client program - whether the client uses that info or not
doesn't matter.

The OP didn't want to have to change to using two different queries:
select count(*) from table;
select * from table limit 10 offset 0;

Josh's comment was to do the query again without the limit:
select userid, username, password from users;

and then use something like http://www.php.net/pg_numrows to work out
the number of results the query would have returned.. but that would
keep the dataset in memory and eventually with a large enough dataset
cause a problem.

--
Postgresql & php tutorials
http://www.designmagick.com/


From: Andreas Kostyrka <andreas(at)kostyrka(dot)org>
To: Chris <dmagick(at)gmail(dot)com>
Cc: Mark Kirkwood <markir(at)paradise(dot)net(dot)nz>, pgsql-performance(at)postgresql(dot)org
Subject: Re: SQL_CALC_FOUND_ROWS in POSTGRESQL / Some one can
Date: 2006-12-11 06:42:14
Message-ID: 20061211064214.GV2050@andi-lap.la.revver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

* Chris <dmagick(at)gmail(dot)com> [061211 07:01]:
> select SQL_CALC_FOUND_ROWS userid, username, password from users limit 10;
>
> will do:
>
> select userid, username, password from users limit 10;
>
> and calculate this:
>
> select userid, username, password from users;
>
> and tell you how many rows that will return (so you can call 'found_rows()').
>
>
> the second one does do a lot more because it has to send the results across to the client program - whether the client uses that info or not doesn't matter.
Not really. Sending the data to the client is usually (if you are not
connected via some small-bandwidth connection) a trivial cost compared
to calculating the number of rows.

(Our tables involve 100Ms of rows, while the net connectivity is a
private internal Gigabit net, returning the data seems never to be an
issue. Reading it from the disc, selecting the rows are issues. Not
sending the data.)

Actually, if you think that sending the data is an issue, PG offers
the more generic concept of cursors.

Andreas


From: Ragnar <gnari(at)hive(dot)is>
To: Chris <dmagick(at)gmail(dot)com>
Cc: Mark Kirkwood <markir(at)paradise(dot)net(dot)nz>, pgsql-performance(at)postgresql(dot)org
Subject: Re: SQL_CALC_FOUND_ROWS in POSTGRESQL / Some one can
Date: 2006-12-11 09:39:32
Message-ID: 1165829972.6369.26.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

On mán, 2006-12-11 at 17:01 +1100, Chris wrote:
> Mark Kirkwood wrote:
> > 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."

yes but not any faster than a
select count(*) from (full query without LIMIT)

so the only advantage to the SQL_CALC_FOUND_ROWS thingie
is that instead of doing
select count(*) from full-query
select * from query-with-LIMIT
which will do the query twice, but possibly with
different optimisations,

you would do a non-standard
select SQL_CALC_FOUND_ROWS query-with-LIMIT
select FOUND_ROWS()
which will do one full query, without any
LIMIT optimisation, but with the same
number of round-trips, and same amount of
data over the line.

the only case where the second way may be
more effective, is if no LIMIT optimisation
can be made, and where the dataset is larger
than file buffer space, so that there is no
effect from caching.

gnari