mySQL + vba,检查一个表是否已经存在一个项目

我有一个电子表格,用于连接到我的MySQL数据库,并对该数据库进行更改。 我想我在系统中检查重复的方式是非常低效的,但我很难想到一个更有效的方法。 每件物品所需的时间不会超过100件,但是50,000件物品的使用时间非常长,而且我试图find一种方法来缩短这个过程需要花费的时间。

在Excel中

'/ Define last row with data in it lastRow = Range("C" & Rows.Count).End(xlUp).Row '/ go through each row, line by line, to check if this part number is in the system For c = 5 To lastRow Step 1 '/ Based on user input, runs a SELECT query to see if the part number in question is already in the table they're trying to upload into SQLStr = "SELECT quotePartNumber FROM " & table & " where (quotePartNumber, `debitNumber) IN (('" & partNumber & "', '" & debitNum & "')) LIMIT 1"` rs.Open SQLStr, conn, adOpenStatic ' Dump to spreadsheet With Worksheets("DATA").Range("N28:N28") .ClearContents '/ copy SQL output to cell N28, then check if an output exists. If the part exists in the table already, a part number will fill N28, if the part doesn't exist, N28 will be blank. .CopyFromRecordset rs End With '/ If the part already exists, delete this part number from the spreadsheet. pnCheck = Range("N28").Value If pnCheck <> "" Then Range(Cells(c, 1), Cells(c, 11)).Select Selection.Delete Shift:=xlUp deleted = deleted + 1 c = c - 1 End If '/ reset rs for the next run through rs.Close Set rs = Nothing Range("N28") = "" debit = False next c 

所以,基本上,我通过电子表格中的每一行,检查该部分是否已经存在于我的表中,如果存在,我从电子表格中删除该行。 一旦列表被削减到什么不是在表中,我上传它(代码不显示在这里…)

是否有更有效的方法检查5000到100,000个零件号码,而不是逐行检查?

对不起,最初被误解了。

如果您尝试上传包含重复ID的表格,mySQL如何响应? 该字段应该被定义为需要唯一的值,如果这是它所需要的,我想MySQL会抛出一个警告并转储违规的行。

除此之外,Excel可以做得比你反复查询,写入,删除和更新要快。 你可以将所有表格的ID转储到另一张表格中的电子表格中吗? 然后你可以使用Excel的countif公式,并删除超过1的任何东西。我在手机上,所以很难具体,但每个电子表格ID旁边有一个countif,并计数是否在MySQL的ID范围。 然后过滤> 1并删除。 您也可以将ID转储到一列中,使用相邻的列来跟踪什么是电子表格和什么是数据库,使用Excel的删除重复,然后删除数据库条目。 剩下的就是唯一电子表格条目。

一般来说,对于性能来说,select和修改是很慢的。 不要select范围并删除select,请彻底删除范围。 删除和移动也很慢,所以也许只是在相邻的列中标记违规的行,然后一次删除它们。 另外,closuresscreenupdating,application.screenupdating = false。 更新屏幕也很慢。