DISTINCT查询解决方案

时间:2013-02-14 19:04:37

标签: sql ms-access

我有以下查询

SELECT DISTINCT T_Med.Comp, T_Med.ItemID, T_Med.WP, T_Med.DocName, T_Med.TrfCode, 
       T_Med.CatDescr, T_Med.SubCatDescr, T_LKPWP.RuleCode, T_MedWP.RuleCode, 
       T_Med.Group
FROM (T_Med INNER JOIN T_LKPWP ON (T_Med.DocName=T_LKPWP.SDocName)
AND (T_Med.[WP]=T_LKPWP.WP)) 
LEFT JOIN T_MedWP ON T_LKPWP.RuleCode=T_MedWP.RuleCode
WHERE (((T_MedWP.RuleCode) Is Null));

由于DISTINCT,需要很长时间才能处理。正如我发现的那样,即使我为这些相关领域创建了索引,它也无助于DISTINCT。没有DISTINCT相同的查询给出的结果不到一秒

此处T_Med有1070065条记录,T_MedWP有366315条记录,T_LKPWP有55条记录

无论如何以不同方式实现预期结果。我尝试创建一个没有DISTINCT的单独表,即使这需要很长时间。

感谢您的评论

2 个答案:

答案 0 :(得分:0)

 SELECT DISTINCT Comp, ItemID, WP, DocName, TrfCode, 
   CatDescr, SubCatDescr, RuleCode, .RuleCode, 
   Group FROM (

 maybe try this...

 SELECT T_Med.Comp, T_Med.ItemID, T_Med.WP, T_Med.DocName, T_Med.TrfCode, 
   T_Med.CatDescr, T_Med.SubCatDescr, T_LKPWP.RuleCode, T_MedWP.RuleCode, 
   T_Med.Group
  FROM (T_Med INNER JOIN T_LKPWP ON (T_Med.DocName=T_LKPWP.SDocName)
   AND (T_Med.[WP]=T_LKPWP.WP)) 
   LEFT JOIN T_MedWP ON T_LKPWP.RuleCode=T_MedWP.RuleCode
   WHERE (((T_MedWP.RuleCode) Is Null));

  )

还要检查一下Query taking long time with DISTINCT

答案 1 :(得分:0)

这是一般的Oracle示例,但也许它会给你一些想法。我不太了解Access,抱歉。无法确定Access中是否存在可用的运算符。

-- Same as Distinct --
SELECT deptno, dname FROM scott.dept D WHERE EXISTS ( SELECT 'X' FROM scott.emp E WHERE E.deptno = D.deptno)
/