您好,登錄后才能下訂單哦!
利用python怎么對oracle數(shù)據(jù)庫的性能進(jìn)行測試?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
一、概述
dba在工作中避不開的兩個問題,sql使用綁定變量到底會有多少的性能提升?數(shù)據(jù)庫的審計功能如果打開對數(shù)據(jù)庫的性能會產(chǎn)生多大的影響?最近恰好都碰到了,索性做個實驗。
實驗采用的辦法很簡單,就是通過python讀取csv文件,然后將其導(dǎo)入到數(shù)據(jù)庫中,最后統(tǒng)計程序執(zhí)行完成所需要的時間
二、準(zhǔn)備腳本
python腳本dataimporttest.py
# author: yangbao # function: 通過導(dǎo)入csv,測試數(shù)據(jù)庫性能 import cx_Oracle import time # 數(shù)據(jù)庫連接串 DATABASE_URL = 'user/password@ip:1521/servicename' class CsvDataImport: def __init__(self, use_bind): self.csv_name = 'test.csv' self.use_bind = use_bind if use_bind == 1: self.insert_sql = "insert into testtb values(:0, " \ "to_date(:1,'yyyy-mm-dd hh34:mi:ss'), " \ "to_date(:2,'yyyy-mm-dd hh34:mi:ss'), " \ ":3, :4, :5, :6, :7, :8, :9, :10, :11, :12, :13, :14, " \ ":15, :16, :17, :18, :19, :20, :21)" # 使用綁定變量的sql else: self.insert_sql = "insert into testtb values({0}, " \ "to_date('{1}','yyyy-mm-dd hh34:mi:ss'), " \ "to_date('{2}','yyyy-mm-dd hh34:mi:ss'), " \ "{3}, {4}, '{5}', {6}, '{7}', {8}, {9}, {10}, {11}, {12}, {13}, {14}, " \ "{15}, {16}, {17}, {18}, {19}, {20}, {21})" # 不使用綁定變量的sql def data_import(self): begin_time = time.perf_counter() try: conn = cx_Oracle.connect(DATABASE_URL) curs = conn.cursor() with open(self.csv_name) as f: csv_contents = f.readlines() import_rows = 0 message = '{} start to import'.format(self.csv_name) print(message) for line, csv_content in enumerate(csv_contents[1:]): data = csv_content.split(',') if self.use_bind == 1: data = map(lambda x: None if x == '' else x, data) else: data = map(lambda x: 'null' if x == '' else x, data) data = list(data) data[-1] = data[-1].replace('\n', '') if self.use_bind == 1: curs.execute(self.insert_sql, data) # 使用綁定變量的方式插入數(shù)據(jù) else: # print(self.insert_sql.format(*data)) curs.execute(self.insert_sql.format(*data)) # 使用非綁定變量的方式插入數(shù)據(jù) import_rows += 1 if import_rows % 10000 == 0: curs.execute('commit') message = '{} has imported {} lines'.format(self.csv_name, import_rows) print(message) conn.commit() curs.close() conn.close() end_time = time.perf_counter() elapsed = round(end_time - begin_time, 2) message = '{}, import rows: {}, use_bind: {}, elapsed: {}'.format( self.csv_name, import_rows, self.use_bind, elapsed) print(message) except Exception as e: message = '{} import failed, reason: {}'.format(self.csv_name, str(e)) print(message) if __name__ == '__main__': CsvDataImport(use_bind=1).data_import()
csv文件
test.csv(內(nèi)容略)
三、測試sql使用綁定變量對性能的影響
a. 使用綁定變量
對庫進(jìn)行重啟,目的是清空數(shù)據(jù)庫內(nèi)的所有緩存,避免對實驗結(jié)果產(chǎn)生干擾
SQL> startup force; SQL> drop table yang.testtb purge; SQL> create table yang.testtb as select * from yang.test where 1=0;
運行腳本python dataimporttest.py
結(jié)果:test.csv, import rows: 227795, use_bind: 1, elapsed: 260.31
b. 不使用綁定變量
對庫進(jìn)行重啟
SQL> startup force; SQL> drop table yang.testtb purge; SQL> create table yang.testtb as select * from yang.test where 1=0;
將腳本的最后一行CsvDataImport(use_bind=1).data_import()改為CsvDataImport(use_bind=0).data_import()
運行腳本python dataimporttest.py
結(jié)果:test.csv, import rows: 227795, use_bind: 0, elapsed: 662.82
可以看到同樣的條件下,程序運行的時間,不使用綁定變量是使用綁定變量的2.54倍
四、測試數(shù)據(jù)庫開啟審計功能對性能的影響
查看數(shù)據(jù)庫審計功能是否開啟
SQL> show parameter audit NAME TYPE VALUE -------------- ----------- ---------- audit_trail string NONE
統(tǒng)計sys.aud$這張表的行數(shù)
SQL> select count(*) from sys.aud$; COUNT(*) ---------- 0
所以可以直接拿第三步中的(a. 使用綁定變量)的結(jié)果作為沒開通審計功能程序運行的時間
對庫開通審計功能,并進(jìn)行重啟
SQL> alter system set audit_trail=db,extended scope=spfile; # 如果設(shè)置成db,那么在sys.aud$里面sqltext將為空,也就是說看不到用戶執(zhí)行的sql語句,審計毫無意義 SQL> startup force; SQL> drop table yang.testtb purge; SQL> create table yang.testtb as select * from yang.test where 1=0; SQL> audit insert table by yang; # 開通對用戶yang的insert操作審計
將腳本的最后一行CsvDataImport(use_bind=0).data_import()改為CsvDataImport(use_bind=1).data_import()
運行腳本python dataimporttest.py
結(jié)果:test.csv, import rows: 227795, use_bind: 1, elapsed: 604.23
與前面使用綁定變量但沒有開通數(shù)據(jù)庫審計功能,程序運行的時間,開通數(shù)據(jù)庫審計功能是不開通數(shù)據(jù)庫審計功能的2.32倍
再來看看sys.aud$這張表的大小
SQL> select count(*) from sys.aud$; COUNT(*) ---------- 227798
因sys.aud$這張表中的sqltext與sqlbind都是clob字段,因此需要通過下面的sql去統(tǒng)計該表所占用的空間
SQL> select sum(bytes) from dba_extents where segment_name in ( select distinct name from (select table_name, segment_name from dba_lobs where table_name='AUD$') unpivot(name for i in(table_name, segment_name))); SUM(BYTES) ---------- 369229824
查看testtb這張表占用的空間
SQL> select sum(bytes) from dba_extents where segment_name in ('TESTTB'); SUM(BYTES) ---------- 37748736
可以看到對一個22萬行的csv數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫,審計的表占用的空間就達(dá)到了驚人的360M,而testtb這張表本身也才37M而已
通過上面的實驗可以得出,對于數(shù)據(jù)庫的審計功能,開通后會嚴(yán)重拖慢數(shù)據(jù)庫的性能以及消耗system表空間!
五、總結(jié)
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。