SQL Server 2005大批量數(shù)據(jù)的操作與實例演示
我們今天主要向大家講述的是SQL Server 2005大批量數(shù)據(jù)的實際操作以及其使用的實例描述,以下是文章的具體介紹,望你瀏覽完以下的內(nèi)容會有所收獲。我們首先是以問題提出的方式來對其進行講述:
在SQL Server 2005數(shù)據(jù)庫中建立兩個類型相同的數(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ù)插入操作
--測試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 [語句執(zhí)行花費時間(毫秒)]=datediff(ms,@startTime,getdate())
go
--測試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 [語句執(zhí)行花費時間(毫秒)]=datediff(ms,@startTime,getdate())
go
插入耗時情況
test1語句執(zhí)行花費時間(毫秒) test2語句執(zhí)行花費時間(毫秒)
- 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
問題現(xiàn)象描述:
<!--[if !supportLists]-->1、 <!--[endif]-->在相同數(shù)據(jù)類型、長度,及約束、索引的情況下,執(zhí)行千條及千條以內(nèi)的數(shù)據(jù)插入操作時,字段長度、系統(tǒng)保留字對SQL語句的執(zhí)行速度沒有影響或者影響很??;執(zhí)行上萬條數(shù)據(jù)插入操作時,字段長度對SQL語句的執(zhí)行速度影響很??;執(zhí)行十萬條以上的數(shù)據(jù)操作時,系統(tǒng)保留字對SQL語句的執(zhí)行速度影響明顯。
<!--[if !supportLists]-->2、 <!--[endif]-->數(shù)據(jù)字段長度、系統(tǒng)保留字對系統(tǒng)占用的空間沒有任何影響。
<!--[if !supportLists]-->3、 <!--[endif]-->在SQL Server 2005大批量數(shù)據(jù)操作時,數(shù)據(jù)類型、長度,甚至數(shù)據(jù)字段是否為系統(tǒng)保留字,對SQL語句的執(zhí)性效率都有影響。
問題總結(jié):
<!--[if !supportLists]-->1、 <!--[endif]-->SQL語句在執(zhí)行時,將首先對相關(guān)數(shù)據(jù)表進行連接,然后進行過濾、分組、選擇字段、DISTINCT、ORDER BY等操作。由此,我們在進行數(shù)據(jù)查詢時,應(yīng)盡量避免“*”連接,應(yīng)考慮過濾的先后順序。
<!--[if !supportLists]-->2、 <!--[endif]-->謹慎使用游標、觸發(fā)器、索引。
<!--[if !supportLists]-->3、 <!--[endif]-->盡量避免使用系統(tǒng)保留字,考慮在SQL語句中區(qū)分數(shù)據(jù)字段的大小寫,即SQL語句中的字段名的形式應(yīng)和數(shù)據(jù)表中的字段名的形式一致。
以上的相關(guān)內(nèi)容就是對SQL Server 2005大批量數(shù)據(jù)操作使用實例的介紹,望你能有所收獲。
【編輯推薦】
- 正確維護Sql Server表索引的2個步驟
- SQL Server數(shù)據(jù)轉(zhuǎn)換服務(wù)的妙招之一
- SQL Server數(shù)據(jù)庫的妙招用法
- SQL Server數(shù)據(jù)轉(zhuǎn)換服務(wù)利用與導入式格式的描述
- 正確維護Sql Server表索引的2個步驟