I'm running these two different queries and I don't understand why on the first one sale table index is not used
EXPLAIN SELECT COUNT(`sale`.`saleId`) AS `nb`
FROM `sale`
WHERE `sale`.`saleTransactionId` IN (
SELECT `transaction`.`transactionId`
FROM `transaction`
WHERE `transaction`.`transactionId` = 87587
)
Result is
result 1 https://snag.gy/SusqF.jpg
Second one
EXPLAIN SELECT COUNT(`sale`.`saleId`) AS `nb`
FROM `sale`
WHERE `sale`.`saleTransactionId` IN (87587)
Result is
result 2 https://snag.gy/ZgOXQ.jpg
Juste to be sure
SELECT `transaction`.`transactionId`
FROM `transaction`
WHERE `transaction`.`transactionId` = 87587
returns juste one row
saleTransactionId is INT(11) as transactionId Both tables uses MyISAM
And sale index structure
sale index structure https://snag.gy/we3HL.jpg
Thanks!
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `transaction` ADD INDEX `transaction_idx_transactionid` (`transactionId`);
SELECT
`transaction`.`transactionId`
FROM
`transaction`
WHERE
`transaction`.`transactionId` = 87587