1. 程式人生 > >DB2批量插入性能對比

DB2批量插入性能對比

hex rst span 性能對比 head {0} name first except

import ibm_db
import random
import time
first_names = 趙錢孫李周吳鄭王馮陳褚衛蔣沈韓楊朱秦尤許何呂施張孔曹嚴華金魏               陶姜戚謝鄒喻柏水竇章雲蘇潘葛奚範彭郎魯韋昌馬苗鳳花方俞任袁柳酆鮑               史唐費廉岑薛雷賀倪湯滕殷羅畢安常樂於時傅皮卞齊康伍余元顧孟黃穆蕭尹               姚邵湛汪祁毛狄米貝明臧計伏成戴談宋龐熊紀舒屈項祝董梁杜阮藍閔季賈路婁               危江童顏郭梅林刁鐘徐邱駱高夏蔡田樊胡淩霍虞萬柯管盧莫房繆幹解丁宣鄧郁單               
洪包諸左石崔龔程嵇邢裴陸翁羊惠甄曲封儲靳段巫烏焦山谷車侯宓全郗班秋仲伊宮 寧仇欒甘厲祖武符劉景詹龍葉幸司韶郜黎薊薄印宿白蒲邰賴卓藺屠蒙池喬胥聞黨翟 譚勞姬申冉雍桑桂濮牛邊農溫莊晏柴瞿閻連習艾向古廖 def GBK2312(): head = random.randint(0xb0, 0xf7) body = random.randint(0xa1, 0xf9) # 在head區號為55的那一塊最後5個漢字是亂碼,為了方便縮減下範圍 val = f{head:x}{body:x} str = bytes.fromhex(val).decode(
gb2312) return str # 獲取隨機姓名 def get_random_name(): first_name = random.choice(first_names) if random.randrange(2) == 0: return first_name + GBK2312() else: return first_name + GBK2312() + GBK2312() # 方案一:拼接SQL批量插入 def first(): conn = ibm_db.connect("database=POBC;
" "hostname=localhost; " "port=50000; " "protocol=tcpip; " "uid=administrator; " "pwd=wyz","","") start_time = time.time() sql_array = [] sql_array.append("INSERT INTO info VALUES(1,‘張三‘,‘男‘)") sexes = ["", ""] for i in range(80000): name = get_random_name() sex = random.choice(sexes) str = ",({0},‘{1}‘,‘{2}‘)".format(i,name,sex) sql_array.append(str) sql = "".join(sql_array) print("拼接完成。。") # print(sql) stmt = ibm_db.exec_immediate(conn,sql) print(ibm_db.num_rows(stmt)) print(time.time() - start_time) ‘‘‘ 拼接完成。。 80001 32.71020555496216 ‘‘‘ # 方案二,放入事務,批量提交 def second(): array = {ibm_db.SQL_ATTR_AUTOCOMMIT: ibm_db.SQL_AUTOCOMMIT_OFF} conn = ibm_db.pconnect("database=POBC; " "hostname=localhost; " "port=50000; " "protocol=tcpip; " "uid=administrator; " "pwd=wyz","","",array) sexes = ["", ""] start_time = time.time() try: for i in range(80000): name = get_random_name() sex = random.choice(sexes) sql = "INSERT INTO info VALUES({0},‘{1}‘,‘{2}‘)".format(i, name, sex) ibm_db.exec_immediate(conn, sql) except: print("Transaction couldn‘t be completed.") ibm_db.rollback(conn) else: ibm_db.commit(conn) print("Transaction complete.") print("COST:", time.time() - start_time) ‘‘‘ Transaction complete. COST: 232.1929647922516 ‘‘‘ # 方案三,常規做法 def third(): conn = ibm_db.pconnect("database=POBC; " "hostname=localhost; " "port=50000; " "protocol=tcpip; " "uid=administrator; " "pwd=wyz", "", "") sexes = ["", ""] start_time = time.time() for i in range(80000): name = get_random_name() sex = random.choice(sexes) sql = "INSERT INTO info VALUES({0},‘{1}‘,‘{2}‘)".format(i, name, sex) ibm_db.exec_immediate(conn, sql) print("Transaction complete.") print("COST:", time.time() - start_time) ‘‘‘ Transaction complete. COST: 159.8599555492401 ‘‘‘ if __name__ == __main__: # first() # second() third()

DB2批量插入性能對比