【问题标题】:PYODBC Insert Statement in MS Access DB Extremely slowMS Access DB中的PYODBC插入语句极慢
【发布时间】:2019-03-19 03:45:50
【问题描述】:

我希望加快我在 Access Db 中的插入语句。数据只有 86500 条记录,处理时间超过 24 小时。我希望加速的代码部分是比较两个表的重复项。如果没有找到重复项,则插入该行。我正在运行 64 位 windows 10、32 位 python 2.7、32 位 ms 访问 odbc 驱动程序和 32 位 pyodbc 模块。任何帮助将不胜感激代码示例如下。

def importDIDsACC():
    """Compare the Ledger to ImportDids to find any missing records"""
    imdidsLst = []
    ldgrLst = readMSAccess("ActivityNumber", "Ledger")
    for x in readMSAccess("DISP_NUM", "ImportDids"):
        if x not in ldgrLst and x not in imdidsLst:
            didsLst.append(x)
    #Select the records to import
    if len(imdidsLst) > 0:
        sql = ""
        for row in imdidsLst:
            sql += "DISP_NUM = '" + row[0]
            cursor.execute("SELECT * FROM ImportDids WHERE " + sql)
            rows = cursor.fetchall()
            #Import to Ledger
            dupChk = []
            for row in rows:
                if row[4] not in dupChk:
                    cursor.execute('INSERT into Ledger ([ActivityNumber], [WorkArea], [ClientName], [SurfacePurpose], [OpsApsDist], [AppDate], [LOADate], [EffDate], [AmnDate], [CanDate], [RenDate], [ExpDate], [ReiDate], [AmlDate], [DispType], [TRM], [Section], [Quarter], [Inspected_Date], [Inspection_Reason], [Inspected_By], [InspectionStatus], [REGION], [DOC], [STATCD]) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
                                   str(row[1]), str(row[18]), str(row[17]), row[14], str(row[26]), row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], str(row[1][0:3]), trmCal(str(row[21]),str(row[20]), str(row[19])), str(row[22]), str(row[23]), inspSts(str(row[1]), 0),inspSts(str(row[1]), 1), inspSts(str(row[1]), 2), inspSts(str(row[1]), 3), str(row[27]), str(row[3]), str(row[13]))
                    dupChk.append(row[4])
            cnxn.commit()

def readMSAccess(columns, table):
    """Select all records from the chosen field"""
    sql = "SELECT "+ columns +  " FROM " + table
    cursor.execute(sql)
    rows = cursor.fetchall()
    return rows

def dbConn():
    """Connects to Access dataBase"""
    connStr = """
    DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};
    DBQ=""" + getDatabasepath() + ";"
    cnxn = pyodbc.connect(connStr)
    cursor = cnxn.cursor()
    return cursor, cnxn

def getDatabasepath():
    """get the path to the access database"""
    mycwd = os.getcwd()
    os.chdir("..")
    dataBasePath = os.getcwd() + os.sep + "LandsAccessTool.accdb"
    os.chdir(mycwd)
    return dataBasePath

# Connect to the Access Database
cursor, cnxn = dbConn()

# Update the Ledger with any new records from importDids
importDIDsACC()

【问题讨论】:

标签: python performance ms-access sql-insert pyodbc


【解决方案1】:

不要使用外部代码检查重复项。数据库(甚至 Access)的强大功能是最大化其数据集操作。不要试图重写那种代码,特别是因为你发现它效率不高。相反,将所有内容导入到临时数据库表中,然后使用 Access(或适当的 Access 数据引擎)执行 SQL 语句来比较表,查找或排除重复行。然后可以使用这些查询的结果来创建和/或更新其他表——所有这些都在数据库引擎的上下文中。当然,使用适当的索引和键设置临时表以最大限度地提高效率。

与此同时,在本地比较数据集(即表)时,通常更快(我可以说总是吗?)从单个数据库请求(即 SQL SELECT 语句)将所有值加载到某个可搜索的集合中,然后使用该内存集合来搜索匹配项。在我上次关于最大化数据库功能的声明之后,这似乎具有讽刺意味,但重要的想法是了解整个数据集是如何被处理的。在 python 进程和数据库引擎之间来回传输数据,即使它在同一台机器上,也将比处理 python 进程中的所有内容或数据库引擎进程中的所有内容要慢得多。唯一可能没有用的情况是远程数据集太大而无法下载,但 87,000 个键值绝对小到足以将所有值加载到 python 集合中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 2017-01-10
    • 2019-08-05
    • 1970-01-01
    • 2013-10-21
    相关资源
    最近更新 更多