Definition of both tables (it’s just an example, I can’t merge tables, no):
CREATE TABLE `MyTable`(
`id` int NOT NULL,
`a` varchar(10) NOT NULL,
`b` varchar(10) NOT NULL,
`state` tinyint NOT NULL,
PRIMARY KEY (`id`)
);
Goal: Update MyTable1 records that don’t share any value with “a” OR “b” from MyTable2.
My solution:
update MyTable1 as t1
inner join MyTable2 as t2 on (t1.a != t2.a and t1.b != t2.b)
set t1.state=3;
I’m basically joining tables where no columns match, so that I can update the state of such records.
My problem: This is slow. It took 6 seconds with 5000 entries in MyTable1 and 3000 entries in MyTable2.
Question: Can it be any faster (if your solution goes a lot faster, I’ll take it too ;)?
EDIT: My “solution” actually doesn’t work at all.
Your join might find a ton of matches per row. That can make a join really expensive. Try a
not existsinstead:or even a double subquery: