一个user表
一个product表
id
userid
product_name
如果想找到username='test' 的所有product的话 用
select * from product where userid in (select id from user where username='test')
这样的语句,可当数据量大的时候效率非常的底下那怎么样提高效率呢?

解决方案 »

  1.   

    select * from product inner join user on product.userid = user.id and user.username = 'test'
      

  2.   

    select * 
    from product a,user b
    where a.userid = b.id and b.username='test'
      

  3.   

    select a.* 
    from product a,user b
    where a.userid = b.id and b.username='test'
      

  4.   

    --exists能够替换in
    select * from product where exists(select 1 from user where username='test')
      

  5.   

    exists应该这样写:select * from product A where EXISTS(select * from user B where b.username='test' AND B.ID=A.userid )
    速度怎么样,楼主自己测吧