考慮以下兩個Python代碼示例,它們實現相同但具有顯着和令人驚訝的性能差異。Postgres:使用光標更新時的令人驚訝的性能
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語句是:
CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);
這表包含10萬行和真空分析測試;已經運行。
我在幾次嘗試中得到了以下結果。
第一個代碼示例:
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
這是非常令人驚訝的我,我會覺得是應該是完全相反的,這意味着使用光標的更新應該是根據this回答顯着更快。