1 pièce(s) jointe(s)
CommandType.StoredProcedure moins rapide que CommandType.Text. Où est l'erreur?
Salut,
Je suis un peu abasourdi, pour ne pas diresur le c**.
J'ai récupéré les source de ce site pour voir un peu de LINQ ou SQL celui qui est le plus performant. Mes test correspondent bien à ce que trouve l'auteur, jusque là ça va. Dans son code il utilise des requetes SQL de type CommandType.Text. Je me suis dit se serait interessant de voir ce que ça donne avec des procédures stockées. Mais là catastrophe! Les procédures stockées seraient moins rapides sur un simple SELECT qu'une commande text!!
Afin de vous permettre de vous faire votre propre jugement je vous laisse les sources. J'ai peut-être fait des erreurs...
- Telecharger et installer AdventureWorks
- Ajouter à la base la procédure stockée suivante:
Code:
1 2 3 4 5 6 7 8
| CREATE PROCEDURE uspSelectCustomers
AS
BEGIN
SELECT * FROM [AdventureWorks].[Sales].[Customer]
END
GO |
- Le programme à console executer:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| using System;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Xml.XPath;
namespace RetrieveUsingADO
{
class Program
{
private static string connectionstring = @"Server=NOM_MACHINE\MSSQL01;Database=AdventureWorks;user id=sa;password=;Connection timeout = 5 ;Pooling='false';";
static void Main(string[] args)
{
displaySettings();
Console.WriteLine("Executing GatherDataADORead");
GatherDataADORead();
} // end of main
# region GatherData
public static void GatherDataADORead()
{
using (DataSet dataSet = new DataSet())
{
using (DataTable dt = new DataTable("ADORead"))
{
dt.Columns.Add(new DataColumn("Pass", typeof(Int32)));
dt.Columns.Add(new DataColumn("SP", typeof(Int64)));
dt.Columns.Add(new DataColumn("TEXT", typeof(Int64)));
dataSet.Tables.Add(dt);
DataRow dr;
Stopwatch sw = new Stopwatch();
for (int i = 0; i <= 500; i++)
{
dr = dt.NewRow();
dr[0] = i;
sw.Start();
ADOReadingStoredProc();
dr[1] = ((long)(sw.ElapsedTicks / 100));
sw.Reset();
sw.Start();
ADOReadingText();
dr[2] = ((long)(sw.ElapsedTicks / 100));
sw.Reset();
dt.Rows.Add(dr);
} //end of for
}
dataSet.WriteXml(string.Format("c:\\temp\\{0}_ADORead.xml", DateTime.Now.ToString("yyyy-MM-ddThh-mm-ss")));
}
}
# endregion GatherData
# region ActualFunctions
/// <summary>
/// This function reads from customer table using ado and then sums up the values of the first column.
/// </summary>
private static void ADOReadingText()
{
using (SqlConnection Conn = new SqlConnection(connectionstring))
{
Conn.Open();
using (SqlCommand cmd = new SqlCommand("SELECT * FROM [AdventureWorks].[Sales].[Customer]", Conn))
{
cmd.CommandType = CommandType.Text;
double i = 0;
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
i = i + Convert.ToDouble(dr[0]);
}
}
}
}
}//end ADO Reading
private static void ADOReadingStoredProc()
{
using (SqlConnection Conn = new SqlConnection(connectionstring))
{
Conn.Open();
using (SqlCommand cmd = new SqlCommand("uspSelectCustomers", Conn))
{
cmd.CommandType = CommandType.StoredProcedure;
double i = 0;
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
i = i + Convert.ToDouble(dr[0]);
}
}
}
}
}
# endregion ActualFunctions
# region Helpers
private static void displaySettings()
{
if (Stopwatch.IsHighResolution)
{
Console.WriteLine("Operations timed using the system's high-resolution performance counter.");
}
else
{
Console.WriteLine("Operations timed using the DateTime class.");
}
long frequency = Stopwatch.Frequency;
Console.WriteLine(" Timer frequency in ticks per second = {0}",
frequency);
long nanosecPerTick = (1000L * 1000L * 1000L) / frequency;
Console.WriteLine(" Timer is accurate within {0} nanoseconds",
nanosecPerTick);
}
# endregion Helpers
}
} |
- Analyser avec Excel les fichiers Xml générés dans le repertoire temp. Attention l'unité de mesure du temps (Axe y) est en ElapsedTicks. Cela ne gène pas la comparaison.
D'avance merci de votre analyse.
A+