I am running the following SQL query:
UPDATE A
join B
on A.b_id = B.id
SET B.col1 = 'val1'
, B.col2 = ''
, B.col3 = 1
where a.col4 = 'val4'
and b.col5 = 1234
and b.col6 = 1
and b.col7 = 'val7';
In table B, id is PRIMARY KEY of B, in table A. b_id is just a column in A supposed to have same value as id of table B.
My question is, is there any way to optimize the above query with/without (preferable) changing table definition ? Thanks.
You use SUBQUERY to perform JOIN to reduce the number of rows involved to the JOIN.
Since you only update table B's column , so you only put B at UPDATE clause. INNER JOIN was suggested to prevent some unwanted updates.
The enhancement shown as below:-