Hi I was hoping I could get some help on how to optimize my database so it doesn’t take a year. I know that to speed it up I need to add indexes, but I’m not exactly sure on what I should be adding them on.
Heres the three tables in my database:
CREATE TABLE IF NOT EXISTS `journeyPattern2` (
`journeyPatternId` int(11) NOT NULL AUTO_INCREMENT,
`serviceId` int(11) NOT NULL,
`direction` enum('inbound','outbound') NOT NULL,
PRIMARY KEY (`journeyPatternId`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `journeyPatternTimingLink2` (
`journeyPatternTimingLinkId` int(11) NOT NULL AUTO_INCREMENT,
`journeyPatternId` int(11) NOT NULL,
`from` varchar(15) NOT NULL,
`to` varchar(15) NOT NULL,
`direction` enum('inbound','outbound') NOT NULL,
`runTime` varchar(15) NOT NULL,
PRIMARY KEY (`journeyPatternTimingLinkId`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS `line` (
`lineId` int(11) NOT NULL AUTO_INCREMENT,
`serviceId` int(11) NOT NULL,
`lineName` tinytext NOT NULL,
PRIMARY KEY (`lineId`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS `service` (
`serviceId` int(11) NOT NULL AUTO_INCREMENT,
`serviceCode` varchar(50) NOT NULL,
`registeredOperatorRef` varchar(50) NOT NULL,
`origin` tinytext NOT NULL,
`destination` tinytext NOT NULL,
PRIMARY KEY (`serviceId`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
The table journeyPattern2 contains about 33000 rows, journeyPatternTimingLink2 contains around 13 million, and line and service around 70000.
The query I’m trying to optimize is the following
SELECT DISTINCT lineName,line.serviceId FROM `journeyPatternTimingLink2`
INNER JOIN journeyPattern2
ON journeyPattern2.journeyPatternId=journeyPatternTimingLink2.journeyPatternId
INNER JOIN line
ON journeyPattern2.serviceId = line.serviceId
WHERE `from` = '13006785E'
I’ve never really worked with tables of this size before so I’m not sure if I’m joining correctly in the first place. I’ve also uploaded a screenshot of me running EXPLAIN on the query from phpmyadmin, but I’m not exactly sure how to interpret the results so any help would be appreciated.
Thanks for your help.
You should add some indexes to these fields –
fromThese are indexes used in JOIN-ON clauses and WHERE condition.