SELECT CategoryID, CategoryName
FROM testcategory
WHERE CategoryID NOT
IN (
SELECT ChildMuzeCategoryID
FROM testsubcategory
)
First table contain 75k Second table contain 45k records
SELECT CategoryID, CategoryName
FROM testcategory
WHERE CategoryID NOT
IN (
SELECT ChildMuzeCategoryID
FROM testsubcategory
)
First table contain 75k Second table contain 45k records
Try to use join:
SELECT CategoryID, CategoryName
FROM testcategory
LEFT JOIN testsubcategory ON testcategory.CategoryID = testsubcategory.ChildMuzeCategoryID
WHERE testsubcategory.ChildMuzeCategoryID IS NULL
This query is perfectly fine, there's no way to rewrite it that would actually cause it to perform any better.
What you have written is the best way to do and there is no better way, but I have a suggestion:
Since when working with large amount of rows, it would be better if you have some flag like testsubcategory
and update it to 1 (boolean)
while updating the testsubcategory
table. And we can easily use this query:
SELECT `CategoryID`, `CategoryName` FROM `testcategory` WHERE NOT `testsubcategory` = 1;