使用Python在MySQL中打开多个游标是否是一项昂贵的操作?

时间:2020-05-08 04:36:45

标签: mysql python-3.x stored-procedures mysql-python

我在Mysql中有几张表要加载到Teradata中,我在这里采用基于文件的方法,这意味着我将Mysql表导出到定界符文件中,而我正在尝试将这些文件加载​​到Teradata中。我期望的问题/明确性是,我们正在维护Mysql存储过程以从表中提取数据,我正在python脚本中使用此存储过程来获取表数据。使用存储过程是好的/最优的。因为要获取表列表,保留期,日期库和其他详细信息,所以我正在创建一个游标以从1个表中获取数据,并且再次必须创建另一个游标来调用存储过程。

  1. 在mysql中创建游标是否昂贵?
  2. 最好将表保存在平面文件中,而不是用表来获取表列表,保留期,日期库和其他详细信息。

请分享您的想法。

import sys
import mysql.connector
from mysql.connector import MySQLConnection, Error
import csv

output_file_path='/home/XXXXXXX/'
sys.path.insert(0, '/home/XXXXXXX/')

from mysql_config import *

def stored_proc_call(tbl):


    print('SP call:', tbl)
    conn_sp = mysql.connector.connect(host=dsn,database=database,user=username,passwd=password,allow_local_infile=True)
    conn_sp_cursor = conn_sp.cursor(buffered=True)
    conn_sp_cursor.callproc('mysql_stored_proc', [tbl])

    output_file = output_file_path + tbl + '.txt'
    print('output_file:', output_file)
    with open(output_file, 'w') as filehandle:
         writer = csv.writer(filehandle, delimiter='\x10')
         for result in conn_sp_cursor.stored_results():
             print('Stored proc cursor:{}, value:{}'.format(type(result), result))
             for row in result:
                 writer.writerow(row)
                 #print('cursor row', row)

# Allow loading client-side files using the LOAD DATA LOCAL INFILE statement.
con = mysql.connector.connect(host=dsn,database=database,user=username,passwd=password,allow_local_infile=True)
cursor = con.cursor(buffered=True)

cursor.execute("select * from table")

for row in cursor:
    print('Archive table cursor:{}, value:{}'.format(type(row), row))
    (db,table,col,orgid,*allvalues)=row
    stored_proc_call(table)
    #print('db:{}, table:{}, col:{}, orgid:{}, ret_period:{}, allvalues:{}'.format(db,table,col,orgid,ret_period,allvalues))

#print('db:{}, table:{}, col:{}, orgid:{}, ret_period:{}, allvalues:{}'.format(db,table,col,orgid,ret_period,allvalues))

0 个答案:

没有答案
相关问题