简体   繁体   中英

C#/ASP.NET Web Site using SQL Server CE Database Slow Query

The main issue I have is that a simple select from my SQL Server CE database has a typical wait time of 17 seconds for the first query and ~5 seconds for each subsequent query.

After letting the site sit idle for a couple minutes, the 17 second delay comes back. I am assuming the delay is from establishing connections, caching, etc, but I feel on our LAN this is a ridiculous wait and will only get worse for outside users.

The site is ac#/ASP.NET web site that is using a SQL Server Compact database (v4.0.8876.1). This is deployed to an IIS8 server. The site is intended to calculate values from user input using coefficients for different models that are stored in the database.

What am I doing wrong for there to be such a massive delay during the connection/queries?

Connection string:

<add name="DatabaseConnection" 
     connectionString="Data Source=|DataDirectory|\Database.sdf" 
     providerName="System.Data.SqlServerCe.4.0"/>

Query:

string connstr = ConfigurationManager.ConnectionStrings["DatabaseConnection"].ConnectionString;

using (SqlCeConnection conn = new SqlCeConnection(connstr))
{
    string queryString = @"Select id, description, imagePath
                           from ConservationModels
                           where model = @modelName";

    SqlCeCommand command = new SqlCeCommand(queryString, conn);
    command.Parameters.AddWithValue("@modelName", modelName);

    try
    {
        conn.Open();

        SqlCeDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {
            modelID = (int)reader[0];
            description = (string)reader[1];
            imagePath = (string)reader[2];
        }

        reader.Close();
     }
     catch (Exception ex)
     {
        throw;
     }
 }

Apologies for any missing information and I appreciate any suggestions. If there is a better way to achieve the goal for a web site, I am all ears.

This may be belated, but I recent found out (the hard way), that if you use SQL Server CE with ASP.NET on OWIN hosted on IIS, IIS will recompile or build a new app domain on every request. No idea why this happens, but the time you're seeing is likely to be ASP.NET rebuilding the application.

My application originally uses SQL Server Express with Entity Framework and everything works fine -- typical slow response on the first request as Entity Framework compiles the database schema, then lightning-fast afterwards. Static variables on the application carry across multiple requests.

I moved from SQL Server Express to SQL Server CE, and then suddenly I see Entity Framework recompiling the schema on every request. That means that every request is now slow.

Also, any static variables I keep on the application is reset for every request -- consistent with a new app domain being built.

Moving the whole stack unchanged to self-hosting on OWIN and suddenly it becomes normal again -- the first request slow, the subsequent ones lightning-fast.

This proves that ASP.NET on OWIN on IIS with SQL Server CE is doing something weird by not reusing existing threads and constantly building new app domains.

Following MatteoSp's advice, I removed SQL Server CE components from the site and created a SQL Server Express database to replace it. Though the compilation time on the site seems to have increased for some reason, this resolved my issue for the slow queries/connection.

Thank you for the assistance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM