Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Optimising Query Question and Help Please

Author  Topic 

MRBax
Starting Member

3 Posts

Posted - 2014-06-14 : 06:29:46
Could anyone please give me any ideas on ways to optimise/speed up this kind of query?

There are only around 15,000 records that are being looked up here, but the cross checking really slows the query down (and causes script timeouts when running through the website (.asp)).
I know I could increase the 'page timeout' value, but it would be much better if I knew how best to optimise this kind of query first!

General idea is to select an email address to use for a mailshot, but I need to first check that;
1) is not on the opt out list
2) has not already been sent this mailshot
3) is in a specific country
4) the record is active

So the general idea is:
select distinct email from tblInTouchMailshot where
email not in table X
and email not in table Y
and email is in Country Z
and record is active

The Actual SQL is:
select distinct email from tblInTouchMailshot where
(email NOT IN (SELECT distinct email FROM tblInTouchMailshotOptOut))
AND (email NOT IN (SELECT distinct email FROM tblInTouchMailshotSent where newsID=223))
AND (email IN (SELECT distinct email FROM tblCustomer where [countryID]=1))
AND active=1 group by email,firstName

I've never had to deal with issues like this before so I don't even know where to begin optimising anything - If anyone can see anything in here that could be improved or otimised then any help would be greatfully received.

Thanks,
Mark

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2014-06-14 : 07:05:48
You need to find the proper fields to join the tables, preferably the primary key. Is not the primary key, then at least a field with index.
Assuming you have find those fields, you can use either of these queries, replacing the fields marked in red, with the correct fieldnames:
select a.email
,a.firstName
from tblInTouchMailshot as a
left outer join tblInTouchMailshotOptOut as b
on b.id=a.id
and b.email=a.email
left outer join tblInTouchMailshotSent as c
on c.id=a.id
and c.email=a.email
and c.newsID=223
left outer join tblCustomer as d
on d.id=a.customerID
and d.email=a.email
and CountryID=1

where a.active=1
and b.email is null
and c.email is null
and d.email is null
group by a.email
,a.firstName
-----------------
select email
,firstName
from tblInTouchMailshot as a
where active=1
and not exists (select 1
from tblInTouchMailshotOptOut as b
where b.id=a.id
and b.email=a.email
)
and not exists (select 1
from tblInTouchMailshotSent as c
where c.id=a.id
and c.email=a.email
and c.newsID=223
)
and not exists (select 1
from tblCustomer as d
where d.id=a.customerID
and d.email=a.email
and d.countryID=1
)
Go to Top of Page

MRBax
Starting Member

3 Posts

Posted - 2014-06-14 : 08:12:36
Many thanks for your (prompt!) reply :)

Yes, each table has a primary key by the way.

That's interesting though, I would have automatically assumed that introducing extra fields (even primary keys) would have added yet more processing time given that it's the email field that still needs to be compared/looked up.

I will try these and see what a difference it makes by simply timing it I guess!

Thanks again.
Mark
Go to Top of Page

MRBax
Starting Member

3 Posts

Posted - 2014-06-19 : 08:44:11
Hi bitsmed,

I just wanted to say a huge thank you for your help with this.

The biggest thing I took away from this is that I should be using "select 1 where email = whatever" when cross referencing other tables - instead of "select all" and then checking - this single (and now OBVIOUS) change has converted a 1 minute PLUS query into a SUB 1 SECOND query! This has made a huge difference to performance and opened my eyes to more carefully considered querying in future!

Many, thank thanks.
Go to Top of Page

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2014-06-19 : 12:06:03
You'r welcome.
I'm glad I could help :-)
Go to Top of Page
   

- Advertisement -