问题提出:
在sql server2005数据库中建立两个类型相同的数据表,如下
|
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) ) |
两表所占用的系统空间
|
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 |
| |
由上图得知两表所占用的系统空间一致。
执行数据插入操作
|
--测试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 [语句执行花费时间(毫秒)]=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 [语句执行花费时间(毫秒)]=datediff(ms,@startTime,getdate()) go |
插入耗时情况
|
|
test1语句执行花费时间(毫秒) |
test2语句执行花费时间(毫秒) |
|
100条 |
30 |
30 |
|
1000条 |
250 |
250 |
|
10000条 |
2623 |
2516 |
|
100000条 |
26453 |
26560 |
|
1000000条 |
275110 |
282796 |
最后两表所占用的系统空间
|
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 |
| |
问题现象描述:
1、 在相同数据类型、长度,及约束、索引的情况下,执行千条及千条以内的数据插入操作时,字段长度、系统保留字对SQL语句的执行速度没有影响或者影响很小;执行上万条数据插入操作时,字段长度对SQL语句的执行速度影响很小;执行十万条以上的数据操作时,系统保留字对SQL语句的执行速度影响明显。
2、 数据字段长度、系统保留字对系统占用的空间没有任何影响。
3、 在大批量数据操作时,数据类型、长度,甚至数据字段是否为系统保留字,对SQL语句的执性效率都有影响。
问题总结:
1、 SQL语句在执行时,将首先对相关数据表进行连接,然后进行过滤、分组、选择字段、DISTINCT、ORDER BY等操作。由此,我们在进行数据查询时,应尽量避免“*”连接,应考虑过滤的先后顺序。
2、 谨慎使用游标、触发器、索引。
3、 尽量避免使用系统保留字,考虑在SQL语句中区分数据字段的大小写,即SQL语句中的字段名的形式应和数据表中的字段名的形式一致。
本文通过实验对比了在SQL Server 2005中不同字段名(包括系统保留字)对数据插入操作速度的影响,发现对于小批量数据操作,字段长度和系统保留字几乎不影响执行速度;而对于大量数据操作,系统保留字则对执行速度产生一定影响。
2331

被折叠的 条评论
为什么被折叠?



