I have table 1 and table 2.
Table 1 PARTNUM - ID_BRAND partnum - primary key id_brand is "indexed"
Table 2 ID_BRAND - BRAND_NAME id_brand - primary key brand_name is "indexed"
Table 1 contains 1 million records, and Table 2 contains 1,000 records.
I am trying to optimize some query using EXPLAIN, and after many attempts I reached a dead end.
EXPLAIN
SELECT pm.partnum, pb.brand_name
FROM products_main AS pm
LEFT JOIN products_brands AS pb ON pm.id_brand=pb.id_brand
ORDER BY pb.brand ASC
LIMIT 0, 10
The query returns this execution plan:
ID, SELECT_TYPE, TABLE, TYPE, POSSIBLE_KEYS, KEY, KEY_LEN , REF, ROWS, EXTRA
1, SIMPLE, pm, range, PRIMARY, PRIMARY, 1, , 1000000, Using where; Using temporary; Using filesort
1, SIMPLE, pb, ref, PRIMARY, PRIMARY, 4, demo.pm.id_pbrand, 1,
The MySQL query optimizer shows a temporary file + filesort in terms of execution. How can i avoid this?
"EVIL" is located in ORDER BY pb.brand ASC. Ordering for this external field is apparently a bottleneck.