I have a bit of code which pulls in a HTML page from a local SQLite database file in .NET Core 2.0.
This code works fine when exicuted in debug mode, but runs extremely slow in production after I have published the app.
I used a Stopwatch to diagnose which piece of code is causing the problem and found that connection.QueryFirstOrDefault takes 2ms to find a single row in debug mode, however the same task takes 1.4 seconds after I have published the app. That's about 700 times slower.
//initialize connection
var connection = new SqliteConnection("Data Source=" + dbName);
// Build SQL String
string query = @"SELECT *
FROM HtmlItems
WHERE PostID = 1;
// Start Timer
var watch = System.Diagnostics.Stopwatch.StartNew();
Submit query
HtmlItem = connection.QueryFirstOrDefault<HtmlItem>(query);
// End Timer
watch.Stop();
var result = watch.ElapsedMilliseconds();
The query maps to an object looking like this
public class HtmlItem
{
public int PostID { get; set; }
public string PostTitle { get; set; }
public string PostDescription { get; set; }
public int PostDate { get; set; } // Unix Timestamp
public int Hidden { get; set; }
public string Url { get; set; }
public string PostHTML { get; set; }
}
The same database file is used in debug and production, this database file only has three rows. My app should be the only thing trying to access the file.
Indexing the SQLite database file dosen't seem to make any speed improvement.
I'm wondering how I can identify what's causing my connection to the database to be 700x slower in production.
SELECT *