• sql中多表查询


    create table Testing
    (
        ID int identity(1,1) primary key nonclustered,
        Data float not null default 0,
        Style smallint not null default 0,
        AddTime DateTime not null default getdate(),
        Data2 float not null default 0,
        Data3 float not null default 0,
        Data4 float not null default 0,
        Data5 float not null default 0,
        Data6 float not null default 0,
        Data7 float not null default 0,
    ) 
    
    declare @j int
    declare @data float
    declare @style smallint
    set @j = 1
    while @j<5000000
        begin
           set @data = floor(rand()*100000.0000)
           set @style = CAST(rand() * 6 as smallint)
           insert into Testing(Data, Style, AddTime) values(@data,@style,getdate())
        set @j = @j + 1
    end 
    
    declare @d datetime
    set @d = getdate()
    declare   @i   int
    
     
    
    -------------------------------------------------------------------------------------
    set   @i=0
    while   @i <20
    begin
        select top 1 * from Testing with(readpast) where [AddTime]>=(select max([AddTime]) from Testing with(readpast) where [Style]=0)
        select top 1 * from Testing with(readpast) where [AddTime]>=(select max([AddTime]) from Testing with(readpast) where [Style]=0)
        set   @i   =   @i+1
    end
    -------------------------------------------------------------------------------------
    select [语句执行时间(毫秒)]=datediff(ms, @d, getdate())
    print '全部1 语句执行时间(毫秒):' + CONVERT(varchar(30), datediff(ms, @d, getdate()))
     
    
    --=======================================================================================
    set @d = getdate()
    -------------------------------------------------------------------------------------
    set   @i=0
    while   @i <20
    begin
        select top 1 * from Testing with(readpast) where [AddTime]>=(select max([AddTime]) from Testing with(readpast) where [Style]=0)
        union all
        select top 1 * from Testing with(readpast) where [AddTime]>=(select max([AddTime]) from Testing with(readpast) where [Style]=0)
    set   @i   =   @i+1
    end
    -------------------------------------------------------------------------------------
    select [语句执行时间(毫秒)]=datediff(ms, @d, getdate())
    print '全部2 语句执行时间(毫秒):' + CONVERT(varchar(30), datediff(ms, @d, getdate())) 
     
    
    --=======================================================================================
    
    set @d = getdate()
    -------------------------------------------------------------------------------------
    set   @i=0
    while   @i <20
    begin
        select top 1 Data from Testing with(readpast) where [AddTime]>=(select max([AddTime]) from Testing with(readpast) where [Style]=0)
        select top 1 Data from Testing with(readpast) where [AddTime]>=(select max([AddTime]) from Testing with(readpast) where [Style]=0)
        set   @i   =   @i+1
    end
    -------------------------------------------------------------------------------------
    select [语句执行时间(毫秒)]=datediff(ms, @d, getdate())
    print '截取1 语句执行时间(毫秒):' + CONVERT(varchar(30), datediff(ms, @d, getdate()))
     
    
    --=======================================================================================
    
    set @d = getdate()
    -------------------------------------------------------------------------------------
    set   @i=0
    while   @i <20
    begin
        select top 1 Data from Testing with(readpast) where [AddTime]>=(select max([AddTime]) from Testing with(readpast) where [Style]=0)
        union all
        select top 1 Data from Testing with(readpast) where [AddTime]>=(select max([AddTime]) from Testing with(readpast) where [Style]=0)
    set   @i   =   @i+1
    
    end
    
    -------------------------------------------------------------------------------------
    
    select [语句执行时间(毫秒)]=datediff(ms, @d, getdate())
    print '截取2 语句执行时间(毫秒):' + CONVERT(varchar(30), datediff(ms, @d, getdate()))
     
    
    
    结论,查询越少的字段速度越快,多条sql语句比组合成单条要慢
  • 相关阅读:
    基于python的知乎开源爬虫 zhihu_oauth使用介绍
    python scrapy 抓取脚本之家文章(scrapy 入门使用简介)
    模拟退火算法(SA)求解TSP 问题(C语言实现)
    遗传算法的C语言实现(二)-----以求解TSP问题为例
    遗传算法的C语言实现(一):以非线性函数求极值为例
    C语言实现粒子群算法(PSO)二
    C语言实现粒子群算法(PSO)一
    python wordcloud 对电影《我不是潘金莲》制作词云
    svn更新失败,解决
    java发送邮箱验证码
  • 原文地址:https://www.cnblogs.com/smallidea/p/2575052.html
Copyright © 2020-2023  润新知