very slow query

the query below is working but takes forever. well over a minute to go through 300 records. surely there must be a more efficient alternative. the vwClientMembers is actually a query that looks across a network into an access database. I get the feeling it is going back and forth unnecessarily.

any ideas?



SELECT     EmailAbsolute, Nom, Prenom, ContactID, Country, ClientLanguage, Prefix, EmailAbsolute AS NumOccurrences
FROM         vwClientMembers AS q1
WHERE     (NOT EXISTS
                          (SELECT     NULL AS Expr1
                            FROM          members
                            WHERE      (active = 1) AND (q1.EmailAbsolute = email)))
AND q1.EmailAbsolute in
(SELECT EmailAbsolute FROM vwClientMembers GROUP BY EmailAbsolute
HAVING (COUNT(*) > 1))
LVL 1
ShawnAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

danrosenthalCommented:
Give this a try...
SELECT	
	EmailAbsolute, Nom, Prenom, ContactID, Country, ClientLanguage
	, Prefix, EmailAbsolute AS NumOccurrences
FROM	
	vwClientMembers AS q1
WHERE
	q1.EmailAbsolute NOT IN ( 
		SELECT email
		FROM members
		WHERE active = 1
	) 
AND q1.EmailAbsolute IN (
		SELECT EmailAbsolute 
		FROM vwClientMembers 
		GROUP BY EmailAbsolute
		HAVING (COUNT(*) > 1)
	)

Open in new window

ShawnAuthor Commented:
still well over a miniute. pretty much the same amount of time
SharathData EngineerCommented:
try this
SELECT     EmailAbsolute, Nom, Prenom, ContactID, Country, ClientLanguage, Prefix, EmailAbsolute AS NumOccurrences
FROM         vwClientMembers AS q1
JOIN (SELECT EmailAbsolute FROM vwClientMembers GROUP BY EmailAbsolute 
HAVING (COUNT(*) > 1)) cm ON q1.EmailAbsolute = cm.EmailAbsolute
LEFT JOIN members m1 ON m1.active = 1 and q1.EmailAbsolute = m1.email
WHERE m1.email IS NULL

Open in new window

Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

ShawnAuthor Commented:
getting various errors here:
Msg 209, Level 16, State 1, Line 1
Ambiguous column name 'EmailAbsolute'.
Msg 209, Level 16, State 1, Line 1
Ambiguous column name 'ContactID'.
Msg 209, Level 16, State 1, Line 1
Ambiguous column name 'Country'.
Msg 209, Level 16, State 1, Line 1
Ambiguous column name 'EmailAbsolute'.
SharathData EngineerCommented:
SELECT     q1.EmailAbsolute, q1.Nom, q1.Prenom, q1.ContactID, q1.Country, q1.ClientLanguage, q1.Prefix, q1.EmailAbsolute AS NumOccurrences
FROM         vwClientMembers AS q1
JOIN (SELECT EmailAbsolute FROM vwClientMembers GROUP BY EmailAbsolute
HAVING (COUNT(*) > 1)) cm ON q1.EmailAbsolute = cm.EmailAbsolute
LEFT JOIN members m1 ON m1.active = 1 and q1.EmailAbsolute = m1.email
WHERE m1.email IS NULL

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Olaf DoschkeSoftware DeveloperCommented:
Is there an index in each table on the email fields?

Bye, Olaf.
ShawnAuthor Commented:
brilliant! lightning fast now.

thank you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.