我尝试使用cx_Oracle在python中并行运行完全独立的Oracle查询。在
我可以通过为每个线程设置一个新的数据库连接,然后在每个单独的线程中运行查询,这将使总时间从大约2分钟增加到1分钟20,因此它肯定可以工作。查询计时:START_TIME END_TIME
17-FEB-16 22.33.28.000000000 17-FEB-16 22.33.30.000000000
17-FEB-16 22.33.30.000000000 17-FEB-16 22.33.33.000000000
17-FEB-16 22.33.33.000000000 17-FEB-16 22.33.36.000000000
17-FEB-16 22.33.36.000000000 17-FEB-16 22.33.36.000000000
17-FEB-16 22.33.36.000000000 17-FEB-16 22.34.08.000000000
17-FEB-16 22.34.08.000000000 17-FEB-16 22.34.26.000000000
17-FEB-16 22.34.26.000000000 17-FEB-16 22.34.27.000000000
17-FEB-16 22.34.27.000000000 17-FEB-16 22.34.29.000000000
但是,在每个线程中设置到数据库的连接会有开销,我很确定我应该能够为每个线程创建一个新的游标并共享连接,如下所示:
但是,当我共享连接并使用一个单独的游标时,会发生的情况是,所有查询同时开始,然后在同一时间结束,这样看来,当线程被派生时,数据库上的查询仍在按顺序运行。查询计时:
^{pr2}$
多连接代码:for file in file_transporter.complete_file_list:
#Get database and open connection
the_db = shared_lib_wrapper.get_oracle().Oracle(the_logger)
the_db .connect(conn_str())
#Create new thread
thread = threading.Thread(target=Loader, args=(params, the_date, the_logger, the_db, file, file_transporter.complete_file_list[file]))
the_logger.info("Running Thread: " + thread.getName())
thread.start()
多光标的代码(在runLoad中有一个函数可以创建一个新的游标并执行一个过程-见下文):for file in self.file_list:
file_parametes = self.file_list[file]
function_to_run = file_parametes['LOAD_PACKAGE'] + '.' + file_parametes['LOAD_FUNCTION']
#Create new thread
thread = threading.Thread(target=self.runLoad, args=(file_parametes['RUN_ID'], function_to_run))
self.log.info("Spawned Thread: " + thread.getName())
self.log.info("Running Thread: " + thread.getName())
thread.start()
创建游标的代码:def execute_stored_proc_with_in_and_out_params(self, proc_name, params, dbms_logging=False):
try:
cursor = cx_Oracle.Cursor(self.db_conn
因此,我的问题是:
1)我是否在创建光标时出错?-如果有关于如何修复它的任何想法,我已经读到cx峎u oracle是threadsafety 2:Currently 2, which means that threads may share the module and connections, but not cursors.
2)如果我不能共享连接,那么为每个线程创建一个新的连接是否有问题,即使创建每个连接的开销也似乎仍然可以提高我的性能?在