I have two tables - a temporary table for working with imported data, and the existing accounts table. I need to update the user id in the temp table based on matching by an account number and group number in the accounts table.
CREATE TABLE `_temp` (
`g_id` int(11) NOT NULL AUTO_INCREMENT,
`g_group_norm` varchar(10) DEFAULT NULL,
`g_uid1` varchar(25) DEFAULT NULL,
`g_spid` int(11) DEFAULT NULL,
`g_pid` int(11) DEFAULT NULL,
PRIMARY KEY (`g_id`),
KEY `groupn` (`g_group_norm`) USING BTREE,
KEY `guid` (`g_uid1`) USING BTREE,
KEY `gspid` (`g_spid`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `accounts` (
`sa_actid` int(11) NOT NULL AUTO_INCREMENT,
`sa_grp` varchar(10) DEFAULT NULL,
`sa_account` varchar(25) DEFAULT NULL,
`sa_spid` int(11) DEFAULT NULL,
`sa_partnerid` int(11) DEFAULT NULL,
PRIMARY KEY (`sa_actid`),
KEY `spid` (`sa_spid`) USING BTREE,
KEY `grp` (`sa_grp`) USING BTREE,
KEY `act` (`sa_account`) USING BTREE,
KEY `partnerid` (`sa_partnerid`) USING BTREE,
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
The accounts table is upwards of 5M rows. The _temp table data I'm working with now can be anywhere from 50,000 to 700,000 rows.
The query I've been using to set that g_spid = sa_spid is this:
UPDATE _temp T, accounts A SET
T.g_spid = A.sa_spid
WHERE T.g_uid1 = A.sa_account
AND T.g_group_norm = A.sa_grp
AND A.sa_partnerid = 118
AND T.g_spid IS NULL;
The accounts table has around 3M rows, of which 2.84M are partner ID "118". The temp table update works slow, but OK on 10-20k rows, but when I have a larger set of data to work with (currently temp table is around 100k rows) it seems to never finish (it's been running for 15 minutes now).
Is there a better way to do this query? I've tried it with an inner join on g_uid1 = sa_account, g_group_norm = sa_grp etc. and that seems even slower.