473,422 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,422 software developers and data experts.

SELECT with subquery incredibly slow..

Hey there--

I have a form with two listboxes, each populated by SQL statements that pull from separate tables, like this:

lstAvail.RowSource = SELECT Person_Id, LastName, FirstName FROM HREmps WHERE Person_Id NOT IN (SELECT Person_Id FROM Employees) ORDER BY LastName, FirstName;
lstSel.RowSource = SELECT Person_Id, cLName, cFName FROM Employees ORDER BY cLName, cFName, Person_Id;

This shows all employees in the Employees table (in lstSel) and only those employees in the HREmps table that are not already in Employees (in lstAvail).

The problem is that opening the form takes around 2mins, as does requerying the recordset when a record is copied from HREmps to Employees. I'm assuming this is because Access, in its infinite wisdom, runs the subquery for every record in the HREmps table (about 1900) to compare Person_Id.

I'm wondering if anyone has some thoughts about how to speed this up? Is something wrong with my SQL? Could there be an easier way to do this?

Thanks
Sep 5 '07 #1
4 5254
FishVal
2,653 Expert 2GB
Are the tables [HREmps] and [Employees] in one-to-one relation by [Person_ID]? Or in one-to-many?
Sep 5 '07 #2
JConsulting
603 Expert 512MB
Hey there--

I have a form with two listboxes, each populated by SQL statements that pull from separate tables, like this:

lstAvail.RowSource = SELECT Person_Id, LastName, FirstName FROM HREmps WHERE Person_Id NOT IN (SELECT Person_Id FROM Employees) ORDER BY LastName, FirstName;
lstSel.RowSource = SELECT Person_Id, cLName, cFName FROM Employees ORDER BY cLName, cFName, Person_Id;

This shows all employees in the Employees table (in lstSel) and only those employees in the HREmps table that are not already in Employees (in lstAvail).

The problem is that opening the form takes around 2mins, as does requerying the recordset when a record is copied from HREmps to Employees. I'm assuming this is because Access, in its infinite wisdom, runs the subquery for every record in the HREmps table (about 1900) to compare Person_Id.

I'm wondering if anyone has some thoughts about how to speed this up? Is something wrong with my SQL? Could there be an easier way to do this?

Thanks
you can try this option. Its an unmatched query

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT Person_Id, LastName, FirstName
  3. FROM HREmps LEFT JOIN Employees ON [HREmps ].[Person_Id] = [Employees].[Person_Id]
  4. WHERE ([HREmps ].[Person_Id] Is Null);
  5.  
  6.  
Sep 6 '07 #3
you can try this option. Its an unmatched query

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT Person_Id, LastName, FirstName
  3. FROM HREmps LEFT JOIN Employees ON [HREmps ].[Person_Id] = [Employees].[Person_Id]
  4. WHERE ([HREmps ].[Person_Id] Is Null);
  5.  
  6.  
There was a typo in the WHERE clause; it should be
Expand|Select|Wrap|Line Numbers
  1. WHERE ([Employees].[Person_Id] Is Null);
But that works perfectly! Took the load/requery time from 2mins to 2seconds.

Thanks!
Sep 6 '07 #4
JConsulting
603 Expert 512MB
There was a typo in the WHERE clause; it should be
Expand|Select|Wrap|Line Numbers
  1. WHERE ([Employees].[Person_Id] Is Null);
But that works perfectly! Took the load/requery time from 2mins to 2seconds.

Thanks!
Excellent...I modeled mine then tried pasting in your field names...guess I missed that one :)
Happy to help.
J
Sep 7 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: RCS | last post by:
I've been running into more and more complexity with an application, because as time goes on - we need more and more high-level, rolled-up information. And so I've created views, and views that use...
17
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by...
4
by: stoppal | last post by:
I need some help. I am trying to write a query which does the following SELECT * from table1 where field1=(SELECT distinct field1 FROM table1 WHERE field2='2005' or field2='2010')
23
by: Brian | last post by:
Hello All - I am wondering if anyone has any thoughts on which is better from a performance perspective: a nested Select statement or an Inner Join. For example, I could do either of the...
2
by: Pavel Stehule | last post by:
Hello, Pg make query 1. and 2. very fast (use index), but for query 3. dont use index. I can solve its using select union, but I readed so pg 7.5 don't problem with OR operator. I use cvs pg. I...
4
by: frizzle | last post by:
Hi group. I have a news management system, with a mySQL backend. I tested it yesterday with 1.000.000+ records, testing my url system. I pulled out records calling them by the url field. It was...
3
by: laurentc via AccessMonster.com | last post by:
Hi. I have an issue with my Access project. I have rather big tables of data (about 11 000 rows). These tables are historical product quotations, so they are very simple : - MyDate...
5
by: No bother | last post by:
I am using 5.0.26-NT on Windows 2000. I have need to use a reference in the outer from clause in a subquery in the select clause. Consider the following example: Select (select b.baitID from...
4
by: jordonkraft | last post by:
Table Structure. tblData ---------- fldID (AutoNumber) (PK) fldType (String) fldLocationID (Number) tblLocation ---------------
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.