往返读取后台数据的代价

发表于:2012-5-25 09:58

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:潘文佳 译    来源:51Testing软件测试网采编

  数据库最重要是的为前台应用服务。 在众多决定应用性能的因素中, 如何快速有效从后台读取数据很大程度上地影响到最终效果。本文将对不同的数据往返(round-trip)读取进行比较和归纳总结。最后的结果非常出人意料。往往在时间紧迫的情况下,我们会本能地使用最简单的方法来完成任务,但是这种编译习惯会让我们的前台应用的性能大打折扣。

  返回 15,000 条数据:这个测试会从一个表格里面读取15000条数据。我们通过用三种不同的编译方式来看如何提高数据库提取的效率。

  以下这个脚本用来创建表格然后放入一百万条数据。因为我们需要足够多的数据来完成3个测试,每个测试读取新鲜的数据,所以创建了一百万条。我创建的这个列表每15000条数据一小组,这样确保了测试读取15000条数据的准确性。不会因为数据的不同,而影响测试的结果。

  这个脚本稍作修改就可以放在MS SQL服务器上跑:

createtabletest000 (
    intpkintprimarykey
   ,fillerchar(40)
)
  
--  BLOCK 1, first 5000 rows
--  pgAdmin3: run as pgScript
--  All others: modify as required
--
declare@x,@y;
set@x = 1;
set@y = string(40,40,1);
while @x <= 5000begin
    insertintotest000 (intpk,filler)
    values((@x-1)*200 +1,'@y');
  
    set@x = @x + 1;
end
  
-- BLOCK 2, put 5000 rows aside
--
select *intotest000_tempfromtest000
  
-- BLOCK 3, Insert the 5000 rows 199 more
--          times to get 1million altogether
--  pgAdmin3: run as pgScript
--  All others: modify as required
--
declare@x;
set@x = 1;
while @x <= 199begin
    insertintotest000 (intpk,filler)
    selectintpk+@x,fillerfromtest000_temp;
  
    set@x = @x + 1;
end

  测试-:基本代码

  最简单的代码就是通过一个直白的查询语句跑15000次往返。

# Make adatabaseconnection
$dbConn = pg_connect("dbname=roundTrips user=postgres");
  
# Program 1, Individual explicit fetches
$x1 = rand(0,199)*5000 + 1;
$x2 = $x1 + 14999;
echo"\nTest 1, using $x1 to $x2";
$timeBegin = microtime(true);
while ($x1++ <= $x2) {
    $dbResult = pg_exec("select * from test000 where intpk=$x1");
    $row = pg_fetch_array($dbResult);
}
$elapsed = microtime(true)-$timeBegin;
echo"\nTest 1, elapsed time: ".$elapsed;
echo"\n";

21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号