Two nearly equal querys, but very different performance
Hallo,
I've got two nearly equal querys with the same amount of source and result data. But one of the querys need about 40-times longer than the other.
I figured out that this is because MySQL uses the Join-Type "index" instead of "ref" for this query statement, but I don't understand why.
Here some informations:
=> The slow one:
SELECT mhd.*
FROM measurement_has_dot mhd
LEFT JOIN pixel_has_dot phd ON mhd.iddot = phd.iddot
WHERE phd.idpixel IS NULL AND mhd.idmeasurement = 35
=> The MySQL explanation:
1, 'SIMPLE', 'mhp', 'ref', 'PRIMARY', 'PRIMARY', '4', 'const', 3415, 'Using index'
1, 'SIMPLE', 'phd', 'ref', 'PRIMARY', 'PRIMARY', '4', 'thedb.mhp.idpixel', 1, 'Using where; Using index; Not exists'
=> The fast one:
EXPLAIN SELECT mhp.*
FROM measurement_has_pixel mhp
LEFT JOIN pixel_has_dot phd ON mhp.idpixel = phd.idpixel
WHERE phd.iddot IS NULL AND mhp.idmeasurement = 35
=> The MySQL explanation:
1, 'SIMPLE', 'mhd', 'ref', 'PRIMARY', 'PRIMARY', '4', 'const', 2970, 'Using index'
1, 'SIMPLE', 'phd', 'index', '', 'PRIMARY', '8', '', 51119, 'Using where; Using index; Not exists'
=> The Tables:
TABLE `measurement_has_dot` (
`idmeasurement` int(10) unsigned NOT NULL,
`iddot` int(10) unsigned NOT NULL,
PRIMARY KEY (`idmeasurement`,`iddot`)
TABLE `measurement_has_pixel` (
`idmeasurement` int(10) unsigned NOT NULL,
`idpixel` int(10) unsigned NOT NULL,
PRIMARY KEY (`idmeasurement`,`idpixel`)
TABLE `pixel_has_dot` (
`idpixel` int(10) unsigned NOT NULL,
`iddot` int(10) unsigned NOT NULL,
`idmeasurement` int(10) unsigned NOT NULL,
PRIMARY KEY (`idpixel`,`iddot`)
The Table Engine is always MyISAM.
I'm grateful for any suggestions.