有没有办法加快 SQLite Update 命令的速度?

时间:2021-01-16 18:05:17

标签: python sql python-3.x sqlite xlrd

我正在从另一个表创建此表,然后尝试使用 Excel 工作表中的信息更新 SQL 表。更新功能非常慢,我希望找到一种方法来提高速度。这是我正在使用的。

masterDBConn = sqlite3.connect('MasterDB.db')
masterCursor = masterDBConn.cursor()

masterCursor.execute('CREATE TABLE MasterV2 (InventoryNbr, IndexListID, col1, col2, col3, col4, col5, col6, col7)')
masterCursor.execute('INSERT INTO MasterV2 SELECT InventoryNbr, MAX(IndexListID), col1, col2, col3, col4, col5, col6, col7 FROM Master GROUP BY InventoryNbr')
masterCursor.execute('CREATE INDEX InventoryNbrIndex ON MasterV2(InventoryNbr)')
masterDBConn.commit()


wb = xlrd.open_workbook(r'C:\Users\MasterDatabaseV2.xlsx')
ws = wb.sheet_by_index(0)
start_time = time.time()
for row in range(1, ws.nrows):
    print('here1 ---%s seconds ---' % (time.time() - start_time))
    number= ws.cell_value(row, 7).split(',')
    number= [x.strip(' ') for x in number]
    for holl in number:
        print('here2 ---%s seconds ---' % (time.time() - start_time))
        if len(holl) > 3:
            col1 = str(ws.cell_value(row, 0)).strip('.0')[0:255]
            col2 = str(ws.cell_value(row, 1)).strip()[0:255]
            col3 = str(ws.cell_value(row, 2)).strip()[0:255]
            col4 = str(ws.cell_value(row, 3)).strip()[0:255]
            col5 = str(ws.cell_value(row, 4)).strip()[0:255]
            col6 = str(ws.cell_value(row, 5)).strip()[0:255]
            col7 = str(ws.cell_value(row, 6)).strip()[0:255]
            print('here3 ---%s seconds ---' % (time.time() - start_time))
            masterCursor.execute('UPDATE MasterV2 SET col1 = ?, col2 = ?, col3 = ?, col4 = ?, col5 = ?, col6 = ?, col7 = ?', (col1, col2, col3, col4, col5, col6, col7))
            print('here4 ---%s seconds ---' % (time.time() - start_time))

这是我的时间打印的输出:

here1 ---0.003003358840942383 seconds ---
here2 ---0.003003358840942383 seconds ---
here3 ---0.003003358840942383 seconds ---
here4 ---1.7457876205444336 seconds ---
here1 ---1.7457876205444336 seconds ---
here2 ---1.7457876205444336 seconds ---
here3 ---1.7457876205444336 seconds ---
here4 ---2.5108072757720947 seconds ---

正如您所看到的,Update 语句每次执行都要花费半秒多的时间,我有大约 20,000 行需要更新信息。任何帮助表示赞赏。

编辑:我的 Update 语句中缺少 WHERE 子句,这解决了我的问题。

0 个答案:

没有答案