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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:11:25+00:00 2026-06-12T18:11:25+00:00

I have a query that when modified ever so slightly will reduce its runtime

  • 0

I have a query that when modified ever so slightly will reduce its runtime from approx 37 seconds down to 4 seconds. There is no change to the joins, or the columns being returned.

SLOW query (37 seconds):

declare @PeriodFrom DateTime
declare @PeriodTo DateTime
Set @PeriodFrom = '2012-06-01'
Set @PeriodTo = '2012-06-30'

Select
  0 as PrimaryAccount,
  0 as PrintOrder,
  Cast(Null as integer) as ID,
  Sum(IsNull(MT.Amount, 0)) as Amount,
  Cast(0 as Money) as NetAmount,
  Cast(0 As Money) as TaxAmount,
  Cast(0 as Money) as AmountOutstanding,
  Cast(0 as Money) as AmountPaid,
  'Balance brought forward' as Description
From
  db_site4.dbo.AccountReceivable P
Join
  db_site4.dbo.ARType ART on ART.ARTypeID = P.ARTypeID and ART.ARTypeID = 24
left Join
  db_site4.dbo.vw_MemberTransactions MT
on
  P.AccountReceivableID = MT.AccountReceivableID
where 
  (MT.AccountingDate <= @PeriodFrom or MT.AccountingDate is null)
and
  (Authorised = 1 or Authorised is Null)
and
  IsHidden = 0
and
  P.MemberID = 123

SQL I/O Statistics for the above:

Table 'Payment'. Scan count 16, logical reads 23558, physical reads 19, read-ahead reads 5448.
Table 'InvoiceItemPayment'. Scan count 4, logical reads 22237, physical reads 51, read-ahead reads 13432.
Table 'UnallocatedPayment'. Scan count 12, logical reads 431, physical reads 1, read-ahead reads 80.
Table 'AccountReceivable'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0.
Table 'ARType'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0.
Table 'Invoice'. Scan count 11116, logical reads 116984, physical reads 190, read-ahead reads 30910.
Table 'InvoiceItem'. Scan count 5122, logical reads 99786, physical reads 316, read-ahead reads 46236.

Now for the query that returns in 4 seconds:

declare @PeriodFrom DateTime
declare @PeriodTo DateTime
Set @PeriodFrom = '2012-06-01'
Set @PeriodTo = '2012-06-30'

Select
  0 as PrimaryAccount,
  0 as PrintOrder,
  Cast(Null as integer) as ID,
  Sum(IsNull(MT.Amount, 0)) as Amount,
  Cast(0 as Money) as NetAmount,
  Cast(0 As Money) as TaxAmount,
  Cast(0 as Money) as AmountOutstanding,
  Cast(0 as Money) as AmountPaid,
  'Balance brought forward' as Description
From
  db_site4.dbo.AccountReceivable P
Join
  db_site4.dbo.ARType ART on ART.ARTypeID = P.ARTypeID and ART.ARTypeID = 24
left Join
  db_site4.dbo.vw_MemberTransactions MT
on
  P.AccountReceivableID = MT.AccountReceivableID
where 
  (MT.AccountingDate <= @PeriodFrom or MT.AccountingDate is null)
and
  (Authorised = 1 or Authorised is Null)
and
  (IsHidden = 0 or IsHidden is null)
and
  P.MemberID = 123

SQL I/O statistics for the above:

Table 'Payment'. Scan count 6271, logical reads 19857, physical reads 0, read-ahead reads 0.
Table 'UnallocatedPayment'. Scan count 2, logical reads 4, physical reads 0, read-ahead reads 0.
Table 'InvoiceItemPayment'. Scan count 4399, logical reads 33400, physical reads 0, read-ahead reads 0.
Table 'InvoiceItem'. Scan count 10581, logical reads 60682, physical reads 4, read-ahead reads 0.
Table 'Invoice'. Scan count 3, logical reads 22102, physical reads 3, read-ahead reads 0.
Table 'AccountReceivable'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0.
Table 'ARType'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0.

My question is: how can there be such a difference in execution time between the two, when the only change that is made is replacing IsHidden = 0 with (IsHidden = 0 or IsHidden IS NULL) ? (3 lines from the bottom)

  • 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-12T18:11:26+00:00Added an answer on June 12, 2026 at 6:11 pm

    When you are adding the additional clause to include ISNULL, then you are likely to be returning a greater number of rows from AccountRecievable. In turn, you are then joining with more rows in the view. This means that is having to read more rows from the views’s base tables, and thus increasing the time of the query.

    The logic here comes from looking at the IO stats you provided. There are additional reads for the tables that are “invisible” to the query you provided. Consider reviewing your index strategy on the view’s base tables.

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

Sidebar

Related Questions

I have a query that successfully grabs the unique products from my products table
I have a query that basically combines tables of actions and selects from them
I have a query that looks like this SELECT * from myTable WHERE Date
I have a query that is pulling users who liked a specific object from
I have a query that was recently required to be modified. Here's the original
I have the following query. Is there an easy way that I could sort
I have simple query that loads data from two tables into GUI. I'm saving
I am using in C# MYsql .I have query that works if I run
I have have this query that i want to execute. SELECT warehouse.expiry_date, pharmacy.expiry_date, drugs.active_substance,
I have a query that creates a result set like this: Rank Name 1

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.