I tested an update between two large (~5 mil records each) which was taking 10 seconds or so per update. So, doing Explain for my very first time tested the select:
SELECT
T1.Z, T2.Z
FROM
TableB T1
INNER JOIN TableL T2
on T1.Name=T2.Name
and T1.C=T2.C
and T1.S=T2.S
and T1.Number>=T2.MinNumber
and T1.Number<=T2.MaxNumber
Explain returned the following as possible keys:
- Name
- C
- S
- Number
and chose C as the key.
I was told that my best bet was to make a compound key, and in the order of the select so I did
Alter Table TableB Add Index Compound (Name,C,S,Number)
And did an explain again, hoping it would choose my compound but now even though it shows the compound index as a possible key it still chooses Index C.
I read that I can force the index I want with:
SELECT
T1.Z, T2.Z
FROM TableB T1 Force Index(Compound)
INNER JOIN TableL T2
on T1.Name=T2.Name
and T1.C=T2.C
and T1.S=T2.S
and T1.Number>=T2.MinNumber
and T1.Number<=T2.MaxNumber
yet I am not sure if it makes any sense to over-ride MySql's selection and, given that if it doesn't help the update is going to take almost two years it doesn't seem like a smart thing to test.
Is there some step I am missin? Do I need to remove the other keys so that it chooses my compound one and if so how will I know if it will even make a difference (given that Mysql saw it and rejected it)?
Explain output on T1: (note: I did not yet add the Compound Index as the table is huge and it might be wasted time until I figure this out. I previously added it on a highly truncated version of the table but that won't help with this explain) Table1
- select_type: simple
- type: ref
- possible_keys:
- Number,C,S,Name
- key: Name
- key_len: 303
- ref: func
- rows: 4
- Extra: using where
Explain for Table2
- select_type: SIMPLE
- type: ALL
- possible_Keys: MinNumber, MaxNumber
- key:
- key_length:
- ref:
- rows: 5,447,100
- Extra:
Cardinality (only showing indexes relevant here as there are a few others):
Primary: 5139680
Name: 1284920
Number: 57749
C: 7002
S: 21