I'm using this query to get all employees of {clients with name starting with lowercase "a"}:
SELECT * FROM employees
WHERE client_id IN (SELECT id FROM clients WHERE name LIKE 'a%')
Column employees.client_id
is an int, with INDEX client_id (index_id)
. The subquery should IMHO return a list of id-s, which is then used in the WHERE clause.
When I EXPLAIN
the query, the primary query uses no indexes (type:ALL
). But when I EXPLAIN
a list taken from the subquery (e.g. SELECT ... WHERE client_id IN (121,184,501)
), the EXPLAIN
switches to type:range
, and this query gets faster by 50%.
How can I make the query use the index for the data returned by subquery - or, is there a more efficient way of retrieving this data? (Retrieving the id-list to application server, joining it and sending a second query is even more expensive here).
Thanks in advance.