select distinct * from table1 
where table1.字段1 not in(select table2.字段1 from table2)执行上面的查询奇慢table1 大约有200万条数据
table2 大约有2000万条数据
table1和table2都没有设置索引请教高手如何能提高查询的速度?

解决方案 »

  1.   

    對table1.字段1和table2.字段1分別建立索引。
    sql改成
    select * from table1
    where not exists (select 1 from table2 where table2.字段1 = table1.table1);希望有所幫助。
      

  2.   

    1,在table2的"字段1"上建立索引;
    2,将语句修改为
      select distinct * from table1 
    where not exists(select 1 from table2 where table1.字段1 = table2.字段1)
      

  3.   

    两个表应该分别如何建立索引,建立哪种类型的索引,最好有详细的sql语句,谢谢!
    talbe1的字段1是没有重复值的
    table2的字段1有重复值另外到底需不需要两个表都建立索引
      

  4.   

    1.在table1的"字段1"上建立索引:create unique index uindx_1 on table(字段1);
    select distinct * from table1
    where not exists(select 1 from table2 where table1.字段1 = table2.字段1)2.建议不要用‘*’代替所有列名。
      

  5.   

    我现在创建索引已经2个小时了,还是没有完成
    命令:create index uindx_1 on table2(字段1);
      

  6.   

    select * from table1 
    where table1.字段1=table2.字段1(+) and table2.字段1 is null这样查询可能快一点
      

  7.   


    create   index uindx_1 on table1(字段1);
    create   index uindx_2 on table2(字段1);
    select distinct * from table1 where table1.字段1 not exists (select table1.字段1   from table2,table1 where table1.字段1=table2.字段1);
      

  8.   


    create   index uindx_1 on table1(字段1);
    create   index uindx_2 on table2(字段1);
    select distinct * from table1 where not exists (select table1.字段1   from table2,table1 where table1.字段1=table2.字段1);
      

  9.   

    其实一般的解决办法就是NOT EXISTS 和外连接楼主本身的SQL 中带有DISTINCT ,其实已经决定了低效率........常用GROUP BY  来代替一下DISTINCT 的,这两个低层不一样,一个是聚合,一个是排序.
    尝试一下这样select table1.字段1
      from table1
     where table1.字段1 = table2.字段1(+)
       and table2.字段1 is null
      group by table1.字段1 
    这个也可以尝试一下
    方法二:  select table1.字段1
        from table1
       group by table1.字段1 except
                 select table2.字段1 from table2 group by table2.字段1
    希望对你有帮助,一般来说尽量不要用DISTINCT , 实在是业务需要的话, 找点别的方法替代.
    最好的办法是说服客户,不要这样做哈...............
      

  10.   

    select distinct * from table1
    where table1.字段1 not in(select table2.字段1 from table2)1.把这个语句修改为join来处理,
    2.建立索引