資料庫訪問與本地檔案訪問實測報告

來源:互聯網
上載者:User
訪問|資料|資料庫 一直認為資料庫訪問比本地檔案訪問是要慢很多的,但究竟慢多少卻不清楚。終於忍不住作了下比較測試。

測試環境: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
========================================================

做過多次測試,基本上是如上數量層級(一個數量級)的差異(第一次執行差異),因此建議,大家對於無需經常修改的資料儲存為本地參數檔案形式比直接從數庫讀取效率要高一些。




相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。