我使用以下SQL查询来比较两个Excel工作表(两者的设置差异)
strSql = "SELECT [CD_CLIENTE], [NM_CLIENTE], [CPF/CNPJ], [DS_DOCUMENTO], [DTVALIDADE] FROM [BaseAnterior$] " & _
"WHERE NOT EXISTS ( " & _
"SELECT * FROM [BaseAtual$] WHERE" & _
"[BaseAtual$].[CD_CLIENTE] = [BaseAnterior$].[CD_CLIENTE] AND " & _
"[BaseAtual$].[NM_CLIENTE] = [BaseAnterior$].[NM_CLIENTE] AND " & _
"[BaseAtual$].[DS_DOCUMENTO] = [BaseAnterior$].[DS_DOCUMENTO] AND " & _
"[BaseAtual$].[CPF/CNPJ] = [BaseAnterior$].[CPF/CNPJ] AND " & _
"[BaseAtual$].[DTVALIDADE] = [BaseAnterior$].[DTVALIDADE]" & _
")"
声明很好,问题是它很慢。工作表有aprox。 100k记录(不要问我为什么他们不在一个严肃的数据库中)并且执行可能需要几分钟。我知道问题出在我的SQL语句上,所以我的问题是:有没有更快的方法来选择SQL中的设置差异?
答案 0 :(得分:1)
首先尝试按所有列排序两张纸,然后尝试:
strSql = "SELECT " &_
"[B1].[CD_CLIENTE], [B1].[NM_CLIENTE], [B1].[CPF/CNPJ], [B1].[DS_DOCUMENTO], [B1].[DTVALIDADE], " &_
"[B2].[CD_CLIENTE], [B2].[NM_CLIENTE], [B2].[CPF/CNPJ], [B2].[DS_DOCUMENTO], [B2].[DTVALIDADE] " &_
[FROM BaseAnterior$] [B1] " & _
"FULL JOIN [BaseAtual$] [B2] " & _
"ON [B2].[CD_CLIENTE] = [B1].[CD_CLIENTE] AND " & _
"[B2].[NM_CLIENTE] = [B1].[NM_CLIENTE] AND " & _
"[B2].[DS_DOCUMENTO] = [B1].[DS_DOCUMENTO] AND " & _
"[B2].[CPF/CNPJ] = [B1].[CPF/CNPJ] AND " & _
"[B2].[DTVALIDADE] = [B1].[DTVALIDADE]"
答案 1 :(得分:0)
最快的方法可能根本就是不使用SQL。执行以下操作:
Excel在排序记录方面做得非常好。然后比较应该非常快。
在最初的表述中,必须将一张表中的每条记录与另一张表中的所有记录进行比较(好,也许是一半)。这需要很长时间。