I have a table with 60 attributes in it, attribute1..attribute60. The database engine in MySQL and the table engine is MyISAM. The query is as follows:
SELECT DISTINCT attribute1
FROM `product_applications`
WHERE `product_applications`.`brand_id` NOT IN (642, 630, 513, 637, 632,
556, 548, 628, 651, 660,
648, 557, 650, 624, 652,
636, 546, 662, 634, 629,
657, 638, 658, 659, 661, 625)
I use a NOT IN, because that list is significantly smaller than the IN list.
I have created the following index:
brand_id, attribute1, attribute2, attribute3, attribute4
A DESC reveals that this index is being selected, but it looks like it is still looking at the whole table because I see the whole row count in the “rows” column:
6732948
In the “extra” column I have:
Using where; Using index; Using temporary
This query is taking over 7 seconds. I am looking at all different options here, including breaking the table up.
UPDATE:
I was able to cut the query time in half with the clever use of the UNION ALL noted by my friend below. Also, this is a dynamically generated query, so none of the temporary table options that some of you have offered, while an excellent idea, were available to me.
Previously, the following used a LEFT JOIN — but the OP reversed the logic to use an INNER JOIN:
You could consider populating a temp table, to use in place of the derived one you see in my answer.