Home >Database >Mysql Tutorial >How Can I Efficiently Update MySQL Table Values from Another Table?
The task is to update the tobeupdated table with data from the original table based on matching values stored in a VARCHAR(32) field. The current query, while functional, suffers from performance issues.
To enhance performance, consider the following optimized query:
UPDATE tobeupdated INNER JOIN original ON (tobeupdated.value = original.value) SET tobeupdated.id = original.id
This query leverages the JOIN syntax instead of multiple WHERE conditions, making it easier to read. Additionally, ensure that both tables have indexes created on the value field to facilitate faster lookups during the join operation.
For further optimization, a simplified version of the query using the USING keyword is recommended:
UPDATE tobeupdated INNER JOIN original USING (value) SET tobeupdated.id = original.id
The USING keyword is employed when both tables in a join share an identical named key (such as id in this case), denoting an equi-join optimization.
The above is the detailed content of How Can I Efficiently Update MySQL Table Values from Another Table?. For more information, please follow other related articles on the PHP Chinese website!