a.name建的索引在这个查询没有作用,因为你用的是LIKE '%12%'
语句好象没有优化的余地了,还是调整一下表结构吧,表的关系太多了.

解决方案 »

  1.   

    试试这样:
    Select distinct a.ID,a.name,sex=
    case a.sex
    when 0 then '男'
    when 1 then '女'
    end, a.Company, a.Department, a.business 
    from Ta_AddressBook a,
    Ta_AddressBookTree b
    where a.name like '%12%' 
    and a.ABTID=b.ID 
    and b.PUID=2
    union
    Select distinct a.ID,a.name,sex=
    case a.sex
    when 0 then '男'
    when 1 then '女'
    end, a.Company, a.Department, a.business 
    from Ta_AddressBook a,
    Ta_FistCatalogTree FCT,
    Ta_FistCatalog FC,
    Ta_FistCatalogRight FCR,
    Ta_FistCatalogAndRight FCAR 
    where a.name like '%12%' 
    and a.ABTID=FCT.ID 
    and FCT.Attribute=FC.AFCT 
    AND FC.ID=FCAR.FCID 
    and FCAR.FCRID=FCR.ID 
    AND FCAR.UserID=2
      

  2.   

    试试下面的语句Select distinct a.ID,a.name,sex=
    case a.sex
    when 0 then '男'
    when 1 then '女'
    end, a.Company, a.Department, a.business from Ta_AddressBook a inner join Ta_AddressBookTree b on a.ABTID=b.ID
    WHERE a.name like '%12%' and b.PUID=2
    union 
    Select distinct a.ID,a.name,sex=
    case a.sex
    when 0 then '男'
    when 1 then '女'
    end, a.Company, a.Department, a.business from Ta_AddressBook a INNER JOIN Ta_FistCatalogTree FCT ON a.ABTID=FCT.ID
    inner join Ta_FistCatalog FC on FCT.Attribute=FC.AFCT
    inner join Ta_FistCatalogRight FCR on FC.ID=FCAR.FCID
    inner join Ta_FistCatalogAndRight on FCAR.FCRID=FCR.ID
    WHERE a.name like '%12%' and FCAR.UserID=2
      

  3.   

    to Haiwer(Haiwer)
        我也想调整表结构,问题是这不是由我定的!
    to zhhp6489(坐看云起时)
        其实我的语句也是使用的内联接,SQLSERVER会自动转为内联接写法
      

  4.   

    Select distinct a.ID,a.name,sex= case a.sex when 0 then '男' when 1 then '女' end,
           a.Company, a.Department, a.business
      from Ta_AddressBook a,
           Ta_AddressBookTree b,
           Ta_FistCatalogTree FCT,
           Ta_FistCatalog FC,
           Ta_FistCatalogRight FCR,
           Ta_FistCatalogAndRight FCAR
     where a.name like '%12%'
       and ((a.ABTID=b.ID and b.PUID=2)
             or
            (a.ABTID=FCT.ID and FCT.Attribute=FC.AFCT AND FC.ID=FCAR.FCID and FCAR.FCRID=FCR.ID AND FCAR.UserID=2))问题出在OR上,表多不怕,就怕非约束性的查询,象OR就是.你把OR拿掉比较一下.
      

  5.   

    非常感谢Haiwer(海阔天空)和以上回复的各位朋友,Haiwer(海阔天空) 提出的方法非常好,用合并记录集的方式大大减少了扫描行,事实证明提高了很大效率,谢谢!