Select records without a relationship

mysql

Solution

Sounds like an INDEX issue, your query won't get quicker than it already is by modifying it.

If you don't already have an index on `ref_part_id`, Try this, it should speed up your query quite a bit:

ALTER TABLE tbl_part_product  ADD INDEX (ref_part_id);

Problem

I have three table - tbl_part - Contains an indexed list of parts - tbl_product - Contains an indexed list of products - tbl_part_product - Contains the relationships between parts and products. I'd like to select all the parts that AREN'T related to a product but having some 20k+ parts my query is just too slow. Does anyone know a better way of how I can accomplish this? ``` SELECT * FROM tbl_part AS p LEFT JOIN tbl_part_product AS pp ON pp.ref_part_id = p.part_id WHERE pp.ref_part_id IS NULL ```

Original source