Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6739471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:29:36+00:00 2026-05-26T11:29:36+00:00

I’m running a stored procedure with multiple joins with one table that contains over

  • 0

I’m running a stored procedure with multiple joins with one table that contains over 600,000 records. The problem is the procedure is very slow and can take minutes to execute. We have indexed the relevant tables columns and still no luck.

What can we do to help improve performance? The query is posted below.

Thanks

with CTE
as
(
select * from 
(
    select distinct c.ContactId, c.FirstName, c.LastName, 
    (select top 1 ce.Email from dbo.ContactEmails as ce where ce.ContactId =  c.ContactId and ce.IsPrimary = 1) as Email, 
    comp.CompanyName, j.JobName, c.MobileNumber, c.OfficeNumber, cse.DateSent, MAX(cse.DateSent) over(partition by ce.email) as maxdate 

    from dbo.ContactSentEmails as cse

    join dbo.ContactEmails as ce on cse.ContactId = ce.ContactId
    join dbo.Contacts as c on ce.ContactId = c.ContactId
    left join dbo.Jobs as j on c.JobId = j.JobId
    left join dbo.Companies as comp on c.CompanyId = comp.CompanyId

    join dbo.StaffProjects as sp on cse.StaffProjectId = sp.StaffProjectId
    join dbo.Staff as s on sp.StaffId = s.StaffId
    join dbo.Projects as p on sp.ProjectId = p.ProjectId

    where (@ContactSourceId = -1 or c.ContactSourceId = @ContactSourceId)
    and (@FirstName = '' OR c.FirstName LIKE '%' + @FirstName + '%')
    and (@LastName = '' OR c.LastName LIKE '%' + @LastName + '%')
    and (@EmailAddress = '' OR ce.Email LIKE '%' + @EmailAddress + '%')
    and (@StaffId = -1 or sp.StaffId = @StaffId)
    and (@ProjectId = -1 or sp.ProjectId = @ProjectId)
    and (@OfficeId = -1 or p.OfficeId = @OfficeId)
    and cse.DateSent between CONVERT(datetime, @startDate) and CONVERT(datetime, @endDate)

    group by c.ContactId, c.FirstName, c.LastName, Email,comp.CompanyName, j.JobName, c.MobileNumber, c.OfficeNumber, cse.DateSent
) as tbContacts
)

select ContactId, FirstName, LastName, Email, CompanyName, JobName, MobileNumber, OfficeNumber from CTE where cte.DateSent = CTE.maxdate order by CTE.Email
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:29:36+00:00Added an answer on May 26, 2026 at 11:29 am

    Many defects!

    Defect 1: No plan can be chosen without looking at supplied values.

    (@ContactSourceId = -1 or c.ContactSourceId = @ContactSourceId) 
    

    If @ContactSourceId is -1 you have one query execution plan. If ContactSourceId is not -1, you have another query execution plan. If you want the correct execution plan to be chosen, you’ll need to provide a query that knows what it is supposed to be filter without looking at the values of the variables.

    Since you’ve used this criteria construct 7 times, there are 2^7 potential plans and your odds of getting the right one is 1/2^7 = 1/128 < 1%

    You need to break this query text into 128 different queries – the query optimizer will only do this for you badly.


    Defect 2: Failure to use SARGable search criteria

    One of those 128 queries works like this: Suppose that @FirstName was supplied and the other variables aren’t. In this case, @FirstName is the primary criteria to access the Contacts table.

    c.FirstName LIKE '%' + @FirstName + '%'
    

    If you wrote a query with just this criteria on the Contacts table – there would be no index you can add to the Contacts table that would be used. You are doomed to table scan. Learn about SARGable search criteria.


    Defect 3: Operations on each row, yields same result for each row.

    cse.DateSent between CONVERT(datetime, @startDate) and CONVERT(datetime, @endDate)
    

    Why are you converting a variable to DateTime on each row? Do those convert before running the query.


    Defect 4: 90% of the time Distinct is a crutch

    distinct
    top 1
    group by
    

    So many “give me just one” operators in a query means the query author was just trying stuff and seeing what sticks. Simplify to the actual intent. My guess is the distinct is not needed. If you add distinct when you don’t need it – you still pay for it!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.