Postgres: Amazing cursor update performance

Consider the following two Python code examples that achieve the same, but with a significant and surprising performance difference.

import psycopg2, time

conn = psycopg2.connect("dbname=mydatabase user=postgres")
cur = conn.cursor('cursor_unique_name')  
cur2 = conn.cursor()

startTime = time.clock()
cur.execute("SELECT * FROM test for update;")
print ("Finished: SELECT * FROM test for update;: " + str(time.clock() - startTime));
for i in range (100000):
    cur.fetchone()
    cur2.execute("update test set num = num + 1 where current of cursor_unique_name;")
print ("Finished: update starting commit: " + str(time.clock() - startTime));
conn.commit()
print ("Finished: update : " + str(time.clock() - startTime));

cur2.close()
conn.close()

and

import psycopg2, time

conn = psycopg2.connect("dbname=mydatabase user=postgres")
cur = conn.cursor('cursor_unique_name')  
cur2 = conn.cursor()

startTime = time.clock()
for i in range (100000):
    cur2.execute("update test set num = num + 1 where id = " + str(i) + ";")
print ("Finished: update starting commit: " + str(time.clock() - startTime));
conn.commit()
print ("Finished: update : " + str(time.clock() - startTime));

cur2.close()
conn.close()

The create statement for a table test:

CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);

And this table contains 100,000 rows and VACUUM ANALYZE TEST; was launched.

I got the following results sequentially with several attempts.

First code example:

Finished: SELECT * FROM test for update;: 0.00609304950429
Finished: update starting commit: 37.3272754429
Finished: update : 37.4449708474

Second code example:

Finished: update starting commit: 24.574401185
Finished committing: 24.7331461431

This is very surprising to me, as I think it should be the exact opposite, which means updating using the cursor should be significantly faster according to this answer .

+2
1

, - , , ID . , FETCH, UPDATE, / .

( - , , , , , )

, ctid ( , where current of...) - pkey - , , .

100 000 , , , , , , .

+4

All Articles