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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:41:41+00:00 2026-06-04T20:41:41+00:00

Overall challenge: We are adding items to a table several times a day for

  • 0

Overall challenge:

We are adding items to a table several times a day for a number of “markets”.

So

  • at 12:00 we add 2000 items for market “x”
  • at 12:30 we add 3000 items for market “y”
  • at 14:00 we add 2500 items for market “x” again

This is done several times each day.

At any given time we need to extract the latest items for each market for each day

The desired result for the above insertions is

2500 items for market “x”

3000 items for market “y”

Each addition of a batch of data has an ExecutionTime timestamp that defines the batch uniquely. So the 2000 items for market “x” at 12:00 will have the same ExecutionTime value and the 2500 items for market “x” at 14:00 will have another ExecutionTime value.

We have created a view doing this for us as

SELECT     
    *
FROM         
    dbo.Items AS s
WHERE     
    (ExecutionTime =
       (SELECT     MAX(ExecutionTime) AS Expr1
        FROM          dbo.Items AS s2
        WHERE      (SiteAlias = s.SiteAlias) AND (Market = s.Market) 
          AND (LocalTimestamp >= 
            DATEADD(dd, DATEDIFF(dd, 0, s.LocalTimestamp), 0)) 
          AND 
              (LocalTimestamp < 
            DATEADD(dd, DATEDIFF(dd, 0, s.LocalTimestamp), 1))))

We query the view like this:

SELECT *
FROM [ExportedData]
WHERE 
  SiteAlias = 'MyAlias'
  AND LocalTimeStamp between '2012-05-14 00:00' AND '2012-05-18 00:00'
ORDER BY [Timestamp]

We have defined indexes on the table ITems on the fields Execution time and a combined index on sitealias, marked and localtimestamp.

Problem: the performance sucks. It takes several minutes to query about 150000 rows.

Are there any obvious improvements to the view we should do? I am ready to supply queryplans etc – in case there is no simple screwup we did in creating the view.

An interesting thing is that if we query the view with “LIKE” on the SiteAlias instead of “=”, it speeds up the execution with about 90% – which I did not expect.

Thanks,

:o)

/Jesper
Copenhagen

  • 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-04T20:41:42+00:00Added an answer on June 4, 2026 at 8:41 pm

    Your T-SQL and table structure look fine to me at first glance – so this is just a wild shot into the dark 🙂

    What I would probably try in your position would be to use a CTE (common table expression) and casting LocalTimestamp to datatype DATE since you’re on SQL Server 2008.

    With those in place, you can have your view be something like:

    CREATE VIEW dbo.YourView
    AS
       WITH DataPerDay AS
       (
          SELECT 
             *,
             RowNum = ROW_NUMBER() OVER (PARTITION BY CAST(LocalTimestamp AS DATE) 
                                         ORDER BY ExecutionTime DESC)
          FROM         
             dbo.Items AS s
       )
       SELECT *
       FROM DataPerDay 
       WHERE RowNum = 1
    

    Basically, the CTE “partitions” your data by the date-only part of LocalTimestamp and then assigns sequential numbers to all entries on that day, starting at 1 – so the “newest” or “most recent” entry per day gets RowNum = 1 which is what I use in the select from that CTE.

    This gets around the SELECT(MAX) .... subquery and seems to be a tad faster in my personal observation – but that’s heavily dependant on your tables and data – so just try that for yourself and see if it helps!

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

Sidebar

Related Questions

My overall goal is to add fake/unbound items to a listview control (for final
Overall goal: User enters some number in minutes and I change those numbers from
I have a HTML table where I must set the overall width and want
Is there a way to hide table rows without affecting the overall table width?
I'm trying to extract the overall comments number from a web page using Jsoup.
My intention was to write several functions aimed at finding the overall similarity between
I have an overall application that is comprised of several separate apps. I would
The overall goal I'm trying to accomplish is to implement a customizable avatar system
My overall goal is isolate tags that contain a certain word in the text
Are there overall rules/guidelines for what makes a method thread-safe? I understand that there

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.