uwekeim

Performance MDB vs SQLite vs MS SQL CE vs MS SQL vs VistaDB

Feb 5th, 2016
583
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.35 KB | None | 0 0
  1. #define KEEP_CONNECTION_OPEN
  2.  
  3. namespace PerformanceMdbVsSqlCe
  4. {
  5.     using System;
  6.     using System.Data;
  7.     using System.Data.OleDb;
  8.     using System.Data.SqlClient;
  9.     using System.Data.SqlServerCe;
  10.     using System.Data.SQLite;
  11.     using System.Diagnostics;
  12.     using VistaDB.Provider;
  13.  
  14.     /*
  15.     Performance comparisons (maybe somewhat naive) between:
  16.  
  17.         - SQL Server Express 2014 on the same local PC and local SSD drive as the test application.
  18.         - SQL Server Express 2014 on a server in a gigabit LAN.
  19.         - SQL Server Compact (CE) on the same local PC and local SSD drive as the test application.
  20.         - Microsoft Access MDB/Jet on the same local PC and local SSD drive as the test application.
  21.         - Microsoft SQLite on the same local PC and local SSD drive as the test application.
  22.         - Microsoft VistaDB 5 on the same local PC and local SSD drive as the test application.
  23.  
  24.     See also:
  25.  
  26.         - http://pastebin.com/edit/NNJni52W - This file on Pastebin.
  27.         - http://stackoverflow.com/a/35225563/107625 - My question/answer on Stack Overflow.
  28.         - http://entwicklergate.de/t/125 - Posting in my German developer community.
  29.  
  30.         - http://uwe.co - My personal weblog :-)
  31.     */
  32.  
  33.     internal static class Program
  34.     {
  35.         private const string Folder = @"C:\Users\ukeim\Dropbox\Beruf\Prog\PerformanceMdbVsSqlCe\PerformanceMdbVsSqlCe";
  36.  
  37.         private static readonly string SqlCeConnectionString = $@"DataSource=""{Folder}\test.sdf""";
  38.         private static readonly string SqlServerLocaleConnectionString = $@"Server=W8-UWEKEIM\SQLEXPRESS2014;Database=Test;Integrated Security=True;";
  39.         private static readonly string SqlServerNetworkConnectionString = $@"Server=ZETAS23\SQLEXPRESS2014;Database=Test;Integrated Security=True;";
  40.         private static readonly string VistaDBConnectionString = $@"Data Source={Folder}\test.vdb5";
  41.         private static readonly string SqliteConnectionString = $@"Data Source={Folder}\test.db";
  42.  
  43.         private static readonly string MdbConnectionString =
  44.             $@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""{Folder}\test.mdb""";
  45.  
  46.         private static int _counter;
  47.  
  48.         private static void Main()
  49.         {
  50. #if KEEP_CONNECTION_OPEN
  51.             connSqlCe.Open();
  52.             connSqlLocale.Open();
  53.             sqlNetworkConn.Open();
  54.             mdbConn.Open();
  55.             sqliteConn.Open();
  56.             vistaDBConn.Open();
  57. #endif
  58.             /*
  59.             File.Delete($@"{Folder}\test.sdf");
  60.  
  61.             createSqlCeDB();
  62.             executeSqlCe(
  63.                 @"CREATE TABLE [Tabelle1] (
  64.                     [ID] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
  65.                     Name1 NVARCHAR(255),
  66.                     Wert1 [int]
  67.                 )");
  68.             */
  69.  
  70.             //executeSqlite(
  71.             //    @"CREATE TABLE [Tabelle1] (
  72.             //        [ID] INTEGER PRIMARY KEY AUTOINCREMENT,
  73.             //        Name1 NVARCHAR(255),
  74.             //        Wert1 [int]
  75.             //    )");
  76.  
  77.             test1(1, @"DELETE FROM Tabelle1");
  78.  
  79.             test1(1, @"INSERT INTO Tabelle1 (Name1, Wert1) VALUES ({LOOPCTR}, '{LOOPCTR}')");
  80.             test1(10, @"INSERT INTO Tabelle1 (Name1, Wert1) VALUES ({LOOPCTR}, '{LOOPCTR}')");
  81.             test1(100, @"INSERT INTO Tabelle1 (Name1, Wert1) VALUES ({LOOPCTR}, '{LOOPCTR}')");
  82.             test1(1000, @"INSERT INTO Tabelle1 (Name1, Wert1) VALUES ({LOOPCTR}, '{LOOPCTR}')");
  83.  
  84.             test1(1, @"SELECT * FROM Tabelle1");
  85.             test1(10, @"SELECT * FROM Tabelle1");
  86.             test1(100, @"SELECT * FROM Tabelle1");
  87.             test1(1000, @"SELECT * FROM Tabelle1");
  88.  
  89.             test1(1, @"SELECT a.* FROM Tabelle1 a LEFT JOIN Tabelle1 b ON a.ID=b.ID ORDER BY a.ID");
  90.             test1(10, @"SELECT a.* FROM Tabelle1 a LEFT JOIN Tabelle1 b ON a.ID=b.ID ORDER BY a.ID");
  91.             test1(100, @"SELECT a.* FROM Tabelle1 a LEFT JOIN Tabelle1 b ON a.ID=b.ID ORDER BY a.ID");
  92.             test1(1000, @"SELECT a.* FROM Tabelle1 a LEFT JOIN Tabelle1 b ON a.ID=b.ID ORDER BY a.ID");
  93.  
  94.             test1(1,
  95.                 @"SELECT a.* FROM Tabelle1 a LEFT JOIN Tabelle1 b ON a.ID=b.ID WHERE a.ID < 100 OR a.ID > 300 ORDER BY a.ID");
  96.             test1(10,
  97.                 @"SELECT a.* FROM Tabelle1 a LEFT JOIN Tabelle1 b ON a.ID=b.ID WHERE a.ID < 100 OR a.ID > 300 ORDER BY a.ID");
  98.             test1(100,
  99.                 @"SELECT a.* FROM Tabelle1 a LEFT JOIN Tabelle1 b ON a.ID=b.ID WHERE a.ID < 100 OR a.ID > 300 ORDER BY a.ID");
  100.             test1(1000,
  101.                 @"SELECT a.* FROM Tabelle1 a LEFT JOIN Tabelle1 b ON a.ID=b.ID WHERE a.ID < 100 OR a.ID > 300 ORDER BY a.ID");
  102.         }
  103.  
  104.         private static void test1(int rowCount, string sql)
  105.         {
  106.             var sw = new Stopwatch();
  107.  
  108.             // --
  109.  
  110. #if KEEP_CONNECTION_OPEN
  111.             const string keep = @"keeping connection open";
  112. #else
  113.             const string keep = @"Closing connections";
  114. #endif
  115.  
  116.             Console.WriteLine(@"{0}.: {1} x {2} ({3}):", ++_counter, rowCount, sql, keep);
  117.  
  118.             // --
  119.  
  120.             sw.Start();
  121.             for (var i = 0; i < rowCount; i++)
  122.             {
  123.                 executeSqlServerLocale(sql.Replace(@"{LOOPCTR}", i.ToString()));
  124.             }
  125.             sw.Stop();
  126.  
  127.             Console.WriteLine(@"- SQL Express local : {0}", sw.Elapsed);
  128.  
  129.             // --
  130.  
  131.             sw.Start();
  132.             for (var i = 0; i < rowCount; i++)
  133.             {
  134.                 executeSqlServerNetwork(sql.Replace(@"{LOOPCTR}", i.ToString()));
  135.             }
  136.             sw.Stop();
  137.  
  138.             Console.WriteLine(@"- SQL Express remote: {0}", sw.Elapsed);
  139.  
  140.             // --
  141.  
  142.             sw.Start();
  143.             for (var i = 0; i < rowCount; i++)
  144.             {
  145.                 executeSqlCe(sql.Replace(@"{LOOPCTR}", i.ToString()));
  146.             }
  147.             sw.Stop();
  148.  
  149.             Console.WriteLine(@"- SQL CE            : {0}", sw.Elapsed);
  150.  
  151.             // --
  152.  
  153.             sw.Start();
  154.             for (var i = 0; i < rowCount; i++)
  155.             {
  156.                 executeMdb(sql.Replace(@"{LOOPCTR}", i.ToString()));
  157.             }
  158.             sw.Stop();
  159.  
  160.             Console.WriteLine(@"- MS Access         : {0}", sw.Elapsed);
  161.  
  162.             // --
  163.  
  164.             sw.Start();
  165.             for (var i = 0; i < rowCount; i++)
  166.             {
  167.                 executeSqlite(sql.Replace(@"{LOOPCTR}", i.ToString()));
  168.             }
  169.             sw.Stop();
  170.  
  171.             Console.WriteLine(@"- SQLite            : {0}", sw.Elapsed);
  172.  
  173.             // --
  174.  
  175.             sw.Start();
  176.             for (var i = 0; i < rowCount; i++)
  177.             {
  178.                 executeVistaDB(sql.Replace(@"{LOOPCTR}", i.ToString()));
  179.             }
  180.             sw.Stop();
  181.  
  182.             Console.WriteLine(@"- VistaDB           : {0}", sw.Elapsed);
  183.  
  184.             // --
  185.  
  186.             Console.WriteLine();
  187.         }
  188.  
  189.         private static void createSqlCeDB()
  190.         {
  191.             var en = new SqlCeEngine(SqlCeConnectionString);
  192.             en.CreateDatabase();
  193.         }
  194.  
  195. #if KEEP_CONNECTION_OPEN
  196.         private static readonly SqlCeConnection connSqlCe = new SqlCeConnection(SqlCeConnectionString);
  197. #endif
  198.  
  199.         private static DataTable executeSqlCe(string sql)
  200.         {
  201. #if !KEEP_CONNECTION_OPEN
  202.             using (var connSqlCe = new SqlCeConnection(SqlCeConnectionString))
  203. #endif
  204.             using (var da = new SqlCeDataAdapter(sql, connSqlCe))
  205.             {
  206.                 var table = new DataTable();
  207.                 da.Fill(table);
  208.  
  209.                 return table;
  210.             }
  211.         }
  212.  
  213. #if KEEP_CONNECTION_OPEN
  214.         private static readonly SqlConnection connSqlLocale = new SqlConnection(SqlServerLocaleConnectionString);
  215. #endif
  216.  
  217.         private static DataTable executeSqlServerLocale(string sql)
  218.         {
  219. #if !KEEP_CONNECTION_OPEN
  220.             using (var connSqlLocale = new SqlConnection(SqlServerLocaleConnectionString))
  221. #endif
  222.             using (var da = new SqlDataAdapter(sql, connSqlLocale))
  223.             {
  224.                 var table = new DataTable();
  225.                 da.Fill(table);
  226.  
  227.                 return table;
  228.             }
  229.         }
  230.  
  231. #if KEEP_CONNECTION_OPEN
  232.         private static readonly SqlConnection sqlNetworkConn = new SqlConnection(SqlServerNetworkConnectionString);
  233. #endif
  234.  
  235.         private static DataTable executeSqlServerNetwork(string sql)
  236.         {
  237. #if !KEEP_CONNECTION_OPEN
  238.             using (var sqlNetworkConn = new SqlConnection(SqlServerNetworkConnectionString))
  239. #endif
  240.             using (var da = new SqlDataAdapter(sql, sqlNetworkConn))
  241.             {
  242.                 var table = new DataTable();
  243.                 da.Fill(table);
  244.  
  245.                 return table;
  246.             }
  247.         }
  248.  
  249. #if KEEP_CONNECTION_OPEN
  250.         private static readonly VistaDBConnection vistaDBConn = new VistaDBConnection(VistaDBConnectionString);
  251. #endif
  252.  
  253.         private static DataTable executeVistaDB(string sql)
  254.         {
  255. #if !KEEP_CONNECTION_OPEN
  256.             using (var vistaDBConn = new VistaDBConnection(VistaDBConnectionString))
  257. #endif
  258.             using (var da = new VistaDBDataAdapter(sql, vistaDBConn))
  259.             {
  260.                 var table = new DataTable();
  261.                 da.Fill(table);
  262.  
  263.                 return table;
  264.             }
  265.         }
  266.  
  267. #if KEEP_CONNECTION_OPEN
  268.         private static readonly OleDbConnection mdbConn = new OleDbConnection(MdbConnectionString);
  269. #endif
  270.  
  271.         private static DataTable executeMdb(string sql)
  272.         {
  273. #if !KEEP_CONNECTION_OPEN
  274.             using (var mdbConn = new OleDbConnection(MdbConnectionString))
  275. #endif
  276.             using (var da = new OleDbDataAdapter(sql, mdbConn))
  277.             {
  278.                 var table = new DataTable();
  279.                 da.Fill(table);
  280.  
  281.                 return table;
  282.             }
  283.         }
  284.  
  285. #if KEEP_CONNECTION_OPEN
  286.         private static readonly SQLiteConnection sqliteConn = new SQLiteConnection(SqliteConnectionString);
  287. #endif
  288.  
  289.         private static DataTable executeSqlite(string sql)
  290.         {
  291. #if !KEEP_CONNECTION_OPEN
  292.             using (var sqliteConn = new SQLiteConnection(SqliteConnectionString))
  293. #endif
  294.             using (var da = new SQLiteDataAdapter(sql, sqliteConn))
  295.             {
  296.                 var table = new DataTable();
  297.                 da.Fill(table);
  298.  
  299.                 return table;
  300.             }
  301.         }
  302.     }
  303. }
Add Comment
Please, Sign In to add comment