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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:05:02+00:00 2026-06-05T19:05:02+00:00

We’ve got a query that is taking a very long time to complete with

  • 0

We’ve got a query that is taking a very long time to complete with a large dataset. I think I’ve tracked it down to a table-value function in the SQL server.

The query is designed to return the difference in printing usage between two dates. So if a printer had usage of 100 at date x and 200 at date y a row needs to be returned which reflects that it has had a usage change of 100.

These readings are taken periodically (but not every day) and stored in a table called MeterReadings. The code for the table-value function is below. This is then called from another SQL query which joins the returned table on a devices table with an inner join to get extra device information.

Any advise as to how to optimise the below would be appreciated.

ALTER FUNCTION [dbo].[DeviceUsage]
-- Add the parameters for the stored procedure here
( @StartDate DateTime , @EndDate DateTime )
RETURNS table
AS
RETURN
(

SELECT      MAX(dbo.MeterReadings.ScanDateTime) AS MX,
        MAX(dbo.MeterReadings.DeviceTotal - reading.DeviceTotal) AS TotalDiff, 
        MAX(dbo.MeterReadings.TotalCopy - reading.TotalCopy) AS CopyDiff,
        MAX(dbo.MeterReadings.TotalPrint - reading.TotalPrint) AS PrintDiff,
        MAX(dbo.MeterReadings.TotalScan - reading.TotalScan) AS ScanDiff,
        MAX(dbo.MeterReadings.TotalFax - reading.TotalFax) AS FaxDiff,
        MAX(dbo.MeterReadings.TotalMono - reading.TotalMono) AS MonoDiff,
        MAX(dbo.MeterReadings.TotalColour - reading.TotalColour) AS ColourDiff, 
        MIN(reading.ScanDateTime) AS MN, dbo.MeterReadings.DeviceID

FROM        dbo.MeterReadings INNER JOIN (SELECT * FROM dbo.MeterReadings WHERE     
        (dbo.MeterReadings.ScanDateTime > @StartDate) AND 
        (dbo.MeterReadings.ScanDateTime < @EndDate) ) 
        AS reading ON dbo.MeterReadings.DeviceID = reading.DeviceID

WHERE       (dbo.MeterReadings.ScanDateTime > @StartDate) AND (dbo.MeterReadings.ScanDateTime < @EndDate)

GROUP BY    dbo.MeterReadings.DeviceID);
  • 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-05T19:05:04+00:00Added an answer on June 5, 2026 at 7:05 pm

    On the assumption that a value can only ever increase over time, it can certainly be simplified.

    SELECT
      DeviceID,
      MIN(ScanDateTime)                      AS MN,
      MAX(ScanDateTime)                      AS MX,
      MAX(DeviceTotal ) - MIN(DeviceTotal)   AS TotalDiff,
      MAX(TotalCopy   ) - MIN(TotalCopy  )   AS CopyDiff,
      MAX(TotalPrint  ) - MIN(TotalPrint )   AS PrintDiff,
      MAX(TotalScan   ) - MIN(TotalScan  )   AS ScanDiff,
      MAX(TotalFax    ) - MIN(TotalFax   )   AS FaxDiff,
      MAX(TotalMono   ) - MIN(TotalMono  )   AS MonoDiff,
      MAX(TotalColour ) - MIN(TotalColour)   AS ColourDiff
    FROM
      dbo.MeterReadings
    WHERE
          ScanDateTime > @StartDate
      AND ScanDateTime < @EndDate
    GROUP BY
      DeviceID
    

    This assumes that if you have reading on dates 1, 3, 5, 7, 9 and you want to report on 2 -> 8 then you want reading 7 – reading 3. I would have thought you wanted reading 7 – reading 1?

    The above query should be fine for relatively small ranges. If you have Huge ranges of time, the MAX() - MIN() will be operating on large numbers of rows. This can then possibly be improved even further with the following (with correlated sub-queries to lookup just the two rows that you want).

    As a side benefit, this also works even if the values can go down as well as up.

    (I assume the existance of a Device table for a simpler query and faster performance.)

    SELECT
      Device.DeviceID,
      start.ScanDateTime                      AS MN,
      finish.ScanDateTime                     AS MX,
      finish.DeviceTotal - start.DeviceTotal  AS TotalDiff,
      finish.TotalCopy   - start.TotalCopy    AS CopyDiff,
      finish.TotalPrint  - start.TotalPrint   AS PrintDiff,
      finish.TotalScan   - start.TotalScan    AS ScanDiff,
      finish.TotalFax    - start.TotalFax     AS FaxDiff,
      finish.TotalMono   - start.TotalMono    AS MonoDiff,
      finish.TotalColour - start.TotalColour  AS ColourDiff
    FROM
      dbo.Device                 AS device
    INNER JOIN
      dbo.MeterReadings          AS start
        ON  start.DeviceID = device.DeviceID
        AND start.ScanDateTime = (SELECT MIN(ScanDateTime)
                                    FROM dbo.MeterReadings
                                   WHERE DeviceID = device.DeviceID
                                     AND ScanDateTime > @startDate
                                     AND ScanDateTime < @endDate)
    INNER JOIN
      dbo.MeterReadings          AS finish
        ON  finish.DeviceID     = device.DeviceID
        AND finish.ScanDateTime = (SELECT MAX(ScanDateTime)
                                    FROM dbo.MeterReadings
                                   WHERE DeviceID = device.DeviceID
                                     AND ScanDateTime > @startDate
                                     AND ScanDateTime < @endDate)
    

    This can also be modified to pick up the start as being the first date on or before @startDate, if required.

    EDIT: Modification to pick the start reading as being for the first date on or before @startDate.

    SELECT
      Device.DeviceID,
      start.ScanDateTime                                                AS MN,
      finish.ScanDateTime                                               AS MX,
      COALESCE(finish.DeviceTotal, 0) - COALESCE(start.DeviceTotal, 0)  AS TotalDiff,
      COALESCE(finish.TotalCopy  , 0) - COALESCE(start.TotalCopy  , 0)  AS CopyDiff,
      COALESCE(finish.TotalPrint , 0) - COALESCE(start.TotalPrint , 0)  AS PrintDiff,
      COALESCE(finish.TotalScan  , 0) - COALESCE(start.TotalScan  , 0)  AS ScanDiff,
      COALESCE(finish.TotalFax   , 0) - COALESCE(start.TotalFax   , 0)  AS FaxDiff,
      COALESCE(finish.TotalMono  , 0) - COALESCE(start.TotalMono  , 0)  AS MonoDiff,
      COALESCE(finish.TotalColour, 0) - COALESCE(start.TotalColour, 0)  AS ColourDiff
    FROM
      dbo.Device                 AS device
    LEFT JOIN
      dbo.MeterReadings          AS start
        ON  start.DeviceID = device.DeviceID
        AND start.ScanDateTime = (SELECT MAX(ScanDateTime)
                                    FROM dbo.MeterReadings
                                   WHERE DeviceID = device.DeviceID
                                     AND ScanDateTime < @startDate)
    LEFT JOIN
      dbo.MeterReadings          AS finish
        ON  finish.DeviceID     = device.DeviceID
        AND finish.ScanDateTime = (SELECT MAX(ScanDateTime)
                                    FROM dbo.MeterReadings
                                   WHERE DeviceID = device.DeviceID
                                     AND ScanDateTime < @endDate)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
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 have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
i got an object with contents of html markup in it, for example: string
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post

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.