I have a table "picture" with 255 records for picture colors. I have a table "colors_mixed" with 23897 records. I need to find a closest mixed color to all picture colors, so I wrote a query:
SELECT m.id
FROM picture AS p
INNER JOIN colors_mixed AS m ON
(ABS(m.red - p.r) + ABS(m.green - p.g) + ABS(m.blue - p.b))
= (select min(abs(red - p.r)
+ abs(green - p.g)
+ abs(blue - p.b)) from colors_mixed)
WHERE p.id BETWEEN 1 AND 10
It runs less than 1 second. If I use
WHERE p.id BETWEEN 1 AND 150
it runs about 4 seconds. But if I use
WHERE p.id BETWEEN 1 AND 200
or do not use "WHERE" at all, it runs for a minute, 2 minutes, and I just do not want to wait more because it must be done in 5 seconds. Same thing happens if I use reverse order -
WHERE p.id BETWEEN 200 and 255
works ok, and "BETWEEN 50 and 255" just does not stop.
Advice please, how can I fix it?