select id, Name 
     from tableA
     where type='1' and 
           id in  
               (select id 
                      from web@dbserver:tableB
                      where role_name in ('角色1','角色2','角色3')
                )
其中TABLEA是仅仅有200条数据的表 tableB 数据有5万条左右
现在需要检索 tableA表中 类型为1的 并且ID 也在tableb中角色为'角色1','角色2','角色3'的ID集合中
这个查询 都序号4  5秒左右呢  两个表不是同一个数据库
修改成:
       select a.id, a.Name 
              from tableA a,web@dbserver:tableB b
              where a.id= b.id and a.type = '1'
                    and b.role_name in ('角色1','角色2','角色3')
 速度还是没啥变化

解决方案 »

  1.   

    select id, Name 
    from tableA a, [web@dbserver:tableB] b
    where a.type='1' and a.id = b.id and b.role_name in ('角色1','角色2','角色3')
      

  2.   

    和我第二种没啥区别啊 就是type='1' 和 ID关联调整了一下位置而已  不过似乎where是从右边开始的 那么我的理论上讲比你的应该还要快啊
      

  3.   

    select id, Name 
    from tableA
    where type='1' and 
    id in 
    (select id 
    from web@dbserver:tableB
    where role_name in ('角色1','角色2','角色3')
    )改成 
    select id, Name 
    from tableA
    where type='1' and 
    exists 
    (select * 
    from web@dbserver:tableB
    where role_name in ('角色1','角色2','角色3') and tableA.id=id
    )在 web@dbserver:tableB上的id 上加索引  role_nam上家索引
    理由 因为这里tableB是大表 这样可以用到tableB的ID上索引
      

  4.   


    select id,Name from tableA a,(
    select id,role_name from [web@dbserver:tableB] where role_name in ('角色1','角色2','角色3') as b)
    where a.type = '1' and a.id = b.id试试这样行不行,
    我的思路是先把tableB中的指定角色的数据查出来作为一个临时表,然后再联表查询.不知会不会有提高速度,
    楼主还请告之结果.