SQL Server 2005大批量數(shù)據(jù)的操作與實(shí)例演示
我們今天主要向大家講述的是SQL Server 2005大批量數(shù)據(jù)的實(shí)際操作以及其使用的實(shí)例描述,以下是文章的具體介紹,望你瀏覽完以下的內(nèi)容會(huì)有所收獲。我們首先是以問(wèn)題提出的方式來(lái)對(duì)其進(jìn)行講述:
在SQL Server 2005數(shù)據(jù)庫(kù)中建立兩個(gè)類(lèi)型相同的數(shù)據(jù)表,如下
- create table test1
- (
- iId int identity(1,1) not null,
- vTest1Code varchar(30) not null,
- vName varchar(30) not null,
- dDate datetime,
- primary key(iId)
- )
- create table test2
- (
- Id int identity(1,1) not null,
- Code varchar(30) not null,
- Name varchar(30) not null,
- date datetime,
- primary key(Id)
- )
兩表所占用的系統(tǒng)空間
- exec sp_spaceused 'test1' exec sp_spaceused 'test2'
- Name Rows Reserved Data Index_size unused
- Test1 0 0KB 0KB 0KB 0KB
- Test2 0 0KB 0KB 0KB 0KB
由上圖得知兩表所占用的系統(tǒng)空間一致。
執(zhí)行數(shù)據(jù)插入操作
--測(cè)試TEST1
- declare @startTime datetime
- set @startTime=getdate()
- declare @i int
- set @i=1
- while @i<100
- begin
- insert into test1(vTest1Code,vName) values('vCode'+CAST(@i as varchar(10)),'vName'+CAST(@i as varchar(10)))
- set @i=@i+1
- end
select [語(yǔ)句執(zhí)行花費(fèi)時(shí)間(毫秒)]=datediff(ms,@startTime,getdate())
go
--測(cè)試TEST2
- declare @startTime datetime
- set @startTime=getdate()
- declare @i int
- set @i=1
- while @i<100
- begin
- insert into test2(Code,Name) values('vCode'+CAST(@i as varchar(10)),'vName'+CAST(@i as varchar(10)))
- set @i=@i+1
- end
select [語(yǔ)句執(zhí)行花費(fèi)時(shí)間(毫秒)]=datediff(ms,@startTime,getdate())
go
插入耗時(shí)情況
test1語(yǔ)句執(zhí)行花費(fèi)時(shí)間(毫秒) test2語(yǔ)句執(zhí)行花費(fèi)時(shí)間(毫秒)
- 100條 30 30
- 1000條 250 250
- 10000條 2623 2516
- 100000條 26453 26560
- 1000000條 275110 282796
最后兩表所占用的系統(tǒng)空間
- exec sp_spaceused 'test1' exec sp_spaceused 'test2'
- Name Rows Reserved Data Index_size unused
- Test1 1000098 48520KB 48272KB 192KB 56KB
- Test2 1000098 48520KB 48272KB 192KB 56KB
問(wèn)題現(xiàn)象描述:
<!--[if !supportLists]-->1、 <!--[endif]-->在相同數(shù)據(jù)類(lèi)型、長(zhǎng)度,及約束、索引的情況下,執(zhí)行千條及千條以內(nèi)的數(shù)據(jù)插入操作時(shí),字段長(zhǎng)度、系統(tǒng)保留字對(duì)SQL語(yǔ)句的執(zhí)行速度沒(méi)有影響或者影響很小;執(zhí)行上萬(wàn)條數(shù)據(jù)插入操作時(shí),字段長(zhǎng)度對(duì)SQL語(yǔ)句的執(zhí)行速度影響很?。粓?zhí)行十萬(wàn)條以上的數(shù)據(jù)操作時(shí),系統(tǒng)保留字對(duì)SQL語(yǔ)句的執(zhí)行速度影響明顯。
<!--[if !supportLists]-->2、 <!--[endif]-->數(shù)據(jù)字段長(zhǎng)度、系統(tǒng)保留字對(duì)系統(tǒng)占用的空間沒(méi)有任何影響。
<!--[if !supportLists]-->3、 <!--[endif]-->在SQL Server 2005大批量數(shù)據(jù)操作時(shí),數(shù)據(jù)類(lèi)型、長(zhǎng)度,甚至數(shù)據(jù)字段是否為系統(tǒng)保留字,對(duì)SQL語(yǔ)句的執(zhí)性效率都有影響。
問(wèn)題總結(jié):
<!--[if !supportLists]-->1、 <!--[endif]-->SQL語(yǔ)句在執(zhí)行時(shí),將首先對(duì)相關(guān)數(shù)據(jù)表進(jìn)行連接,然后進(jìn)行過(guò)濾、分組、選擇字段、DISTINCT、ORDER BY等操作。由此,我們?cè)谶M(jìn)行數(shù)據(jù)查詢時(shí),應(yīng)盡量避免“*”連接,應(yīng)考慮過(guò)濾的先后順序。
<!--[if !supportLists]-->2、 <!--[endif]-->謹(jǐn)慎使用游標(biāo)、觸發(fā)器、索引。
<!--[if !supportLists]-->3、 <!--[endif]-->盡量避免使用系統(tǒng)保留字,考慮在SQL語(yǔ)句中區(qū)分?jǐn)?shù)據(jù)字段的大小寫(xiě),即SQL語(yǔ)句中的字段名的形式應(yīng)和數(shù)據(jù)表中的字段名的形式一致。
以上的相關(guān)內(nèi)容就是對(duì)SQL Server 2005大批量數(shù)據(jù)操作使用實(shí)例的介紹,望你能有所收獲。
【編輯推薦】
- 正確維護(hù)Sql Server表索引的2個(gè)步驟
- SQL Server數(shù)據(jù)轉(zhuǎn)換服務(wù)的妙招之一
- SQL Server數(shù)據(jù)庫(kù)的妙招用法
- SQL Server數(shù)據(jù)轉(zhuǎn)換服務(wù)利用與導(dǎo)入式格式的描述
- 正確維護(hù)Sql Server表索引的2個(gè)步驟