有A、B两个表,A表中有字段C、B表中也有字段C并且都有索引,如果单独查字段C,两个表都是可以秒杀的,但是我想建个视图,直接查询字段C,但是要使用union all  。使用union all 后查询速度很慢,如果提高效率?

解决方案 »

  1.   

    不会慢吧 视图还是转化成事实表的select * from
    (select * from A union all select * from B) tmp
    where tmp.C=xxx;
    这个慢吗
      

  2.   

    是的,非常慢。 单独查秒杀,用了union all 要十多秒
      

  3.   

     explain显示没有使用到索引!
      

  4.   

    你用的SQL语句是什么? 贴出以供分析。
      

  5.   

     CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `view_c` AS select C,c1  from A union all select C,C2 from B A、B为物理表且字段C都有索引
    select *from view_c where C=xxx
    使用这个语句时速度非常慢
      

  6.   

    select C,c1  from A  where C=xxx
    union all
    select C,C2 from B  where C=xxx看一下速度如何。
    另外贴出
    show index from A;
    show index from B;
    explain select *from view_c where C=xxx;以供分析。
      

  7.   

    show index from A; show index from B; explain select *from view_c where C=xxx; 以供分析。
    另外请贴文本,不要贴图。