You do not need to return data from nested subqueries. I'm not sure if this will affect indexing, but itβs easier to read.
And EXISTS / JOIN is probably better than IMHO and then uses IN
SELECT *
FROM
A
JOIN
(SELECT LEFT(B,5) AS b1
FROM A
GROUP BY LEFT(B,5)
HAVING COUNT(DISTINCT C) = 1
) t1 On LEFT(A.B, 5) = t1.b1
JOIN
(SELECT C AS C1
FROM A
GROUP BY C
HAVING COUNT(DISTINCT LEFT(B,5)) = 1
) t2 ON A.C = t2.c1
But you will need a computed column as marc_s said at least
And 2 indexes: one on (computed, C)and another on(C, computed)
source
share