考虑以下两个 Python 代码示例,它们实现了相同但具有显着且令人惊讶的性能差异。
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()
和:
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()
表测试的创建语句是:
CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);
该表包含 100000 行和 VACUUM ANALYZE TEST;已运行。
经过几次尝试,我始终得到以下结果。
第一个代码示例:
Finished: SELECT * FROM test for update;: 0.00609304950429
Finished: update starting commit: 37.3272754429
Finished: update : 37.4449708474
第二个代码示例:
Finished: update starting commit: 24.574401185
Finished committing: 24.7331461431
这对我来说非常令人惊讶,因为我认为应该完全相反,这意味着根据这个答案,使用游标的更新应该明显更快。