现在有两个表(结构相同) T1 和 T2, 两个表都有20几万条记录, 中间有一个关联字段 pid目前执行查询sql:
select t.* from (
    select * from T1 where pid = 2
    union all 
    select * from T2 where pid = 2
) t但发现查询速度很慢, 各位有没有好的办法提高查询效率

解决方案 »

  1.   

        select * from T1 where pid = 2 
        union all  
        select * from T2 where pid = 2 
    直接这样即可.
      

  2.   

    外面的select t.*没什么用吧,括号里的即可
      

  3.   

    直接
        select * from T1 where pid = 2 
        union all  
        select * from T2 where pid = 2 分别在两个表的 PID 字段上面建索引
      

  4.   


    同时对T1,T2表的PID字段建立索引.20多万的表估计在一分钟之内查询出.
      

  5.   

    谢谢各位了我改成下面这种效果也是挺慢的
    select * from T1 where pid = 2
    union all
    select * from T2 where pid = 2两个表加起来差不多 50万条记录了, pid索引我也建了, 但速度还是挺慢的, 而且这两个表数据量是每天都在增加的, 是不是有比这种查询更好的方法呢
      

  6.   

    pid加上索引,或者直接写括号中的