I have a select statement that takes a while (a very long time) for MySQL to complete. All tables are "MyISAM". MySQL version is 5.6.41.
I have three tables
- table 1: 50,000 rows
- table 2: 7,500,000 rows (7.5GB)
- table 3: 250,000,000 rows (17.5GB)
I perform a select statement with the left joins on the table.
select `table1`.*
from `table1`
inner join `table2` on `table2`.`table1_id` = `table1`.`id`
inner join `table3` on `table3`.`table2_id` = `table2`.`id`
where `table1`.`id` = "2" and `table3`.`parameter` = 'param'
"id" columns are primary keys and "parameter" column has index applied on it
Current table code
CREATE TABLE `table1` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`),
INDEX `id` (`id`),
)
COLLATE='utf8_unicode_ci'
ENGINE=MyISAM;
CREATE TABLE `table2` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`table1_id` INT(10) UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
INDEX `id` (`id`),
INDEX `table1_id_index` (`table1_id`),
)
COLLATE='utf8_unicode_ci'
ENGINE=MyISAM;
CREATE TABLE `table3` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`table2_id` INT(10) UNSIGNED NOT NULL,
`parameter` VARCHAR(255) NOT NULL COLLATE 'utf8_unicode_ci',
PRIMARY KEY (`id`),
INDEX `id` (`id`),
INDEX `id` (`parameter`),
INDEX `table2_id_index` (`table1_id`),
)
COLLATE='utf8_unicode_ci'
ENGINE=MyISAM;
The odd thing is that when i re-write the query other way round i have no performance issues:
select `table1`.*
from `table3`
inner join `table2` on `table3`.`table2_id` = `table2`.`id`
inner join `table1` on `table2`.`table1_id` = `table1`.`id`
where `table1`.`id` = "2" and `table3`.`parameter` = 'param'