Following on from my previous post Rich & I are developing an app which has 2 versions - one has only a single sqlite database but the other could use multiple sqlite databases to store the data - in effect the data is partitioned (sharded) alphabetically for the second version.
Our existing code used for the single database version uses a Simple ORM to do our data access. The primary reason being the code is 2 lines and importantly the performance cost of using the Simple ORM is not an issue at the moment. If it ever becomes an issue we'll switch it out.
What follows is how we approached attaching multiple databases together. Sqlite allows multiple databases to be used under the same connection. The databases don't have to share the same schema at all, all that is required to use the attached databases is to use the syntax database-name.table-name. More info can be found at the sqlite.org website here.
The first attempt was to do this from Vici CoolStorage, I didn't expect this to work because it only has the notion of setting the database you are about to access, I couldn't find anyway to attach a second database. The code is shown below and the runtime exception afterwards, as I said this was hopeful and failed.
Our existing code used for the single database version uses a Simple ORM to do our data access. The primary reason being the code is 2 lines and importantly the performance cost of using the Simple ORM is not an issue at the moment. If it ever becomes an issue we'll switch it out.
What follows is how we approached attaching multiple databases together. Sqlite allows multiple databases to be used under the same connection. The databases don't have to share the same schema at all, all that is required to use the attached databases is to use the syntax database-name.table-name. More info can be found at the sqlite.org website here.
The first attempt was to do this from Vici CoolStorage, I didn't expect this to work because it only has the notion of setting the database you are about to access, I couldn't find anyway to attach a second database. The code is shown below and the runtime exception afterwards, as I said this was hopeful and failed.
private void databaseAttach_Click(object sender, RoutedEventArgs e) { CSConfig.SetDB("database1.sql", SqliteOption.None); CSDatabase.ExecuteNonQuery("ATTACH 'database2.sql' AS db2;"); var results = CSDatabase.RunQuery<search_db_column_names>("SELECT db2.search_db_column_names.pk FROM db2.search_db_column_names"); Debug.WriteLine(results.ToString()); }
Blew up with the following exception:The second attempt was to use C# Sqlite For WP7 of codeplex. Vici CoolStorage is written on top of this so my thought were it would be less of an abstraction and therefore more likely to succeed. This time the code has more traditional DAL feel about - the use of connection, command & reader objects.
Success this worked!
The code is shown below and it's pretty much the same as for a single database call apart from the execute no query to attach the second database and the modified SQL statement.
As stated earlier the statement is now explicit about which columns and tables are being referenced:
"SELECT * FROM main.PostCodes UNION SELECT * FROM db2.PostCodes"
In this case all I'm doing is the union between 2 tables as they have the same structure and none over lapping data.
private IList<PostCode> ExecuteAllDatabase1() { var postcodes = new List<PostCode>(); using (var conn = new SqliteConnection("Version=3,uri=file:" + Database1)) { conn.Open(); using (var cmd = conn.CreateCommand()) { cmd.CommandText = "ATTACH '" + Database2 + "' AS db2;"; cmd.ExecuteNonQuery(); cmd.CommandText = "SELECT * FROM main.PostCodes UNION SELECT * FROM db2.PostCodes"; using (var reader = cmd.ExecuteReader(CommandBehavior.SingleResult)) { while (reader.Read()) { var postCode = new PostCode { Value = reader.GetString(1), District = reader.GetString(2), Latitude = reader.GetDouble(3), Longitude = reader.GetDouble(4), }; postcodes.Add(postCode); } } conn.Close(); } } return postcodes; }
Okay got it working but what about if I got as close to the metal as possible - C# Sqlite For WP7 is written using the csharp-sqlite project up on google code. Shown below is the same functionality written using the C style sqlite.org API.
private IList<PostCode> ExecuteAllDatabase2() { var postcodes = new List<PostCode>(); var database1 = new Sqlite3.sqlite3(); if (Sqlite3.sqlite3_open(Database1, ref database1) == Sqlite3.SQLITE_OK) { var attachSql = string.Format("ATTACH DATABASE '{0}' AS db2", Database2); var errorMessage = string.Empty; if (Sqlite3.sqlite3_exec(database1, attachSql, null, null, ref errorMessage) == Sqlite3.SQLITE_OK) { var selectStmt = new Sqlite3.Vdbe(); var selectSql = @"SELECT * FROM main.PostCodes UNION SELECT * FROM db2.PostCodes"; var stringTail = string.Empty; if (Sqlite3.sqlite3_prepare_v2(database1, selectSql, -1, ref selectStmt, ref stringTail) == Sqlite3.SQLITE_OK) { var n = 0; while (Sqlite3.sqlite3_step(selectStmt) == Sqlite3.SQLITE_ROW) { var col1 = Sqlite3.sqlite3_column_int(selectStmt, 0); var postCode = Sqlite3.sqlite3_column_text(selectStmt, 1); var district = Sqlite3.sqlite3_column_text(selectStmt, 2); var latitude = Sqlite3.sqlite3_column_double(selectStmt, 3); var longitude = Sqlite3.sqlite3_column_double(selectStmt, 4); postcodes.Add(new PostCode { District = district, Value = postCode, Latitude = latitude, Longitude = longitude}); } } } else {} } else {} Sqlite3.sqlite3_close(database1); return postcodes; }
The only thing left to do was compare the performance of the 2 working solutions. Interestingly it appears the C# Sqlite For WP7 gives better performance than the sqlite API.
C# Sqlite For WP7 average = 85 ms
sqlite API average = 95 ms
I suggest this is probably due to the fact I haven't tweaked the sqlite API code to be as per-formant as possible. Again I have ignored the first result for both implementations.
Comments
Post a Comment