Solved

Convert NOT IN Sub-Select to LEFT JOIN

Posted on 2011-09-30
12
314 Views
Last Modified: 2012-05-12
When I run this query:

select distinct idQuestion from Question
where parentid is null AND idQuestion not in (select distinct parentid from Question where parentid is not null)
order by idQuestion

Open in new window


The query never finishes after waiting several minutes. I've researched the issue and the conclusion is to use a LEFT JOIN instead of a sub-select. However, I'm not sure how to properly construct the query with these limitations.
0
Comment
Question by:BCRobert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
12 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36891743
please try this:

select q.idQuestion 
from Question q
where q.parentid is null 
AND NOT EXISTS ( select NULL from Question o where o.parentid = q.idQuestion )
order by idQuestion 

Open in new window


and please ensure you have an index on parentid
0
 
LVL 5

Expert Comment

by:eridanix
ID: 36891750
Hi,

I mean you have little nonsense in your query, becouse you compare idQuestion with parentid

The correct query should only be:
select distinct idQuestion
from Question
where parentid is not null
order by idQuestion

Maybe, you need get something else. So try to explain what shloud be result of this query.
0
 

Author Comment

by:BCRobert
ID: 36891794
@angellll: parentid isn't indexed because it's not unique. This query is taking just as long.

@eridanix: That query won't work. Here's what I'm trying to find:

Find the question IDs that are not a parent ID of another question and the parentid is null.

idQuestion    |    data     |    parentid
123               |    abc      |       NULL
321               |    def       |        123
444               |    qqq       |    NULL

Open in new window


The query should return just '444' as its parentid is null AND is not a parentid of another question.
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 5

Expert Comment

by:eridanix
ID: 36891882
select distinct idQuestion
from Question
where parentid is null and idQuestion NOT IN (select idQuestion from Question where parentid is not null)
order by idQuestion
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 36891919
>parentid isn't indexed because it's not unique. T

indexes can be on non-unique fields.
please create such an index.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36891930
edit: you might not know the a primary key is a UNIQUE index, under the hood, but you can created non-unique indexes.
actually, a index is non-unique by default, unless you specify it to add the restriction that each value can only be present once.
0
 

Author Comment

by:BCRobert
ID: 36891937
@eridanix: That will not return the results I need. I need to make sure the the idQuestion IS NOT the parentid of another question.
0
 

Author Comment

by:BCRobert
ID: 36891968
@angellll: The parentid is already an index, apparently (I didn't create the database/tables):

parentIdIdx      BTREE      No      No      parentId      41002      A      YES      
0
 
LVL 5

Expert Comment

by:eridanix
ID: 36892014
select idQuestion
from dbo.Questions
where idQuestion IN (select idQuestion from Question where parentid is null) AND idQuestion NOT IN  (select parentid from Question where parentid is not null)
0
 

Author Comment

by:BCRobert
ID: 36892103
@angellll: It seems that re-creating the index helped tremendously. Not sure why, but the query finished after about 45 seconds, which is still slow, but it's not a query we'll be running often.
0
 

Author Closing Comment

by:BCRobert
ID: 36892108
Creating/recreating the index helped the initial query finished. Converting it to LEFT JOIN/NOT IN isn't needed.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36892237
please try to remove the 2 DISTINCT in the query.

IN ( SELECT DISTINCT  ... ) will be the same, but slower normally, as
IN ( SELECT ... )

the NOT EXISTS () version I posted should be fastest ...
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
IIF in access query 19 41
Select question from MySQL 1 20
Concat multiple records into one line 3 43
mysql qry 1 23
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question