Software Development

How can I perform a bulk UPDATE for multiple rows with different values in a single MySQL query?

CH Asked by Christopher Vance · 14-10-2025
0 upvotes 15,346 views 0 comments
The question

I’m trying to optimize my application's database performance. Currently, I’m running individual UPDATE queries inside a loop to update different price values for various product IDs. Is there a more efficient way to execute this as a single SQL statement to reduce the number of database round-trips? I need each specific ID to receive its own unique value without affecting other rows in the table.

3 answers

0
KI
Answered on 16-10-2025

The most efficient standard way to do this in MySQL is by using the UPDATE ... SET ... CASE syntax. Instead of multiple queries, you construct a single query that defines logic for each ID. For example: UPDATE products SET price = CASE WHEN id = 1 THEN 10 WHEN id = 2 THEN 20 ELSE price END WHERE id IN (1, 2). This approach is significantly faster for batch sizes of 100-500 rows because it reduces the overhead of transaction logging and network latency. By including the WHERE clause with the specific IDs, you ensure that MySQL doesn't perform a full table scan, keeping the operation highly performant even on larger datasets common in enterprise software.

0
BR
Answered on 17-10-2025

Have you considered using INSERT INTO ... ON DUPLICATE KEY UPDATE as an alternative for bulk operations, especially if you have a large volume of data coming from a temporary table?

ST 18-10-2025

Brandon makes an excellent point for high-concurrency environments. The ON DUPLICATE KEY UPDATE trick is often faster than a long CASE statement when you have thousands of rows to update, as it leverages the primary key more effectively. You essentially "insert" the new values, and if the ID already exists, MySQL just updates the row with the new data. In my experience with large-scale Software Development projects, this method scales much better and results in fewer deadlocks compared to complex conditional logic within a standard update statement.

0
ME
Answered on 20-10-2025

You can also use a JOIN with a temporary values list if your MySQL version supports it. This keeps the query cleaner than a giant CASE block.

CH 21-10-2025

I agree with Melissa; joining against a derived table or a JSON object (if you're on MySQL 8.0+) is much more readable and maintainable when your update logic starts getting complicated!

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session