Gert-Jan Strik
3/23/2007 5:18:00 PM
Here is another - complete different approach - to the same query. Make
sure the tables in the view are properly indexed, and make sure there is
an index on tblLead(leadid). Test both your original query, Dan's
version and the version below, and keep the fastest.
SELECT TOP 1 *
FROM (
SELECT leadid, createdate, timestamp, AffiliateName
FROM tblLead L
INNER JOIN vwActiveLead AL
ON AL.DayPhone = L.DayPhone
WHERE leadid = @LeadID
UNION ALL
SELECT leadid, createdate, timestamp, AffiliateName
FROM tblLead L
INNER JOIN vwActiveLead AL
ON AL.DayPhone = L.WorkPhone
WHERE leadid = @LeadID
UNION ALL
SELECT leadid, createdate, timestamp, AffiliateName
FROM tblLead L
INNER JOIN vwActiveLead AL
ON AL.WorkPhone = L.WorkPhone
WHERE leadid = @LeadID
UNION ALL
SELECT leadid, createdate, timestamp, AffiliateName
FROM tblLead L
INNER JOIN vwActiveLead AL
ON AL.WorkPhone = L.DayPhone -- not in original query
WHERE leadid = @LeadID
UNION ALL
SELECT leadid, createdate, timestamp, AffiliateName
FROM tblLead L
INNER JOIN vwActiveLead AL
ON AL.Email = L.Email
WHERE leadid = @LeadID
) AS all_qualifying_leads
ORDER BY createdate
HTH,
Gert-Jan
rh1200la@gmail.com wrote:
>
> Here you go. It's for a leads database. This query is getting the
> first lead from a view, but the criteria is against the whole table.
> It's doing a duplicate analysis.
>
> Thanks.
>
> select top 1 leadid, createdate, timestamp, AffiliateName from
> vwActiveLead where
> (DayPhone = (select DayPhone from tblLead where leadid = @LeadID) or
> DayPhone = (select WorkPhone from tblLead where leadid = @LeadID)) OR
> (WorkPhone = (select DayPhone from tblLead where leadid = @LeadID) or
> DayPhone = (select WorkPhone from tblLead where leadid = @LeadID)) OR
> Email = (select Email from tblLead where leadid = @LeadID)
> order by createdate asc
>
> On Mar 21, 5:18 pm, "Dan Guzman" <guzma...@nospam-
> online.sbcglobal.net> wrote:
> > > Aside from indexing, are
> > > there any other ways to optimize such a query?
> >
> > Subqueries can be used in a number of ways. It's difficult to make
> > recommendations without seeing the actual query and DDL. Consider posting
> > that information so that we can better help you out.
> >
> > --
> > Hope this helps.
> >
> > Dan Guzman
> > SQL Server MVP
> >
> > <rh120...@gmail.com> wrote in message
> >
> > news:1174521785.424909.255550@e1g2000hsg.googlegroups.com...
> >
> > > Hey All. I have a stored procedure that, in the where clause has 4
> > > subqueries. I just ran a sql trace on the db and it's usinq quite a
> > > bit of CPU (compared to the other queries). Aside from indexing, are
> > > there any other ways to optimize such a query?
> >
> > > Thanks.