单数据库,多数据库,单实例,多实例不同情况下的数据访问效率测试
2011-06-28 12:07
471 查看
最近公司的项目准备优化一下系统的性能,希望在数据库方面看有没有提升的空间,目前压力测试发现数据库服务器压力还不够大,Web服务器压力也不是很大的情况下,前台页面访问却很慢,看有没有办法充分利用数据库服务器的性能,于是做了一个单数据库,多数据库,单实例,多实例不同情况下的数据访问效率测试。
测试环境:
CPU:Inter Core2 Quad,Q8300,2.50GHz;
内存:4.00GB
系统:Windows 7 32位系统
数据库系统:SqlServer 2008,有两个实例,一个是默认实例,一个是命名实例QE2
测试数据:
67万真实的基金收益数据,将这个表的数据放到了3个数据库中,详细内容见下面的连接字符串配置:
<add name ="Ins1_DB1" connectionString ="Data Source=.;Initial Catalog=TestDB;Integrated Security=True"/>
<add name ="Ins1_DB2" connectionString ="Data Source=.;Initial Catalog=LocalDB;Integrated Security=True"/>
<add name ="Ins2_DB" connectionString ="Data Source=.\QE2;Initial Catalog=TestDB;Integrated Security=True"/>
测试内容:
首先筛选出表中所有的基金代码,然后统计每只基金的最新收益率日期,对应的T-SQL代码如下:
declare @max_fsrq datetime
declare @currJJDM varchar(10)
declare @temp table (jjdm2 varchar(10))
declare @useTime datetime
set @useTime =GETDATE ();
insert into @temp(jjdm2)
select jjdm from [FundYield] group by jjdm order by jjdm asc
while EXISTS (select jjdm2 from @temp)
begin
set @currJJDM=(select top 1 jjdm2 from @temp)
select @max_fsrq = MAX(fsrq) from [FundYield] where jjdm=@currJJDM
delete from @temp where jjdm2 =@currJJDM
print @max_fsrq
end
print 'T-SQL Execute Times(ms):'
print datediff(ms,@useTime,getdate())
直接执行这个T-SQL脚本,在数据库表没有索引的情况下,耗费的时间是:
T-SQL Execute Times(ms):
58796
根据这个功能,写了一个.net控制台程序来测试,测试程序没有使用任何数据访问框架,直接使用ADO.NET,下面是多线程测试的代码,其它代码略:
public static void Test2(string connName1,string connName2)
{
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
watch.Start();
string allJjdmList = "";
string connString = getConnectionString();
//SqlConnection conn = new SqlConnection(connString);
//conn.Open();
string sql = "select jjdm from [FundYield] group by jjdm order by jjdm asc";
DataSet ds = getData(connString, sql);
int allCount = ds.Tables[0].Rows.Count;
int p = (int)(allCount * 0.5);
System.Threading.Thread t1=new System.Threading.Thread (new System.Threading.ParameterizedThreadStart (tp1=>
{
for (int i = 0; i < p; i++)
{
string jjdm = ds.Tables[0].Rows[i][0].ToString();
object result = getSclar(ConfigurationManager.ConnectionStrings[connName1].ConnectionString,
string.Format("select MAX(fsrq) from [FundYield] where jjdm='{0}'", jjdm));
if (result != DBNull.Value)
{
DateTime dt = Convert.ToDateTime(result);
//Console.WriteLine("Thread 2 No {0} ,jjdm[{1}] last FSRQ is:{2}", i, jjdm, dt);
}
allJjdmList = allJjdmList + "," + jjdm;
}
Console.WriteLine("Tread 1 used all time is(ms):{0}", watch.ElapsedMilliseconds);
}
));
System.Threading.Thread t2 = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(tp2 =>
{
for (int i = p; i < allCount; i++)
{
string jjdm = ds.Tables[0].Rows[i][0].ToString();
//这里不论使用default还是express,区别不大
object result = getSclar(ConfigurationManager.ConnectionStrings[connName2].ConnectionString,
string.Format("select MAX(fsrq) from [FundYield] where jjdm='{0}'", jjdm));
if (result != DBNull.Value)
{
DateTime dt = Convert.ToDateTime(result);
//Console.WriteLine("Thread 2 No {0} ,jjdm[{1}] last FSRQ is:{2}", i, jjdm, dt);
}
allJjdmList = allJjdmList + "," + jjdm;
}
Console.WriteLine("Tread 2 used all time is(ms):{0}", watch.ElapsedMilliseconds);
}
));
t1.Start();
t2.Start();
t1.Join();
t2.Join();
Console.WriteLine("====All thread completed!========");
}
下面是测试结果:
第一次,数据库没有创建索引,进行全表扫描:
------单数据库,单线程测试---------
used all time is(ms):59916
------同一实例,双数据库,单线程测试---------
used all time is(ms):59150
------同一实例,双数据库,多线程测试---------
Tread 2 used all time is(ms):51223
Tread 1 used all time is(ms):58175
====All thread completed!========
------双实例,双数据库,单线程测试---------
used all time is(ms):58230
------双实例,双数据库,多线程测试---------
Tread 2 used all time is(ms):52705
Tread 1 used all time is(ms):58293
====All thread completed!========
第二次,数据库响应的字段创建索引,下面是测试结果:
------单数据库,单线程测试---------
used all time is(ms):1721
------同一实例,双数据库,单线程测试---------
used all time is(ms):1737
------同一实例,双数据库,多线程测试---------
Tread 2 used all time is(ms):1684
Tread 1 used all time is(ms):1714
====All thread completed!========
------双实例,双数据库,单线程测试---------
used all time is(ms):1874
------单数据库,单线程测试---------
used all time is(ms):1699
------同一实例,双数据库,单线程测试---------
used all time is(ms):1754
------同一实例,双数据库,多线程测试---------
Tread 1 used all time is(ms):1043
Tread 2 used all time is(ms):1103
====All thread completed!========
------双实例,双数据库,单线程测试---------
used all time is(ms):1838
------双实例,双数据库,多线程测试---------
Tread 1 used all time is(ms):1072
Tread 2 used all time is(ms):1139
====All thread completed!========
测试结论:
综合全表扫描访问和有索引方式的访问,
单线程访问:
在同一个数据库实例上,双数据库没有体现出优势,甚至单数据库稍微优胜于多数据库;
在两个数据库实例上,双实例双实例要落后于单实例单数据库;
多线程访问:
双数据库实例稍微落后于单数据库实例;
综合结论,看来不论是双数据库还是双实例,对比与单实例或者单数据库,都没有体现出优势,看来前者的优势不在于访问效率,一位朋友说,数据库实例是不同的服务,控制粒度更小,维护影响比较低。但我想,双数据库实例,双数据库,多核CPU,应该跟两台数据库服务器差不多的性能吧,怎么没有体现优势呢?也许是我的测试机器仅仅有一个磁盘,这里磁盘IO成了瓶颈。
这个测试有没有意义,或者这个结果的原因,还请大牛们多多指教!
--------------------------------------------------------------
意外发现:
1,有人说频繁的查询在完全数据库中进行效率最高,测试发现,在查询分析器上直接运行上面的那个T-SQL脚本,跟程序从数据库取出数据,再加工计算查询,效率上没有明显的区别,所以哪些支持“将复杂的业务逻辑写在存储过程中效率最高的观点是站不住脚的!” ,ADO.NET从数据库来回操作数据一样有效率,如果加上复杂的字符函数计算和大批量的循环操作,存储过程的效率不一定高。
2,在使用程序进行频繁的数据库操作的时候,使用一个连接对象还是在每个方法中使用新的连接对象,一直是很纠结的问题,心想频繁的数据操作还是用一个连接对象快吧?在本文给出的测试代码中,有下列语句:
//SqlConnection conn = new SqlConnection(connString);
//conn.Open();
注释掉这些语句,在被调用的方法中使用自己的连接对象,与取消注释,全部使用一个连接对象,效率上没有任何区别!
究其原因,可能是ADO.NET自动使用了连接池,实际上程序在不同的情况下,使用的都是一个连接,所以操作上效率没有区别。
后续测试
在真正的服务器上进行测试,发现测试结论又不一样,我们有服务器A,拥有16个核,32G内存,另外一台服务器B,拥有8个核,16G内存。在服务器A上有一个SqlServer实例,两个一样的数据库;在在服务器B上有一个SqlServer实例,一个数据库,下面是测试结果:
------单数据库,单线程测试---------
used all time is(ms):650
------同一实例,双数据库,单线程测试---------
used all time is(ms):418
------同一实例,双数据库,多线程测试---------
Tread 2 used all time is(ms):221
Tread 1 used all time is(ms):223
====All thread completed!========
------双实例,双数据库,单线程测试---------
used all time is(ms):1283
------双实例,双数据库,多线程测试---------
Tread 1 used all time is(ms):228
Tread 2 used all time is(ms):542
====All thread completed!========
可以看到,同一实例,多数据库,还是有明显的优势,而多线程优势更大;由于两台服务器性能差距较大,双实例测试没有显示出优势,但多线程下还是比单实例单数据库好!
为什么PC机跟服务器测试的结论不一致?也许还是跟计算能力相关,PC机的计算负载太大,已经失去了测试的意义。
测试环境:
CPU:Inter Core2 Quad,Q8300,2.50GHz;
内存:4.00GB
系统:Windows 7 32位系统
数据库系统:SqlServer 2008,有两个实例,一个是默认实例,一个是命名实例QE2
测试数据:
67万真实的基金收益数据,将这个表的数据放到了3个数据库中,详细内容见下面的连接字符串配置:
<add name ="Ins1_DB1" connectionString ="Data Source=.;Initial Catalog=TestDB;Integrated Security=True"/>
<add name ="Ins1_DB2" connectionString ="Data Source=.;Initial Catalog=LocalDB;Integrated Security=True"/>
<add name ="Ins2_DB" connectionString ="Data Source=.\QE2;Initial Catalog=TestDB;Integrated Security=True"/>
测试内容:
首先筛选出表中所有的基金代码,然后统计每只基金的最新收益率日期,对应的T-SQL代码如下:
declare @max_fsrq datetime
declare @currJJDM varchar(10)
declare @temp table (jjdm2 varchar(10))
declare @useTime datetime
set @useTime =GETDATE ();
insert into @temp(jjdm2)
select jjdm from [FundYield] group by jjdm order by jjdm asc
while EXISTS (select jjdm2 from @temp)
begin
set @currJJDM=(select top 1 jjdm2 from @temp)
select @max_fsrq = MAX(fsrq) from [FundYield] where jjdm=@currJJDM
delete from @temp where jjdm2 =@currJJDM
print @max_fsrq
end
print 'T-SQL Execute Times(ms):'
print datediff(ms,@useTime,getdate())
直接执行这个T-SQL脚本,在数据库表没有索引的情况下,耗费的时间是:
T-SQL Execute Times(ms):
58796
根据这个功能,写了一个.net控制台程序来测试,测试程序没有使用任何数据访问框架,直接使用ADO.NET,下面是多线程测试的代码,其它代码略:
public static void Test2(string connName1,string connName2)
{
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
watch.Start();
string allJjdmList = "";
string connString = getConnectionString();
//SqlConnection conn = new SqlConnection(connString);
//conn.Open();
string sql = "select jjdm from [FundYield] group by jjdm order by jjdm asc";
DataSet ds = getData(connString, sql);
int allCount = ds.Tables[0].Rows.Count;
int p = (int)(allCount * 0.5);
System.Threading.Thread t1=new System.Threading.Thread (new System.Threading.ParameterizedThreadStart (tp1=>
{
for (int i = 0; i < p; i++)
{
string jjdm = ds.Tables[0].Rows[i][0].ToString();
object result = getSclar(ConfigurationManager.ConnectionStrings[connName1].ConnectionString,
string.Format("select MAX(fsrq) from [FundYield] where jjdm='{0}'", jjdm));
if (result != DBNull.Value)
{
DateTime dt = Convert.ToDateTime(result);
//Console.WriteLine("Thread 2 No {0} ,jjdm[{1}] last FSRQ is:{2}", i, jjdm, dt);
}
allJjdmList = allJjdmList + "," + jjdm;
}
Console.WriteLine("Tread 1 used all time is(ms):{0}", watch.ElapsedMilliseconds);
}
));
System.Threading.Thread t2 = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(tp2 =>
{
for (int i = p; i < allCount; i++)
{
string jjdm = ds.Tables[0].Rows[i][0].ToString();
//这里不论使用default还是express,区别不大
object result = getSclar(ConfigurationManager.ConnectionStrings[connName2].ConnectionString,
string.Format("select MAX(fsrq) from [FundYield] where jjdm='{0}'", jjdm));
if (result != DBNull.Value)
{
DateTime dt = Convert.ToDateTime(result);
//Console.WriteLine("Thread 2 No {0} ,jjdm[{1}] last FSRQ is:{2}", i, jjdm, dt);
}
allJjdmList = allJjdmList + "," + jjdm;
}
Console.WriteLine("Tread 2 used all time is(ms):{0}", watch.ElapsedMilliseconds);
}
));
t1.Start();
t2.Start();
t1.Join();
t2.Join();
Console.WriteLine("====All thread completed!========");
}
下面是测试结果:
第一次,数据库没有创建索引,进行全表扫描:
------单数据库,单线程测试---------
used all time is(ms):59916
------同一实例,双数据库,单线程测试---------
used all time is(ms):59150
------同一实例,双数据库,多线程测试---------
Tread 2 used all time is(ms):51223
Tread 1 used all time is(ms):58175
====All thread completed!========
------双实例,双数据库,单线程测试---------
used all time is(ms):58230
------双实例,双数据库,多线程测试---------
Tread 2 used all time is(ms):52705
Tread 1 used all time is(ms):58293
====All thread completed!========
第二次,数据库响应的字段创建索引,下面是测试结果:
------单数据库,单线程测试---------
used all time is(ms):1721
------同一实例,双数据库,单线程测试---------
used all time is(ms):1737
------同一实例,双数据库,多线程测试---------
Tread 2 used all time is(ms):1684
Tread 1 used all time is(ms):1714
====All thread completed!========
------双实例,双数据库,单线程测试---------
used all time is(ms):1874
------单数据库,单线程测试---------
used all time is(ms):1699
------同一实例,双数据库,单线程测试---------
used all time is(ms):1754
------同一实例,双数据库,多线程测试---------
Tread 1 used all time is(ms):1043
Tread 2 used all time is(ms):1103
====All thread completed!========
------双实例,双数据库,单线程测试---------
used all time is(ms):1838
------双实例,双数据库,多线程测试---------
Tread 1 used all time is(ms):1072
Tread 2 used all time is(ms):1139
====All thread completed!========
测试结论:
综合全表扫描访问和有索引方式的访问,
单线程访问:
在同一个数据库实例上,双数据库没有体现出优势,甚至单数据库稍微优胜于多数据库;
在两个数据库实例上,双实例双实例要落后于单实例单数据库;
多线程访问:
双数据库实例稍微落后于单数据库实例;
综合结论,看来不论是双数据库还是双实例,对比与单实例或者单数据库,都没有体现出优势,看来前者的优势不在于访问效率,一位朋友说,数据库实例是不同的服务,控制粒度更小,维护影响比较低。但我想,双数据库实例,双数据库,多核CPU,应该跟两台数据库服务器差不多的性能吧,怎么没有体现优势呢?也许是我的测试机器仅仅有一个磁盘,这里磁盘IO成了瓶颈。
这个测试有没有意义,或者这个结果的原因,还请大牛们多多指教!
--------------------------------------------------------------
意外发现:
1,有人说频繁的查询在完全数据库中进行效率最高,测试发现,在查询分析器上直接运行上面的那个T-SQL脚本,跟程序从数据库取出数据,再加工计算查询,效率上没有明显的区别,所以哪些支持“将复杂的业务逻辑写在存储过程中效率最高的观点是站不住脚的!” ,ADO.NET从数据库来回操作数据一样有效率,如果加上复杂的字符函数计算和大批量的循环操作,存储过程的效率不一定高。
2,在使用程序进行频繁的数据库操作的时候,使用一个连接对象还是在每个方法中使用新的连接对象,一直是很纠结的问题,心想频繁的数据操作还是用一个连接对象快吧?在本文给出的测试代码中,有下列语句:
//SqlConnection conn = new SqlConnection(connString);
//conn.Open();
注释掉这些语句,在被调用的方法中使用自己的连接对象,与取消注释,全部使用一个连接对象,效率上没有任何区别!
究其原因,可能是ADO.NET自动使用了连接池,实际上程序在不同的情况下,使用的都是一个连接,所以操作上效率没有区别。
后续测试
在真正的服务器上进行测试,发现测试结论又不一样,我们有服务器A,拥有16个核,32G内存,另外一台服务器B,拥有8个核,16G内存。在服务器A上有一个SqlServer实例,两个一样的数据库;在在服务器B上有一个SqlServer实例,一个数据库,下面是测试结果:
------单数据库,单线程测试---------
used all time is(ms):650
------同一实例,双数据库,单线程测试---------
used all time is(ms):418
------同一实例,双数据库,多线程测试---------
Tread 2 used all time is(ms):221
Tread 1 used all time is(ms):223
====All thread completed!========
------双实例,双数据库,单线程测试---------
used all time is(ms):1283
------双实例,双数据库,多线程测试---------
Tread 1 used all time is(ms):228
Tread 2 used all time is(ms):542
====All thread completed!========
可以看到,同一实例,多数据库,还是有明显的优势,而多线程优势更大;由于两台服务器性能差距较大,双实例测试没有显示出优势,但多线程下还是比单实例单数据库好!
为什么PC机跟服务器测试的结论不一致?也许还是跟计算能力相关,PC机的计算负载太大,已经失去了测试的意义。
相关文章推荐
- 数据库查询效率,百万数据测试
- 通过此方法插入不同数据库数据用于测试
- 精华的微软文章".NET 数据访问架构指南",特别是数据库连接的测试.即监视链接池化(1)
- 精华的微软文章".NET 数据访问架构指南",特别是数据库连接的测试.即监视链接池化 (2)
- 敏感数据加密保护和数据库访问方式的测试内容
- C#:30行数据插入到数据库中的效率测试-一行行执行、构造SQL一次执行、SqlBulkCopy
- 精华的微软文章".NET 数据访问架构指南",特别是数据库连接的测试.即监视链接池化 (2)
- 精华的微软文章".NET 数据访问架构指南",特别是数据库连接的测试.即监视链接池化(1)
- 数据库名与实例名不同情况下的静态监听配置
- 1.oracle dblink(数据库不同实例数据对导)
- .net 访问不同数据库之配置参数和参数数据类型
- 测试环境运行正常的SQL到生产上奇慢无比,最终导致UI访问超时;确定SQL效率无问题,那么就极有可能使生产环境的表数据量较大而且没有做分析。
- 记单表数据较多的数据库查询实例及测试结果
- 大量数据情况下单线程插入和多线程insert数据库的性能测试
- [转]SQLSERVER存储过程调用不同数据库的数据_存储过程中通过链接服务器访问远程服务器
- 初识vb数据库凯发之实例3(使用数据绑定访问数据)
- 数据库中的实例名和数据库名有什么不同?
- java中数据库访问方式的不同处理数据总结:
- 转自:http://m.blog.csdn.net/article/details?id=6554168 在使用order by语句进行查询结果排序时,不同的数据库对于被排序字段数据行为null的情况
- 怎么打开OPENDATASOURCE权限(不同服务器数据库之间的数据操作) -*-- 在T-SQL语句中访问远程数据库(openrowset/opendatasource/openquery) --- OpenDataSource的用法