Database access and local file Access test report

Source: Internet
Author: User
Tags datetime
Access to | data | The database has always thought that database access is much slower than local file access, but it's not clear how slow it is. Finally could not help to make a comparison test.

Test environment: OS---Windows server databaseserver----SQL Server 2000 program language: CSharp

Test method: Read the parameter table from the database/read the record of the equivalent number of configuration parameters from the XML and instantiate the object.

The test procedure is as follows:

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;}
}
}

The results of the implementation are as follows:

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

Has done many tests, basically is above the quantity level (one order of magnitude) the difference (the first execution difference), therefore proposed, everybody to save the data which does not need to modify frequently the local parameter file form is more efficient than the direct reading from the library.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.