DROP TABLE IF EXISTS `la1`; CREATE TEMPORARY TABLE la1 ( `la1_id` int(8) unsigned NOT NULL default '0' ); INSERT INTO `la1` (lal_id) SELECT DISTINCT a_id as la1_id FROM ruwiki_p.pagelinks, articles WHERE pl_from in ( SELECT lkr_id FROM linkers ) and pl_namespace=0 and pl_title=a_title and pl_from!=a_id; The result is in 50% ok, and in 50% is: 'ERROR 1213 (40001) at line 359: Deadlock found when trying to get lock; try restarting transaction'