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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:03:51+00:00 2026-06-05T15:03:51+00:00

Using SQL Server 2008. I have multiple Locations which each contain multiple Departments which

  • 0

Using SQL Server 2008.

I have multiple Locations which each contain multiple Departments which each contain multiple Items which can have zero to many Scans. Each Scan relates to a specific Operation which may or may not have a cutoff time. Each Item also belongs to a specific Package which belongs to a specific Job. Each job contains one or more Packages which contains one or more items.

+=============+                         +=============+
|  Locations  |                         |     Jobs    |
+=============+                         +=============+
      ^                                       ^
      |                                       |
+=============+     +=============+     +=============+
| Departments | <-- |    Items    | --> |   Packages  |
+=============+     +=============+     +=============+
                          ^
                          |
                    +=============+     +=============+
                    |    Scans    | --> | Operations  |
                    +=============+     +=============+

What I am attempting to do is get a count of Scans for a Job grouped by Location and Scan date. The tricky part is that I only want to count the first Scan by date/time per Item where the cutoff time for the Operation is not null. (NOTE: the scans definitely will NOT be in date/time order in the table.)

The query I have is getting me the correct results but it is painfully slow when the number of Items for a Job reaches 75,000 or so. I am pushing for a new server — I know our hardware is lacking — but I am wondering if there is something I am doing in the query that is bogging it down as well.

From what little I can glean from the execution plan, most of the cost of the query seems to be in the sub-query to find the first Scan for each Item. It does an index scan (0%) on an Operations table index (ID, Cutoff) and then a lazy spool (19%). It does an index seek (61%) on a Scans table index (ItemID, DateTime, OperationID, ID). The subsequent nested loops (inner join) is only 2% and the Top operator is 0%. (Not that I really understand much of what I just typed but I am trying to provide as much info as possible…)

Here is the query:

SELECT
    Departments.LocationID
    , DATEADD(dd, 0, DATEDIFF(dd, 0, Scans.DateTime))
    , COUNT(Scans.ItemID) AS [COUNT]
FROM
    Items           
    INNER JOIN Scans
        ON Scans.ID = 
    (
        SELECT TOP 1
            Scans.ID 
        FROM
            Scans
        INNER JOIN Operations
            ON Scans.OperationID = Operations.ID
        WHERE
            Operations.Cutoff IS NOT NULL
            AND Scans.ItemID = Items.ID             
        ORDER BY
            Scans.DateTime
    )
    INNER JOIN Operations
        ON Scans.OperationID = Operations.ID
    INNER JOIN Packages
        ON Items.PackageID = Packages.ID
    INNER JOIN Departments
        ON Items.DepartmentID = Departments.ID      
WHERE
    Packages.JobID = @ID        
GROUP BY
    Departments.LocationID 
    , DATEADD(dd, 0, DATEDIFF(dd, 0, Scans.DateTime));

Which will return a sampling of results like so:

8   2012-06-08 00:00:00.000 11842
21  2012-06-07 00:00:00.000 502
11  2012-06-12 00:00:00.000 1841
15  2012-06-11 00:00:00.000 4314
16  2012-06-09 00:00:00.000 278
23  2012-06-12 00:00:00.000 1345
6   2012-06-06 00:00:00.000 2005
20  2012-06-08 00:00:00.000 352
14  2012-06-07 00:00:00.000 2408
8   2012-06-11 00:00:00.000 290
19  2012-06-10 00:00:00.000 85
20  2012-06-11 00:00:00.000 5484
7   2012-06-10 00:00:00.000 2389
16  2012-06-06 00:00:00.000 6762
18  2012-06-09 00:00:00.000 4473
14  2012-06-10 00:00:00.000 2364
1   2012-06-11 00:00:00.000 1531
22  2012-06-08 00:00:00.000 14534
5   2012-06-10 00:00:00.000 11908
9   2012-06-12 00:00:00.000 47
19  2012-06-07 00:00:00.000 559
7   2012-06-07 00:00:00.000 2576

Here’s the execution plan (not sure what I changed since the original post but the cost % are slightly different. The bottleneck still seems to be in the same area though):
Execution Plan

  • 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-05T15:03:54+00:00Added an answer on June 5, 2026 at 3:03 pm

    I am a little leery about marking this as the answer as I am sure we can still squeeze a little juice out of the query. But this did knock my test run from 22 seconds down to 6 seconds (with an added index on Scans: OperationID including DateTime and ItemID):

    WITH CTE AS 
    (
        SELECT
            Items.ItemID AS ID          
            , Scans.DateTime AS [DateTime]
            , Operations.Cutoff AS Cutoff           
            , ROW_NUMBER() OVER (PARTITION BY Items.ID ORDER BY Scans.DateTime) AS RN
            FROM
                Items
                INNER JOIN Scans            
                    ON Items.ID = Scans.ItemID
                INNER JOIN Operations
                    ON Scans.OperationID = Operations.ID
                INNER JOIN Packages
                    ON Items.PackageID = Packages.ID
            WHERE
                Operations.Cutoff IS NOT NULL
                AND Packages.JobID = @ID                        
    )
    SELECT
        Departments.LocationID
        , CTE.DateTime
        , COUNT(Items.ID) AS COUNT
    FROM
        Items           
        INNER JOIN CTE
            ON Items.ID = CTE.ID
            AND CTE.RN = 1
        INNER JOIN Packages
            ON Items.PackageID = Packages.ID
        INNER JOIN Departments
            ON Items.DepartmentID = Departments.ID      
    WHERE
        Packages.JobID = @ID
    GROUP BY
        Departments.LocationID 
        , CTE.DateTime
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Geoserver and SQL Server 2008. I have a table which has a
I am using SQL Server 2008 and have a hashtable which contains data about
Using Microsoft SQL server 2008 I have a query that is in need of
I'm using SQL Server 2008 and I have a table with three columns: Length
I am using SQL Server 2008 and I have a situation where I have
I'm using SQL Server 2008 and I have a table that has a 'Status'
I am using T/SQL in SQL Server 2008 I have a table MyTable with
i'm using sql server 2008 and visual studio 2010 i have a sqldatasource and
I am using SQL Server 2008. I have 2 table variables like FirstName ==========
I am using SQL Server 2008 Enterprise. I have created a very simple test

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.