Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query Optimization Question

Status
Not open for further replies.

SamirNaenenjad

Programmer
Mar 20, 2002
13
0
0
US
I have an application with an Access backend and I am trying to populate a list box with all the records from table A that aren't in table B. Right now, I am doing this with the following statement:

"SELECT Name, FROM A WHERE [Active] = 'Yes' AND Name NOT IN (SELECT Name FROM B WHERE SalesID = '001')"

This is workable when table B has a few records in it, but when I have up to 6,000 records in table B meeting the criteria, the query takes over a half hour to run (it is still running now).

Does anyone know if there is a way I can write a query like this that Access can handle better? Are there other alternatives I may be looking past? I have indexed the fields that matter, but it still isn't workable. Thanks in advance for any help.

-A.J.
 
You're missing a join. Check out the Find Unmatched Query Wizard.
 
SELECT A.Name, A.Active, B.Name
FROM A LEFT JOIN B ON A.Name = B.Name
WHERE (((B.Name) Is Null AND (A.Active) = 'Yes'));
 
Thanks every one, this is working much better. Just curious, is this just the way Access likes it, or should I stay away from "NOT IN" queries with SQL Server, etc?

-A.J.
 
Actually, Access is processing the query from the complete recordset. If you're using SQL Server, you'll probably want to create a stored procedure instead. What I gave you was a typical unmatched query structure. If you use the unmatched query wizard this is what it will create for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top