表singer:
singerID,singerName
1,刘德华
2,张学友
3,郭富城
4,黎明表song:
songID,songName,singerID
1,忘情水,1
2,中国人,1
3,吻别,2
4,对你爱不完,3
5,今夜你会不会来,4我想查找刘德华的歌曲,有两种sql语句:
1.两表连接select song.songName
from song,singer
where song.singerID = singer.singerID
and singer.singerName = '刘德华'
2.嵌套select songName
from song
where singerID = (select singerID from singer where singerName = '刘德华')在数据量很大的情况是,是连接表更快还是嵌套更快?
谢谢!

解决方案 »

  1.   


    select * from icstockbillentry 
    --171010条记录,用时:45s
    ---------------
    --测试join方案用时
    -------------------
    declare @d datetime
    set @d=getdate()
    select b.fnote
    from icstockbill a,icstockbillentry b 
    where a.finterid=b.finterid
      and a.fbillno='JIN011320'
    select [语句执行花费时间(毫秒)]=datediff(ms,@d,getdate())/*
    语句执行花费时间(毫秒) 
    ------------ 
    6(所影响的行数为 1 行)
    */declare @d datetime
    set @d=getdate()
    select fnote
    from icstockbillentry
    where finterid=(select finterid from icstockbill where fbillno='JIN011320')
    select [语句执行花费时间(毫秒)]=datediff(ms,@d,getdate())
    /*
    语句执行花费时间(毫秒) 
    ------------ 
    5(所影响的行数为 1 行)
    */我测试差不多.
      

  2.   

    楼主可以按此方法测试一下.--select * from icstockbillentry 
    --171010条记录,用时:45s
    ---------------
    --测试join方案用时
    -------------------
    DBCC FREEPROCCACHE 
    GO
    DBCC DROPCLEANBUFFERS 
    GO
    SET Statistics IO ON
    GO
    declare @d datetime
    set @d=getdate()
    select song.songName
    from song,singer
    where song.singerID = singer.singerID
    and singer.singerName = '刘德华'
    select [语句执行花费时间(毫秒)]=datediff(ms,@d,getdate())
    SET Statistics IO OFF
    GO/*
    语句执行花费时间(毫秒) 
    ------------ 
    ?(所影响的行数为 1 行)
    */
    --------------
    --测试嵌套方案
    ------------------
    DBCC FREEPROCCACHE 
    GO
    DBCC DROPCLEANBUFFERS 
    GO
    SET Statistics IO ON
    GO
    declare @d datetime
    set @d=getdate()
    select songName
    from song
    where singerID = (select singerID from singer where singerName = '刘德华')
    select [语句执行花费时间(毫秒)]=datediff(ms,@d,getdate())
    SET Statistics IO OFF
    GO
    /*
    语句执行花费时间(毫秒) 
    ------------ 
    ?(所影响的行数为 1 行)
    */
      

  3.   

    向tony哥学习!链接比嵌套要快!
      

  4.   

    还是没有一个确定的结果
    oracle那边发帖问这个问题 答案也不同意