1

Is there a way to improve my query

SELECT *
FROM  StringControllerDaten
WHERE StringControllerDaten.Erstellt NOT IN
(
    SELECT  
            Min(Erstellt) AS Erstellt   
    FROM        StringControllerDaten

    GROUP BY
    StringControllerDaten.StringController,
    DATEPART(YEAR, Erstellt),
    DATEPART(MONTH, Erstellt),
    DATEPART(DAY, Erstellt),
    DATEPART(HOUR, Erstellt),
    (DATEPART(MINUTE, Erstellt) / 15)
)

This query returns all data who doesn't match one of the min Datetimes from the 15Min Interval.

It is running again +1Mio Row's and takes hours.


Edit:

the estimated query plan

estimated query plan

WiiMaxx
  • 5,322
  • 8
  • 51
  • 89

2 Answers2

2
  1. Create a list of dates with 15 min interval between min and max Erstellt put then in a table/cursor/temptable.

See Generate a resultset of incrementing dates in TSQL

Lets say the table looks like:

    CREATE TABLE [dbo].[AllDates]
    ( COLUMN Item DATETIME)
  1. Cross compare your table with list from 1.

    SELECT
       StringControllerDaten.ID -- or what you need
    FROM
       StringControllerDaten
    WHERE
       StringControllerDaten.Erstellt NOT IN (SELECT AllDates.Item FROM AllDates)
    
Community
  • 1
  • 1
Ralf de Kleine
  • 11,464
  • 5
  • 45
  • 87
0

Could the top answer in this thread help you?

EDIT: still need to figure out how get the NOT IN into an Inner Join

Complex join with nested group-by/having clause?

achinda99 wrote: "In most database systems, the JOIN works a lost faster than doing a WHERE ... IN."

Got no means of testing atm, tho.

Community
  • 1
  • 1
Martin Dreher
  • 1,514
  • 2
  • 12
  • 22