Is bulk update faster than single update in db2?

后端 未结 5 1273
星月不相逢
星月不相逢 2021-01-25 00:59

I have a Table with 10 columns and in that table I have thousands/millions of rows. In some scenario, I want to update more than 10K records at a time. currently my scenario cod

5条回答
  •  说谎
    说谎 (楼主)
    2021-01-25 01:25

    I came here with same question a week back. Then I faced a situation where I had to update a table with around 3500 rows in mySQL database through JDBC. I updated same table twice: once through a For loop, by iterating through a collection of objects, and once using a bulk update query. Here are my findings:

    • When I updated the data in the database through iteration, it took around 7.945 seconds to execute completely.
    • When I came up with a rather gigantic (where 'gigantic' means 183 pages long) update query and executed the same, it took around 2.24 seconds to complete the update process.

    clearly, bulk update wins by a huge margin.

    Why this Difference?

    To answer this, let's see how a query actually gets executed in DBMS.

    Unlike procedural languages, you instruct the DBMS what to do, but not how to do. The DBMS then does the followings.

    • Syntax checking, or more commonly called 'Parsing'. And parsing comprises of steps like Lexical Analysis, Syntactic Analysis, Semantic Parsing.
    • A series of optimization (Although the definition of 'optimization' itself may vary from product to product. At least that's what I learned while surfing through the internet. I don't have much knowledge about it though.).
    • execution.

    Now, when you update a table in database row by row, each of the queries you execute goes through parsing, optimization and execution. In stead if you write a loop to create a rather long query, and then execute the same, it is parsed only once. And the amount of time you save by using batch update in place of iterative approach increases almost linearly with number of rows you update.

    A few tips that might come handy while updating data in your database

    • It is always a good practice to use indexed columns as reference while writing Any query.
    • Try to use integers or numbers and not strings for sorting or searching data in database. Your server is way more comfortable in comparing two numbers than comparing two strings.
    • Avoid using views and 'in' clause. they make your task easier, but slows down your database. Use joins in stead.

提交回复
热议问题