在Access中长时间运行SQL查询

时间:2013-11-15 23:31:48

标签: sql ms-access

我正在尝试编写一个查询,由于某种原因导致Access 2007-2010只是挂起而我认为它是因为查询只是永远。我从来没有等过足够长的时间来看它是否会返回结果。

我有一张桌子" MAIN"拥有100k +家庭住址和另一张桌子," ALIAS"有100个地址。 " MAIN" table是我想要返回的记录。 " ALIAS" table有一小部分来自MAIN的地址,其他地址在MAIN中不存在。 ALIAS表用于对相关的地址组进行分组。可以将其视为当前/ MAIN地址和旧/ ALIAS地址。他们在ALIAS表中共享一个ID,所以我知道哪些是相关的。

因此,查询地址并返回MAIN表中的地址或MAIN表中ALIAS表中具有记录关系的任何内容。这是一个例子:

MAIN
号码,方向,名称,类型
123 W 1st ST
456 W 1st ST

ALIAS
号码,方向,名称,类型,组ID
456 W 1st ST 99
789 E 2nd ST 99
因此,如果用户查询789 E 2nd ST,它应该从MAIN返回456 W 1st ST,因为789地址存在于ALIAS表中并且与456相关,AND 456也在MAIN中。

以下是我的查询,它运行时间过长,可能需要数天才能返回...

SELECT dbo_ae_dt9.docid, dbo_ae_dt9.ID, dbo_ae_dt9.Permit, dbo_ae_dt9.stDirection, 
dbo_ae_dt9.stNumber, dbo_ae_dt9.stType, dbo_ae_dt9.inspDate, dbo_ae_dt9.stName, 
dbo_ae_dt9.numobjects
FROM dbo_ae_dt9 LEFT JOIN dbo_ae_alias ON 
(dbo_ae_dt9.stNumber=dbo_ae_alias.stNumber) AND 
(dbo_ae_dt9.stDirection=dbo_ae_alias.stDirection) AND 
(dbo_ae_dt9.stName=dbo_ae_alias.stName) AND 
(dbo_ae_dt9.stType=dbo_ae_alias.stType)
WHERE (dbo_ae_alias.aliasID in  
   (SELECT aliasID   
    FROM dbo_ae_alias  
    WHERE ((IIf(IsNull(Forms!frmMain!txtstName),dbo_ae_alias.stName like "*", dbo_ae_alias.stName=Forms!frmMain!txtstName)) AND
    (IIf(IsNull(Forms!frmMain!txtstNumber),dbo_ae_alias.stNumber like "*", dbo_ae_alias.stNumber=Forms!frmMain!txtstNumber)) AND
    (IIf(IsNull(Forms!frmMain!txtstDirection),dbo_ae_alias.stDirection like "*", dbo_ae_alias.stDirection=Forms!frmMain!txtstDirection)) AND
    (IIf(IsNull(Forms!frmMain!txtstType),dbo_ae_alias.stType like "*", dbo_ae_alias.stType=Forms!frmMain!txtstType))))) OR
    ((IIf(IsNull([Forms]![frmMain]![txtPermit]),dbo_ae_dt9.Permit Like "*",dbo_ae_dt9.Permit = Forms!frmMain!txtPermit)) And
    (IIf(IsNull(Forms!frmMain!txtstNumber),dbo_ae_dt9.stNumber Like "*",dbo_ae_dt9.stNumber = Forms!frmMain!txtstNumber)) And
    (IIf(IsNull(Forms!frmMain!txtstDirection),dbo_ae_dt9.stDirection Like "*",dbo_ae_dt9.stDirection = Forms!frmMain!txtstDirection)) And 
    (IIf(IsNull(Forms!frmMain!txtstName),dbo_ae_dt9.stName Like "*",dbo_ae_dt9.stName = Forms!frmMain!txtstName)) And 
    (IIf(IsNull(Forms!frmMain!txtstType),dbo_ae_dt9.stType Like "*",dbo_ae_dt9.stType = Forms!frmMain!txtstType)) And 
    (IIf(IsNull(Forms!frmMain!txtstInspDate),dbo_ae_dt9.inspDate Like "*",dbo_ae_dt9.inspDate = Forms!frmMain!txtstInspDate)))

0 个答案:

没有答案