I have this query below:
SELECT a.id, b.item_name
FROM table_1 as a
INNER JOIN table_2 as b on a.item_id = b.item_id
There is index on a.bid as primary, and a index on a.item_id and there is an index on b.item_id as primary and index on b.item_name
However when I run the query through the EXPLAIN the primary table becomes table_1 and there is no index uses so its doing a full scan. Why wouldn’t it join the index for b.item_id?
It needs to do a full scan on at least one table, as you are requesting all records. It sounds like it picked table_1. The index should be used on table_2.
If you additionally add a where clause, you can avoid that table scan. But if you actually need all rows, then a scan is the quickest way to get them.