Postgres: Surprising performance on updates using cursor

后端 未结 1 425
长发绾君心
长发绾君心 2021-01-15 03:53

Consider the two following Python code examples, which achieves the same but with significant and surprising performance difference.

import psycopg2, time

c         


        
1条回答
  •  自闭症患者
    2021-01-15 04:39

    I don't think that the test is balanced- your first code is fetching the data from the cursor, then updating, whereas the second is blindly updating by ID without fetching the data. I assume the first code sequence translates to a FETCH command followed by UPDATE- so that's two client/server command turnarounds as opposed to one.

    (Also the first code starts by locking each row in the table- this pulls the entire table into the buffer cache- although thinking about it, I doubt this actually impacts performance but you didn't mention it)

    Also tbh I think that for a simple table, there won't be much different between updating by ctid (which I assume is how where current of... works) and updating through a primary key- the pkey update is an extra index lookup, but unless the index is huge it's not much of a degradation.

    For updating 100,000 rows like this, I suspect that most of the time is taken up generating the extra tuples and inserting them into or appending them to the table, rather than locating the previous tuple to mark it as deleted.

    0 讨论(0)
提交回复
热议问题