Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am using MS Access database and below is the query I am using, but it taking too much of time (5min) to retrieve the data.

Please check and give some suggestions.

SQL
select distinct (a.customer) from customermaster as a,PMRegister as b where a.active='T' and a.sectorName='Jayanagar' and a.customer not in(select customer from PMRegister where active='T' and month(dt) <= month(cdate(1/12/2012)) and month(dt) >= month(cdate(11/12/2012)) and year(dt) <= year(cdate(1/12/2012)) and year(dt) >= year(cdate(11/12/2012)))  order by a.Customer
Posted
Updated 11-Dec-12 21:45pm
v2

1 solution

Your query is taking so much time as there are many AND conditions.
And the main problem is not in() function which generally takes more time.
You should avoid that.

Instead of not in, try to INNER JOIN[^] the tables to fetch the data faster.

I am not using MS-Access. So, I am not sure whether the following query will work or not.

But I tried to optimize the query. Take a look.
SQL
SELECT DISTINCT (a.customer) 
FROM 
	customermaster as a
INNER JOIN 
	PMRegister as b
ON a.customer = b.customer
AND a.active = 'T'
AND a.sectorName = 'Jayanagar'
AND MONTH(dt) >= MONTH(cdate(11/12/2012)) 
AND YEAR(dt) <= YEAR(cdate(1/12/2012)) 
AND YEAR(dt) >= YEAR(cdate(11/12/2012)))
 
ORDER BY a.Customer

I may be wrong. Please try to implement INNER JOIN and eliminate the extra conditions as far as possible.

Thanks...
 
Share this answer
 
Comments
Please accept this answer, if it has helped you in any way.
This will help others to find the answer in one go and you will also be awarded with some points for this action...

Thanks,
Tadit

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900