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 8993655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:14:23+00:00 2026-06-15T23:14:23+00:00

I’m making some modifications to improve the performance of an old report query written

  • 0

I’m making some modifications to improve the performance of an old report query written 1000 years ago. In the process of modify it to do a UNION ALL, I’ve replaced one of the column selection values with NULL. As soon as I do that the query changes from taking 1-2 seconds to execute to 30 second to execute. I’ve looked at the actual execution plans for the two versions and they look identical. It makes no sense to me that selecting literal NULL could be slower than reading a row value. I’ve also tried explicitly casting NULL to the former type (nvarchar) as well as selecting ” instead of NULL with no difference.

The query and schema is very complex so this will probably take some Q&A troubleshooting. The column slowing it down when selecting NULL is “OtherComments” near the bottom. You can see the original version, that runs fast, commented above it. Just to keep us on track, I’m only looking to understand why changing that column makes it run slowly, not other ways to improve the query (I know there are lots). Here’s an abbreviated version:

SELECT @Date                                         Parameter,
   fml.FirstName + ' ' + fml.LastName                ParentName,
   (
           SELECT TOP 1 p.PhoneNum
           FROM   tbl_Phone p, tbl_PhoneTypes pt, tbl_FamilyPhone fp
           WHERE  p.fk_PhoneTypeID = pt.pk_PhoneTypeID
           AND    p.pk_PhoneID = fp.fk_PhoneID
           AND    fp.fk_FamilyID = fml.pk_familyID
           AND    pt.Type = 'home' 
           AND    fp.IsDeleted = 0
           ORDER BY fp.CreatedDate DESC
   )                                                 PhoneNo,
   fml.Comments                                      FamilyComments,
   std.FirstName + ' ' + std.LastName                StudentName,
   (
           SELECT
           ...
   )                                                 ClassDescription,

   (
           SELECT
           ...
   )                                                 TestClassDescription,
   CASE
      WHEN (sce.pk_StudentEnrollmentID IS NOT NULL) THEN
         (
         SELECT emp.FirstName + ' ' + emp.LastName
         ...
         )
      WHEN (st.pk_StudentTestID IS NOT NULL) THEN
         (
         SELECT emp.FirstName + ' ' + emp.LastName
         ...
         )
      ELSE
         NULL
   END                                               InstructorName,
        CASE
              WHEN (st.pk_StudentTestID IS NOT NULL) THEN
                 (
                 SELECT emp.FirstName + ' ' + emp.LastName
                 ...
                 )
              ELSE
                 NULL
               END                                       TestInstructorName,
   st.TestDate                                      TestDate,
   tr.Description                                    TestResult,
       CASE
          WHEN (
           SELECT COUNT(ClassDate)                                                  --Select absent attendances from yesterday
           ...
           ) >= 1 
          THEN
               CAST(1 AS BIT)
          ELSE 
               CAST(0 AS BIT)
       END
                                                         MissedYesterdaysClass,
   CASE
      WHEN (datediff(day, CONVERT(varchar(11),fml.InquiryDate,102), @Date) =3 and
           (fml.CurrentMembershipDate IS NULL) AND ((fml.WebCreated = 0) OR (fml.WebCreated = NULL)) AND
           ((
           SELECT count(*)
           ...
           ) <= 0) AND
           ((
           SELECT count(*)
           ...
           ) <= 0) AND
           ((
           SELECT count(*)
           ...
           ) <= 0) AND
           ((
           SELECT count(*)
           ...
           ) <= 0) AND
           ((
           SELECT count(*)
           ...
           ) <= 0)) THEN
         CAST(1 AS BIT)
      WHEN (((fml.InquiryDate + 3) = @Date) AND (std.pk_StudentID IS NULL)) THEN
         CAST(1 AS BIT)
      ELSE
         CAST(0 AS BIT)
   END                                               InquiredButDidnotSchedule,
   CASE
      WHEN --(((st.TestDate + 2) = @Date) AND
            (datediff(day, CONVERT(varchar(11),st.testdate,102), @Date) =2 and
           (tr.Description = 'Not Enrolled') AND
               (st.IsDeleted = 0) AND
           (st.IsCancelled = 0)) THEN
         CAST(1 AS BIT)
      ELSE
         CAST(0 AS BIT)
   END                                               AttendedButHavenotEnrolled,
   CASE
      WHEN --(((st.TestDate + 1) = @Date) AND
                (datediff(day, CONVERT(varchar(11),st.testdate,102), @Date) =1 and
           (tr.Description = 'No Show')  AND
               (st.IsDeleted = 0) AND
           (st.IsCancelled = 0)) THEN
         CAST(1 AS BIT)
      ELSE
         CAST(0 AS BIT)
   END                                               PeopleNoShowed,
   CASE
      WHEN ---(((st.TestDate - 1) = @Date) AND
            (datediff(day, CONVERT(varchar(11),st.testdate,102), @Date) =-1 and
           (tr.Description = 'Scheduled')  AND
               (st.IsDeleted = 0) AND
           (st.IsCancelled = 0)) THEN
         CAST(1 AS BIT)
      ELSE
         CAST(0 AS BIT)
   END                                               PeopleHaveTest,
       CAST(0 AS BIT)                                    ShowOtherComments,

CASE
          WHEN  
                ((   
                SELECT count(*)
                ...
                ) > 0) AND
                ((
                SELECT EventDate
                ...
                )
                    BETWEEN 
                    (
                        DateAdd(day, DateDiff(day, 0, @Date), 0)
                    )
                    AND
                    (
                        (DateAdd(day, DateDiff(day, 0, @Date), 0) + 6)
                    )
                )
                AND
                datename(weekday, @Date) = 'Wednesday'
                AND
                bb.IsDeleted = 0
                AND
                bb.IsCancelled = 0 THEN
             CAST(1 AS BIT)
          ELSE
             CAST(0 AS BIT)
       END                                               ShowUpcomingBookingss,
       (
       SELECT EventDate
       ...
       )                                                 BookingDate,
       bb.ChildTurningAge                                Age,
   std.pk_StudentID                                  StudentID,
       fml.pk_familyID                                   FamilyID,
       CASE
      WHEN (datediff(day, CONVERT(varchar(11),fml.InquiryDate,102), @Date) =1 and
           (fml.MembershipDate IS NULL) AND (fml.WebCreated = 1) AND
           ((
           SELECT count(*)
           ...
           ) <= 0) AND
           ((
           SELECT count(*)
           ...
           ) <= 0) AND
           ((
           SELECT count(*)
           ...
           ) <= 0) AND
           ((
           SELECT count(*)
           ...
           ) <= 0) AND
           ((
           SELECT count(*)
           ...
           ) <= 0)) THEN
         CAST(1 AS BIT)
      WHEN (((fml.InquiryDate + 1) = @Date) AND (std.pk_StudentID IS NULL)) THEN
         CAST(1 AS BIT)
      ELSE
         CAST(0 AS BIT)
   END                                               InquiredButDidnotScheduleOnline,

--     Commenting this out and replacing with NULL slows it down from 2 sec to 30 sec
--     fml.OtherComment                        OtherComments,
       NULL                       OtherComments,
    FROM   tbl_Family fml
    LEFT OUTER JOIN tbl_Student std on fml.pk_FamilyID = std.fk_FamilyID
    LEFT OUTER JOIN tbl_StudentEnrollment sce on std.pk_StudentID = sce.fk_StudentID
    LEFT OUTER JOIN tbl_StudentTest st on std.pk_StudentID = st.fk_StudentID
    LEFT OUTER JOIN tbl_Booking bb on std.pk_StudentID = bb.fk_StudentID
    LEFT JOIN tbl_TestResult tr on st.fk_TestResultID = tr.pk_TestResultID
    WHERE  fml.fk_FacilityID = @FacilityID
    AND    (fml.IsDeleted = 0 OR fml.IsDeleted IS NULL)
    AND    (std.IsDeleted = 0 OR std.IsDeleted IS NULL)
  • 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-06-15T23:14:25+00:00Added an answer on June 15, 2026 at 11:14 pm

    I compared the actual XML of the query plans and noticed the memory grant was higher for the fast sproc than the slow sproc. This was explained to me in the MS forums and probably being because when fml.OtherComment is in the query, it is expecting a larger row size. I don’t know a solution if you were run into this with a well written query, but in my case, this spoc was so inefficient (selecting the final records in a round about way) that I rewrote it from the ground up and it’s running fast now.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.