I have the following tables:
Table ROWS ( **RowId**, Title)
Table CELLS ( **CellId**, RowId, Title)
Table ERRORS ( **ErrorId**, CellId, Title )
So rows can have cells and cells can have errors.
How do I write a query that retrieves the rows where the cells have no errors?
I know that something like this would work but I was wondering if it can be done in a more efficient manner :
QUERY1
SELECT ROWS.RowID FROM ROWS
JOIN CELLS ON ROWS.RowId = CELLS.RowID
JOIN ERRORS ON ERRORS.CellId = CELLS.CellId
QUERY2
SELECT * FROM ROWS
WHERE ROWS.RowId NOT IN ( QUERY1 )**
Any thoughts on how to do this in a more efficient manner ?
Some people have done research on this issue, see: http://explainextended.com/2009/09/18/not-in-vs-not-exists-vs-left-join-is-null-mysql/
The conclusion is: use
LEFT JOIN / IS NULLorNOT IN. Don’t useNOT EXISTS; that is slower.I think I would count the errors per row, and filter on that:
This avoids all sub selects.