sql server 中 bigint 和 datetime 性能比較


-- 創建表
create table Test_tbl
(
    ID varchar(40) primary key nonclustered,
    IntCol int,
	DateCol datetime
) 

--==================================================================================
-- 【100w數據測試】
--==================================================================================
-- 創建100w測試數據
declare @j int
declare @data float
declare @style bigint
set @j = 1
while @j<1000000
    begin
       set @style = cast(replace(replace(replace(convert(varchar(30),GETDATE(),120),'-',''),':',''), ' ', '') as bigint)
       insert into Test_tbl(ID, IntCol, DateCol) values(NEWID(),@style, getdate())
    set @j = @j + 1
end 

declare @d datetime
set @d = getdate()
declare   @i   int

print '【100w數據 查詢100次測試】'
-- 測試性能1,datetime
-------------------------------------------------------------------------------------
set nocount on -- 不顯示受影響行數
set   @i=0
while   @i <20
begin
    select top 1 * from Test_tbl where DateCol>getdate()
    set   @i   =   @i+1
end
-------------------------------------------------------------------------------------
select [語句執行時間(毫秒)]=datediff(ms, @d, getdate())
set nocount off 
print '100w數據 date 語句執行時間(毫秒):' + CONVERT(varchar(30), datediff(ms, @d, getdate()))


-- 測試性能2,int
-------------------------------------------------------------------------------------
set nocount on -- 不顯示受影響行數
set   @i=0
while   @i <20
begin
    select top 1 * from Test_tbl where IntCol>20151212030303
    set   @i   =   @i+1
end
-------------------------------------------------------------------------------------
select [語句執行時間(毫秒)]=datediff(ms, @d, getdate())
set nocount off 
print '100w數據 int 語句執行時間(毫秒):' + CONVERT(varchar(30), datediff(ms, @d, getdate()))


--==================================================================================
-- 【1000w數據測試】
--==================================================================================
-- 創建900w測試數據,累計1000w
set @j = 1
while @j<9000000
    begin
       set @style = cast(replace(replace(replace(convert(varchar(30),GETDATE(),120),'-',''),':',''), ' ', '') as bigint)
       insert into Test_tbl(ID, IntCol, DateCol) values(NEWID(),@style,getdate())
    set @j = @j + 1
end 

print '【1000w數據 查詢100次測試】'
-- 測試性能1,datetime
-------------------------------------------------------------------------------------
set nocount on -- 不顯示受影響行數
set   @i=0
while   @i <100
begin
    select top 1 * from Test_tbl where DateCol>getdate()
    set   @i   =   @i+1
end
-------------------------------------------------------------------------------------
select [語句執行時間(毫秒)]=datediff(ms, @d, getdate())
set nocount off 
print '1000w數據 date 語句執行時間(毫秒):' + CONVERT(varchar(30), datediff(ms, @d, getdate()))

-- 測試性能2,int
-------------------------------------------------------------------------------------
set nocount on -- 不顯示受影響行數
set   @i=0
while   @i <100
begin
    select top 1 * from Test_tbl where IntCol>20151212030303
    set   @i   =   @i+1
end
-------------------------------------------------------------------------------------
select [語句執行時間(毫秒)]=datediff(ms, @d, getdate())
set nocount off 
print '1000w數據 int 語句執行時間(毫秒):' + CONVERT(varchar(30), datediff(ms, @d, getdate()))

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM