update 结果集的优化

表记录条数:

SELECT count(1) from db.table1

原sql:

UPDATE db.table
SET attr1 =1
WHERE
	attr2 IN (
		SELECT
			A.attr2 
		FROM
			(
				SELECT
					attr2 
				FROM
					db.table
				WHERE
					 attr3=3195
				AND attr4 ='1234'
				AND attr5 <= '2018-11-15 13:31:30'
				AND (attr1 =0 OR attr1 IS NULL)
				LIMIT 6
			) A
	);

执行时间:26.505s

update 结果集优化后sql:

UPDATE db.table t1
inner join (
	SELECT attr2 FROM db.table WHERE attr3=3195 AND attr4='1234' AND attr5<= '2018-11-15 13:40:31' AND (attr1 =0 or attr1 is NULL)  LIMIT 6
)t2 on t1.attr2 = t2.attr2 
SET t1.attr1 =1 ;

执行时间: 0.028s

你可能感兴趣的:(sql)