.NET开发

本类阅读TOP10

·NHibernate快速指南(翻译)
·vs.net 2005中文版下载地址收藏
·【小技巧】一个判断session是否过期的小技巧
·VB/ASP 调用 SQL Server 的存储过程
·?dos下编译.net程序找不到csc.exe文件
·通过Web Services上传和下载文件
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·VB.NET实现DirectDraw9 (2) 动画
·VB.NET实现DirectDraw9 (1) 托管的DDraw
·建站框架规范书之——文件命名

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
数据库访问与本地文件访问实测报告

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

一直认为数据库访问比本地文件访问是要慢很多的,但究竟慢多少却不清楚。终于忍不住作了下比较测试。

测试环境:OS---Windows 2000 Server        DatabaseServer----SQLServer 2000        程序语言:CSharp

测试方式:从数据库中读取参数表/从XML中读取配置参数等同数量的记录并实例化对象。

测试程序如下:

 using System;
 using System.Globalization;
 using System.IO;
 using System.Xml.Serialization;
 using System.Xml;
 using System.Data;
 using System.Data.SqlClient;

 public class Demo
 {
  public static void Main()
  {
   System.Console.WriteLine("===== ExecuteXML() =====================================");
   System.Console.WriteLine(ExecuteXML());
   System.Console.WriteLine(ExecuteXML());
   System.Console.WriteLine(ExecuteXML());
   System.Console.WriteLine(ExecuteXML());
   System.Console.WriteLine(ExecuteXML());
   System.Console.WriteLine(ExecuteXML());
   System.Console.WriteLine(ExecuteXML());
   System.Console.WriteLine(ExecuteXML());
   System.Console.WriteLine(ExecuteXML());
   System.Console.WriteLine(ExecuteXML());
   System.Console.WriteLine("========================================================");
   
   System.Console.WriteLine("===== ExecuteSQL() =====================================");
   System.Console.WriteLine(ExecuteSQL());
   System.Console.WriteLine(ExecuteSQL());
   System.Console.WriteLine(ExecuteSQL());
   System.Console.WriteLine(ExecuteSQL());
   System.Console.WriteLine(ExecuteSQL());
   System.Console.WriteLine(ExecuteSQL());
   System.Console.WriteLine(ExecuteSQL());
   System.Console.WriteLine(ExecuteSQL());
   System.Console.WriteLine(ExecuteSQL());
   System.Console.WriteLine(ExecuteSQL());
   System.Console.WriteLine("========================================================\n");

  }

  static double ExecuteSQL()
  {
   string _connString = "server=data-server;user id=sa;password=1234;database=LeadDB;min pool size=4;max pool size=4;packet size=3072";
   SqlCommand cmd = new SqlCommand();
   SqlConnection conn = new SqlConnection(_connString);

   double begin = CultureInfo.InvariantCulture.Calendar.GetMilliseconds(DateTime.Now);
   try {
    conn.Open();

    cmd.Connection = conn;
    cmd.CommandText = "SELECT [CategoryId], [Name] FROM [SYS_Category] WHERE [CategoryType] = 2";

    cmd.CommandType = CommandType.Text;
    SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
    while (rdr.Read())
    {
     new Entity(rdr.GetInt32(0), rdr.GetString(1));
    }
    rdr.Close();
   }catch {
    conn.Close();
    throw;
   }
   begin = CultureInfo.InvariantCulture.Calendar.GetMilliseconds(DateTime.Now) - begin;
   return (begin >= 0)?begin:begin+1000.0;
  }

  static double ExecuteXML()
  {
   double begin = CultureInfo.InvariantCulture.Calendar.GetMilliseconds(DateTime.Now);
   XmlDocument xmlDoc = new XmlDocument();
   xmlDoc.Load( @"F:\LeadBridge\XCERP\Web\OnlineService\SYS_Category_Headship.xml" );
   
   foreach(XmlNode node in xmlDoc.SelectNodes("items/item")){
    new Entity(int.Parse(node.Attributes["id"].Value), node.InnerText);
   }
   begin = CultureInfo.InvariantCulture.Calendar.GetMilliseconds(DateTime.Now) - begin;
   return (begin >= 0)?begin:begin+1000.0;
  }
 }

 public class Entity {
  public Entity(int age, string name){
   this._Age = age;
   this._Name = name;
  }
  private int _Age;
  private string _Name;
  public int Age{
   get{ return _Age; }
   set{ _Age = value; }
  }
  public string Name{
   get{ return _Name; }
   set{ _Name = value; }
  }
 }

执行结果如下:

===== ExecuteXML() =====================================
47
0
0
16
0
0
0
0
16
0
========================================================
===== ExecuteSQL() =====================================
203
0
0
0
0
0
16
0
0
0
========================================================

做过多次测试,基本上是如上数量级别(一个数量级)的差异(第一次执行差异),因此建议,大家对于无需经常修改的数据保存为本地参数文件形式比直接从数库读取效率要高一些。




相关文章

相关软件