Question

I have two tables:

Event
ID, StartTime

Video
ID, StartTime, Endtime

I need to find any event that has no video.

For this I have got the following query (well I have tried a fair few)

SELECT * FROM
(Select E.*,V.ID FROM Events E LEFT JOIN Video V On E.StartTime >= V.StartTime AND E.StartTime <= V.EndTime) A
HAVING V.ID IS NULL

This seems to produce the correct data, but it is too slow... (i have 100000 records in Events and 10000 records in Video). This takes approx 2 minutes to do. I have indexes on ALL the columns

Is there a quicker way to do this?

Was it helpful?

Solution

Have you tried removing the subquery?

SELECT E.*
FROM Events E 
LEFT JOIN Video V On E.StartTime >= V.StartTime AND E.StartTime <= V.EndTime
WHERE V.ID IS NULL

Or using NOT EXISTS

SELECT E.*
FROM Events E 
WHERE NOT EXISTS (
  SELECT * FROM Video V WHERE E.StartTime >= V.StartTime AND E.StartTime <= V.EndTime
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top