I have a drop-down list which is generated based on the following sql query:
SELECT * FROM product WHERE
product.id NOT IN (SELECT customer_1.product_id FROM customer_1 WHERE (customer_1.product_id != '$x'))
AND product.id NOT IN (SELECT customer_2.product_id FROM customer_2 WHERE (customer_2.product_id != '$x'))
AND product.id NOT IN (SELECT customer_3.product_id FROM customer_3 WHERE (customer_3.product_id != '$x'));
The problem that arises here is the execution time. This query on its own takes about 5.3 s. I have a couple of other similar queries on the same page.
My question is: Is there a better and faster way of achieving the same result?
Thank you in advance.
You may get better performance from
LEFT JOINs, looking for NULLs on the right side of the join (thecustomer_*tables). If I understand your goal, this ought to do the job: