I'm doing join from two tables of different databases:
SELECT
count(*)
FROM
`db1`.`view1` p
join
`db2`.`table1` e
on e.Migration = p.s_encargo and e.did=149;
I made a index in table1 like this:
alter table table1 add INDEX `DID_Migration` (`DID`, `Migration`);
I added the two fields I used in on clause, in the index ('did' and 'migration')
But when i execute the query, only used the first column of the index (did): This is the explain of the query.
But if I show the explain of the this other query:
select * from table1 where Migration = '100008600' and did=149;
this use all columns of index:
Why in the first query only use the first column of the index, and in the second query uses the two columns?
The key_len column indicates the length of the key that MySQL decided to use. The value of key_len enables you to determine how many parts of a multiple-part key MySQL actually uses.
This mean that for the query you declared mysql choose to use only the first part
in this case you can use force (or use) for impose the index you want
SELECT
count(*)
FROM
`db1`.`view1` p
join
`db2`.`table1` e FORCE INDEX FOR JOIN (`DID_Migration`)
on e.Migration = p.s_encargo and e.did=149;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.