If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > PostgreSQL > what is wrong with this quuery?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-20-09, 03:57
saasira saasira is offline
Registered User
 
Join Date: Jun 2009
Posts: 8
what is wrong with this quuery?

Hi, Can some one explain me how to optimise this query?

select disticnt(csuser.id), csuser.loginName,csuser.displayName,loginSession.l astlogindatetime,loginSession.activesessions,usert ypes.csusertypeid from User csuser join userroleassign csuserroleassign on csuserroleassign.roleid=52 left outer join userloginsession csloginSession on csloginSession.userid=csuser.id left outer join userusertypeassign usertypes on usertypes.userid=csuser.id limit 15;

If I don't add that distcint keyword, query is returning results in 18 milli seconds; but if I add distcinct it did not return results in two miunutes and finally I have to stop the exection of the query.

Interestingly, if I say select distinct(user.id) from user; then it is pretty quick and returning in 15 milliseconds.

Sorry for asking such a basic question but I'm not that good at SQL.

Thanks and Regards,
Samba
Reply With Quote
  #2 (permalink)  
Old 07-20-09, 06:26
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
DISTINCT is ~not~ a function

if you put a column in parentheses after DISTINCT, that does not change what DISTINCT does -- DISTINCT applies to all columns in the SELECT clause

DISTINCT is implemented by a sort on the result set, sorting on all columns, then looking for duplicate rows, i.e. duplicate in all columns
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 07-20-09, 06:32
saasira saasira is offline
Registered User
 
Join Date: Jun 2009
Posts: 8
Thanks

thanks for the clarification
Reply With Quote
  #4 (permalink)  
Old 07-22-09, 09:56
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
Did you miss a join condition on the userroleassign csuserroleassign table? Your example shows the only limiting criteria on that table is the role id being a 52. Without join criteria to your other tables this would create a cartesian product, which could explain the length of time it takes to sort all rows. Also, when you say data is returned in 18 milliseconds without the distinct, is that all rows or just a first subset of the rows?
Dave
Reply With Quote
  #5 (permalink)  
Old 07-23-09, 11:27
saasira saasira is offline
Registered User
 
Join Date: Jun 2009
Posts: 8
Hi Dave,
I meant that the first subset of data is returned in around 18 millisconds if I don't say distinct, which obviously leaves out some records which do not in the subset.

what I have:

User*----*Role
User-----*loginsession
User----*UserType
User-------*Profile
defaultProfile-----*Address
Address----phone

here userroleassign and userusertypeassign are the jooin tables for many to many associations between user==>role and user==>usertype respectively.

Now I want some fields from user,some fields from usertype,some fields from
loginSession,and phone number from his defaultprofile-->adresss where user has a certain role.

I could not come up with a query that gives me the above explained data set.

Can you help build a query that gets the above resultset?

Thanks and Regards,
Samba

Last edited by saasira; 07-24-09 at 12:10.
Reply With Quote
  #6 (permalink)  
Old 07-29-09, 15:49
rski rski is offline
Registered User
 
Join Date: Nov 2006
Posts: 82
Quote:
Originally Posted by r937
DISTINCT is ~not~ a function

if you put a column in parentheses after DISTINCT, that does not change what DISTINCT does -- DISTINCT applies to all columns in the SELECT clause

DISTINCT is implemented by a sort on the result set, sorting on all columns, then looking for duplicate rows, i.e. duplicate in all columns
I'm afraid it is not true. Distinct do not have to aplly to all columns, if you use
Code:
dictinct on (..)
then distinct will apply only to those column you write in parentheses
Reply With Quote
  #7 (permalink)  
Old 07-29-09, 15:53
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by rski
I'm afraid it is not true. Distinct do not have to aplly to all columns, if you use
Code:
dictinct on (..)
then distinct will apply only to those column you write in parentheses
yes, but....
Quote:
Originally Posted by da postgresql manual
The DISTINCT ON clause is not part of the SQL standard and is sometimes considered bad style because of the potentially indeterminate nature of its results.
my advice is to stay away from non-standard SQL unless there is a real good reason

and this aint one of them

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 07-29-09, 15:59
rski rski is offline
Registered User
 
Join Date: Nov 2006
Posts: 82
OK, but you can't say that
Quote:
if you put a column in parentheses after DISTINCT, that does not change what DISTINCT does -- DISTINCT applies to all columns in the SELECT clause
cos in postgres it is not true .
Reply With Quote
  #9 (permalink)  
Old 07-29-09, 16:02
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by rski
cos in postgres it is not true .
it is so true!!!

you are getting confused

in postgresql, DISTINCT ON and DISTINCT are two completely separate things

i was talking about putting an expression into parentheses after the DISTINCT

DISTINCT ON is completely different semantics
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 07-29-09, 16:12
rski rski is offline
Registered User
 
Join Date: Nov 2006
Posts: 82
You are of course right
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On