Unix Technical Forum

Query tuning

This is a discussion on Query tuning within the pgsql Sql forums, part of the PostgreSQL category; --> Hi, I have a query which is run across 3 tables JOB_TYPE_FIRST, JOB_TYPE_SECOND and JOB_ALLOCATION_WORKLIST. The column JOBID is ...


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 05-16-2008, 01:44 PM
kapil.munish@wipro.com
 
Posts: n/a
Default Query tuning

Hi,





I have a query which is run across 3 tables JOB_TYPE_FIRST,
JOB_TYPE_SECOND and JOB_ALLOCATION_WORKLIST.



The column JOBID is referenced in JOB_ALLOCATION_WORKLIST table and
primary key in both JOB_TYPE_FIRST, JOB_TYPE_SECOND tables.



There is one more column BOOK_ID which is supplied as the binding
parameter to the query. The query looks like:



select count(distinct(j.JOBID)) as jobCount

from JOB_TYPE_FIRST a, JOB_TYPE_SECOND b, JOB_ALLOCATION_WORKLIST j

where (( a.JOBID = j.JOBID)

and (a.BOOK_ID = :bookId))

or ((b.JOBID = j.JOBID)

and (b.BOOK_ID = :bookId));



As the records in the database are too large it is having huge cost and
stalling the server and takes lot of time.



Can anyone suggest a better way to fetch the results for the query or
tune it? Any help would be highly appreciated.





Thanks & Regards,

Kapil


Please do not print this email unless it is absolutely necessary.

The information contained in this electronic message and any attachments tothis message are intended for the exclusive use of the addressee(s) and maycontain proprietary, confidential or privileged information. If you are notthe intended recipient, you should not disseminate, distribute or copy thise-mail. Please notify the sender immediately and destroy all copies of thismessage and any attachments.

WARNING: Computer viruses can be transmitted via email. The recipient shouldcheck this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email.

www.wipro.com

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-16-2008, 01:44 PM
Moiz Kothari
 
Posts: n/a
Default Re: Query tuning

Hi kapil,

Here you have specified 3 tables, does JOB_TYPE_FIRST and JOB_TYPE_SECOND
both contain all the JOBID in the third table?

Maybe i can help you if you elaborate your problem a bit more.

Regards,
Moiz Kothari

On Thu, May 15, 2008 at 11:30 AM, <kapil.munish@wipro.com> wrote:

> Hi,
>
>
>
>
>
> I have a query which is run across 3 tables JOB_TYPE_FIRST, JOB_TYPE_SECOND
> and JOB_ALLOCATION_WORKLIST.
>
>
>
> The column JOBID is referenced in JOB_ALLOCATION_WORKLIST table and primary
> key in both JOB_TYPE_FIRST, JOB_TYPE_SECOND tables.
>
>
>
> There is one more column BOOK_ID which is supplied as the binding parameter
> to the query. The query looks like:
>
>
>
> select count(distinct(j.JOBID)) as jobCount
>
> from JOB_TYPE_FIRST a, JOB_TYPE_SECOND b, JOB_ALLOCATION_WORKLIST j
>
> where (( a.JOBID = j.JOBID)
>
> and (a.BOOK_ID = :bookId))
>
> or ((b.JOBID = j.JOBID)
>
> and (b.BOOK_ID = :bookId));
>
>
>
> As the records in the database are too large it is having huge cost and
> stalling the server and takes lot of time.
>
>
>
> Can anyone suggest a better way to fetch the results for the query or tune
> it? Any help would be highly appreciated.
>
>
>
>
>
> Thanks & Regards,
>
> Kapil
>
> * Please do not print this email unless it is absolutely necessary. *
>
> The information contained in this electronic message and any attachments to
> this message are intended for the exclusive use of the addressee(s) and may
> contain proprietary, confidential or privileged information. If you are not
> the intended recipient, you should not disseminate, distribute or copy this
> e-mail. Please notify the sender immediately and destroy all copies of this
> message and any attachments.
>
> WARNING: Computer viruses can be transmitted via email. The recipient
> should check this email and any attachments for the presence of viruses. The
> company accepts no liability for any damage caused by any virus transmitted
> by this email.
>
> www.wipro.com
>




--
Hobby Site : http://dailyhealthtips.blogspot.com

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-18-2008, 10:04 PM
Simon Riggs
 
Posts: n/a
Default Re: Query tuning


On Fri, 2008-05-16 at 09:21 -0600, Scott Marlowe wrote:
> On Thu, May 15, 2008 at 12:00 AM, <kapil.munish@wipro.com> wrote:
> > Hi,
> >
> > select count(distinct(j.JOBID)) as jobCount
> > from JOB_TYPE_FIRST a, JOB_TYPE_SECOND b, JOB_ALLOCATION_WORKLIST j
> > where (( a.JOBID = j.JOBID)
> > and (a.BOOK_ID = :bookId))
> > or ((b.JOBID = j.JOBID)
> > and (b.BOOK_ID = :bookId));

>
> Have you tried joining a with j and b with j separately, and then
> doing a union on those two sets?


That seems the only way to get a sane answer to this query, which is
otherwise an unconstrained join on both sides of the OR. Great example
of a query which runs slow because the question is phrased incorrectly.
Count(distinct) is pretty much the only function that will give the same
answer as a correctly phrased query.

--
Simon Riggs www.2ndQuadrant.com
PostgreSQL Training, Services and Support


--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql

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 03:38 AM.


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