I've been trying to get some speed improvements on a certain SELECT query. The situation is as follows: There is a (in my eyes) huge crossing table. It currently has about 20 million rows, but I expect this to grow alot. Based on this crossing table I need to create another table. To do so, I need to execute the following query:
SELECT hugeCrossingTable.field3, otherTable.field1, hugeCrossingTable.field2 * otherTable.field3 AS someName
FROM hugeCrossingTable
INNER JOIN otherTable ON hugeCrossingTable.field1 = otherTable.field2
Now this currently results in about a million rows. I already have indexes on both the field1 in the 2 tables, but it still takes 18 minutes to finish.. I thought about splitting the table, but then I'd need to find a way on how to split the data, and since it's just a crossing table nothing comes to mind on how to do this.
Any ideas on how this can be optimized?
Thanks.
On request here's the create statement:
CREATE TABLE `hugeCrossingTable` (
`field` int(11) NOT NULL,
`field1` int(11) NOT NULL,
`field2` double(10,5) DEFAULT NULL,
`field3` int(4) DEFAULT NULL,
KEY `field1` (`field1`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `otherTable` (
`field` int(10) unsigned NOT NULL AUTO_INCREMENT,
`field1` int(10) unsigned NOT NULL,
`field2` int(10) unsigned NOT NULL,
`field3` decimal(5,2) NOT NULL,
PRIMARY KEY (`field`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
And the explain output:
id, select_type, table , type , possible_keys, key , key_len, ref , rows, Extra
1 , 'SIMPLE' , 'otherTable' , 'ALL', '' , '' , '' , '' , 294 , ''
1 , 'SIMPLE' , 'hugeCrossingTable', 'ref', 'field1' , 'field1', '4' , 'otherTable.field2', 69 , 'Using where'