用python對oracle進(jìn)行簡單性能測試
一、概述
dba在工作中避不開的兩個(gè)問題,sql使用綁定變量到底會有多少的性能提升?數(shù)據(jù)庫的審計(jì)功能如果打開對數(shù)據(jù)庫的性能會產(chǎn)生多大的影響?最近恰好都碰到了,索性做個(gè)實(shí)驗(yàn)。
- sql使用綁定變量對性能的影響
- 開通數(shù)據(jù)庫審計(jì)功能對性能的影響
實(shí)驗(yàn)采用的辦法很簡單,就是通過python讀取csv文件,然后將其導(dǎo)入到數(shù)據(jù)庫中,最后統(tǒng)計(jì)程序執(zhí)行完成所需要的時(shí)間
二、準(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 hh24:mi:ss'), " \ "to_date(:2,'yyyy-mm-dd hh24: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 hh24:mi:ss'), " \ "to_date('{2}','yyyy-mm-dd hh24: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)的所有緩存,避免對實(shí)驗(yàn)結(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;
運(yùn)行腳本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()
運(yùn)行腳本python dataimporttest.py
結(jié)果:test.csv, import rows: 227795, use_bind: 0, elapsed: 662.82
可以看到同樣的條件下,程序運(yùn)行的時(shí)間,不使用綁定變量是使用綁定變量的2.54倍
四、測試數(shù)據(jù)庫開啟審計(jì)功能對性能的影響
查看數(shù)據(jù)庫審計(jì)功能是否開啟
SQL> show parameter audit NAME TYPE VALUE -------------- ----------- ---------- audit_trail string NONE
統(tǒng)計(jì)sys.aud$這張表的行數(shù)
SQL> select count(*) from sys.aud$; COUNT(*) ---------- 0
所以可以直接拿第三步中的(a. 使用綁定變量)的結(jié)果作為沒開通審計(jì)功能程序運(yùn)行的時(shí)間
對庫開通審計(jì)功能,并進(jìn)行重啟
SQL> alter system set audit_trail=db,extended scope=spfile; # 如果設(shè)置成db,那么在sys.aud$里面sqltext將為空,也就是說看不到用戶執(zhí)行的sql語句,審計(jì)毫無意義 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操作審計(jì)
將腳本的最后一行CsvDataImport(use_bind=0).data_import()改為CsvDataImport(use_bind=1).data_import()
運(yùn)行腳本python dataimporttest.py
結(jié)果:test.csv, import rows: 227795, use_bind: 1, elapsed: 604.23
與前面使用綁定變量但沒有開通數(shù)據(jù)庫審計(jì)功能,程序運(yùn)行的時(shí)間,開通數(shù)據(jù)庫審計(jì)功能是不開通數(shù)據(jù)庫審計(jì)功能的2.32倍
再來看看sys.aud$這張表的大小
SQL> select count(*) from sys.aud$; COUNT(*) ---------- 227798
因sys.aud$這張表中的sqltext與sqlbind都是clob字段,因此需要通過下面的sql去統(tǒng)計(jì)該表所占用的空間
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
可以看到對一個(gè)22萬行的csv數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫,審計(jì)的表占用的空間就達(dá)到了驚人的360M,而testtb這張表本身也才37M而已
通過上面的實(shí)驗(yàn)可以得出,對于數(shù)據(jù)庫的審計(jì)功能,開通后會嚴(yán)重拖慢數(shù)據(jù)庫的性能以及消耗system表空間!
五、總結(jié)
- 代碼中盡量使用綁定變量
- 最好不要開通數(shù)據(jù)庫的審計(jì),可以通過堡壘機(jī)去實(shí)現(xiàn)對用戶操作審計(jì)(ps:還請大家推薦個(gè)堡壘機(jī)廠商,這個(gè)才是本文最主要的目的_)
實(shí)驗(yàn)存在不嚴(yán)謹(jǐn)?shù)牡胤?,相關(guān)對比數(shù)據(jù)也僅作為參考
以上就是用python對oracle進(jìn)行簡單性能測試的示例的詳細(xì)內(nèi)容,更多關(guān)于python 對Oracle進(jìn)行性能測試的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python 網(wǎng)絡(luò)編程起步(Socket發(fā)送消息)
現(xiàn)在開始學(xué)習(xí)網(wǎng)絡(luò)編程,先從簡單的UDP協(xié)議發(fā)送消息開始。我們需要有接受消息的服務(wù)端程序(Server.py)和發(fā)送消息的客戶端程序(Client)。2008-09-09用python實(shí)現(xiàn)簡單EXCEL數(shù)據(jù)統(tǒng)計(jì)的實(shí)例
下面小編就為大家?guī)硪黄胮ython實(shí)現(xiàn)簡單EXCEL數(shù)據(jù)統(tǒng)計(jì)的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01用python代碼將tiff圖片存儲到j(luò)pg的方法
今天小編就為大家分享一篇用python代碼將tiff圖片存儲到j(luò)pg的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12如何徹底解決python?NameError:name?'__file__'?is?not?
這篇文章主要給大家介紹了關(guān)于如何徹底解決python?NameError:name?'__file__'?is?not?defined的相關(guān)資料,文中通過圖文將解決的辦法介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02Python中import的用法陷阱解決盤點(diǎn)小結(jié)
這篇文章主要為大家介紹了Python中import的用法陷阱解決盤點(diǎn)小結(jié),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10python快速建立超簡單的web服務(wù)器的實(shí)現(xiàn)方法
某些條件測試,需要一個(gè)簡單的web服務(wù)器測試一下,為此專門去配置個(gè)nginx 或者 apache服務(wù)器略顯麻煩,這里就為大家介紹一下使用python快速建立超簡單的web服務(wù)器的方法,需要的朋友可以參考下2018-02-02