Unix Technical Forum

Re: slow query execution

This is a discussion on Re: slow query execution within the pgsql Sql forums, part of the PostgreSQL category; --> Hi, Thanks for reply, As you have mentioned I need to get row numbers for my query, so when ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Sql

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-19-2008, 03:26 PM
Trigve Siver
 
Posts: n/a
Default Re: slow query execution

Hi,
Thanks for reply, As you have mentioned I need to get row numbers for my query, so when I make some other query with same data I will know which row number has a particular ID. As you mentioned "You can
do this with a temporary sequence, among other approaches...". Can you point me to some sources or give me some examples, please?

Thanks

Trigve

----- Original Message ----
From: Andrew Sullivan <ajs@crankycanuck.ca>
To: pgsql-sql@postgresql.org
Sent: Wednesday, May 30, 2007 6:45:53 PM
Subject: Re: [SQL] slow query execution

On Wed, May 30, 2007 at 08:56:45AM -0700, Trigve Siver wrote:
> Hi all,
>
> This query executes very slow:
>
> select (select count(*) from customer where id <= a.id) as row, id,
> from customer as a order by id;


So you are trying to get the ordinal position of every ID in the
table? I'm not surprised it takes a long time -- you have to join
the whole table to itself and then do a lot of counting. Are you
just trying to get the "row number" for your query answer? You can
do this with a temporary sequence, among other approaches, more
cheaply.

A


--
Andrew Sullivan | ajs@crankycanuck.ca
Users never remark, "Wow, this software may be buggy and hard
to use, but at least there is a lot of code underneath."
--Damien Katz

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq






__________________________________________________ __________________________________Yahoo! oneSearch: Finally, mobile search
that gives answers, not web links.
http://mobile.yahoo.com/mobileweb/on...h?refer=1ONXIC

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-19-2008, 03:26 PM
=?UTF-8?Q?Rodrigo_De_Le=C3=B3n?=
 
Posts: n/a
Default Re: slow query execution

On 5/30/07, Trigve Siver <trigves@yahoo.com> wrote:
> Can you point me to some sources
> or give me some examples, please?


CREATE OR REPLACE FUNCTION ROWNUM() RETURNS BIGINT AS
$$
BEGIN
RETURN NEXTVAL('ROWNUM_SEQ');
EXCEPTION WHEN OTHERS THEN
CREATE TEMP SEQUENCE ROWNUM_SEQ;
RETURN NEXTVAL('ROWNUM_SEQ');
END;
$$
LANGUAGE 'PLPGSQL';

SELECT ROWNUM(), S.X
FROM GENERATE_SERIES(5,1,-1) S(X);

Remember to reset the sequence value if you use this more than once in
the same session.

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-19-2008, 03:26 PM
Andrew Sullivan
 
Posts: n/a
Default Re: slow query execution

On Wed, May 30, 2007 at 10:03:16AM -0700, Trigve Siver wrote:
> Hi, Thanks for reply, As you have mentioned I need to get row
> numbers for my query, so when I make some other query with same
> data I will know which row number has a particular ID.


Oh, wait. If _that's_ your plan, then this will never work. The
data could change, and your row numbers would come out wrong.
What do you need "row numbers" for anyway? The very idea is inimical
to SQL, because the data is fundamentally unordered.

> As you
> mentioned "You can do this with a temporary sequence, among other
> approaches...". Can you point me to some sources or give me some
> examples, please?


BEGIN;
CREATE SEQUENCE temp_seq;
SELECT nextval('temp_seq'), other stuff from table;
DROP SEQUENCE temp_seq;
COMMIT/ROLLBACK;

If you only select, you don't have to do the DROP, you just ROLLBACK.

I think there's some nifty way to get generate_series to do this too,
but I don't know it offhand (generating row numbers sounds to me like
a bad idea, so I don't do it).

A

--
Andrew Sullivan | ajs@crankycanuck.ca

---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 07:18 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com