Please help me optimize this query:
$result = mysql_query('
SELECT DISTINCT caseNumber
FROM master_break
WHERE invoiceNumber='$invoice'
AND bay='$bay' AND caseNumber
NOT IN( SELECT DISTINCT caseNumber
FROM shipped_data
WHERE status='' AND bay='$bay')
ORDER BY caseNumber ASC");
Thanks.
DISTINCTmeans that for each record added to the result set, MySQL has to check the entire set for its existence before adding the record. This means that it has a O(n^2) to insert. When using it in anINclause, this can mean that you’re adding substantially more work for zero benefit. Try this instead:Obviously index caseNumber on shipped_data, and bay on both.