as shown in this sql-fiddle I have a bridge table events_tags
representing a many-to-many between events
and tags
.
I need the fastest way to find all events not associated to a given tag t
.
The first query in the fiddle shows the incorrect solution where a an event assigned to t
and another tag is included in the result set.
The second query makes a anti-join
(learned this from an answer by ypercubeᵀᴹ) and is correct, but very slow in my real schema, where it would return 18K rows from the bridge table in ~130ms on the hardware on which our application runs.
I need this functionality in an endpoint of a RESTful API, whose total response time must stay <300ms.
Is there a faster approach I can use or am I limited to the fact that the "real" query has to deal with a derived table of 18K rows?
EDIT
I failed to mention that I will then combine this (as a subquery) with another query selecting only events matching some criteria.
I am not selecting 18K events 🙂
P.S.: let me know if any other detail on the problem is required
Thanks
Best Answer
And why a iden PK in events_tags? Drop that.
Just have a composite PK of tag_ID, event_id in that order.
This might be faster but I doubt it
I don't think MYSQL supports except