Reading this wiki article, I found out that the SELECT performance is killed if using IN() clauses with indexed columns in a MySQL database. My question is, how can I rewrite my query so that it won't use any IN() clause while still keeping its functionality?
My query is:
SELECT
`Route`.`route_id`, `Route`.`order`, `Route2`.`order`
FROM
`routes` AS `Route`
INNER JOIN
`routes` AS `Route2`
ON `Route`.`route_id` = `Route2`.`route_id`
WHERE
`Route`.`station_line_id` IN ([10 values]) AND
`Route2`.`station_line_id` IN ([10 values]) AND
`Route`.`order` <= `Route2`.`order`
GROUP BY `
`Route`.`station_line_id`, `Route2`.`station_line_id`, (`Route2`.`order` - `Route`.`order`)
and I have indexed all columns (route_id, station_line_id, station_id and line_id), with the id column being the primary key (the table is just read-only once generated, so no worries for indexing everything). The [10 values]
in the IN() clause are comma separated, like: IN(1, 2, ..., 10)
.
Basically, I self join the table routes table and group the results to get the desired records. The other joins are used for retrieving associated data.
Performance-wise, using InnoDB storage engine, I execute a similar query in >30seconds. Using MyISAM, I get >5seconds. But I believe results can be fetched even faster. I have ~4.5 million records in the table.