在某些系统上打开连接的原因很慢的原因?

时间:2015-03-24 16:29:29

标签: sql-server-ce

我在某些部署的系统上遇到了SQL Server Compact 4.0 SP1 x64的奇怪问题。问题表现为连接打开到SQL CE数据库非常慢。大多数情况下,这种延迟似乎大约是15秒,但它可能会更长(如下面的日志中所示)。打开连接后,后续访问似乎很快如果尝试快速连续发生。但是,只要有一段时间没有访问尝试,就会再次出现缓慢的情况。

这些系统都运行SQL Server 2008 R2,并且都是x64。但是,并非所有已部署的系统都会出现此问题。我的观察是未修补的系统(没有最新的Windows更新)更有可能遇到这个问题。

请参阅以下代码示例以及运行示例时生成的日志。第一个日志显示未修补系统上的运行,第二个日志显示应用了所有Windows更新的计算机上的运行。

代码示例

var watch = new System.Diagnostics.Stopwatch();
_logger.Info("Persistent cache initialization starting...");
watch.Start();
using (var context = new CachedResponseContext())
{
    context.Database.Log = _logger.Debug;
    context.Database.Initialize(force: false);
}
watch.Stop();
_logger.InfoFormat("Persistent cache initialization completed in {0} milliseconds.", watch.ElapsedMilliseconds);

日志显示SLOW连接已打开

请注意第一个日志语句和第二个日志语句之间的大量时间(~78秒)......

2015-03-24 08:49:43,749 [ 4] DEBUG | Redacted.Caching.DatabaseCache | Persistent cache initialization starting...
2015-03-24 08:51:01,899 [ 4] DEBUG | Redacted.Caching.DatabaseCache | Opened connection at 3/24/2015 8:51:01 AM -07:00
2015-03-24 08:51:01,903 [ 4] DEBUG | Redacted.Caching.DatabaseCache | 
SELECT Count(*)
FROM INFORMATION_SCHEMA.TABLES AS t
WHERE t.TABLE_NAME IN ('CachedResponses','edmMetadataContextTableName')
2015-03-24 08:51:01,903 [ 4] DEBUG | Redacted.Caching.DatabaseCache | 
2015-03-24 08:51:01,905 [ 4] DEBUG | Redacted.Caching.DatabaseCache | -- Executing at 3/24/2015 8:51:01 AM -07:00
2015-03-24 08:51:01,940 [ 4] DEBUG | Redacted.Caching.DatabaseCache | -- Completed in 33 ms with result: 1
2015-03-24 08:51:01,940 [ 4] DEBUG | Redacted.Caching.DatabaseCache | 
2015-03-24 08:51:01,947 [ 4] DEBUG | Redacted.Caching.DatabaseCache | Closed connection at 3/24/2015 8:51:01 AM -07:00
2015-03-24 08:51:02,104 [ 4] DEBUG | Redacted.Caching.DatabaseCache | Opened connection at 3/24/2015 8:51:02 AM -07:00
2015-03-24 08:51:03,196 [ 4] DEBUG | Redacted.Caching.DatabaseCache | SELECT 
    [GroupBy1].[A1] AS [C1]
    FROM ( SELECT 
        COUNT(1) AS [A1]
        FROM [__MigrationHistory] AS [Extent1]
        WHERE [Extent1].[ContextKey] = @p__linq__0
    )  AS [GroupBy1]
2015-03-24 08:51:03,196 [ 4] DEBUG | Redacted.Caching.DatabaseCache | 
2015-03-24 08:51:03,197 [ 4] DEBUG | Redacted.Caching.DatabaseCache | -- p__linq__0: 'Redacted.Caching.CachedResponseContext' (Type = String)
2015-03-24 08:51:03,197 [ 4] DEBUG | Redacted.Caching.DatabaseCache | -- Executing at 3/24/2015 8:51:03 AM -07:00
2015-03-24 08:51:03,212 [ 4] DEBUG | Redacted.Caching.DatabaseCache | -- Completed in 14 ms with result: SqlCeDataReader
2015-03-24 08:51:03,212 [ 4] DEBUG | Redacted.Caching.DatabaseCache | 
2015-03-24 08:51:03,230 [ 4] DEBUG | Redacted.Caching.DatabaseCache | Closed connection at 3/24/2015 8:51:03 AM -07:00
2015-03-24 08:51:03,245 [ 4] DEBUG | Redacted.Caching.DatabaseCache | Opened connection at 3/24/2015 8:51:03 AM -07:00
2015-03-24 08:51:03,313 [ 4] DEBUG | Redacted.Caching.DatabaseCache | SELECT TOP (1) 
    [Project1].[C1] AS [C1], 
    [Project1].[MigrationId] AS [MigrationId], 
    [Project1].[Model] AS [Model], 
    [Project1].[ProductVersion] AS [ProductVersion]
    FROM ( SELECT 
        [Extent1].[MigrationId] AS [MigrationId], 
        [Extent1].[Model] AS [Model], 
        [Extent1].[ProductVersion] AS [ProductVersion], 
        1 AS [C1]
        FROM [__MigrationHistory] AS [Extent1]
        WHERE [Extent1].[ContextKey] = @p__linq__0
    )  AS [Project1]
    ORDER BY [Project1].[MigrationId] DESC
2015-03-24 08:51:03,313 [ 4] DEBUG | Redacted.Caching.DatabaseCache | 
2015-03-24 08:51:03,314 [ 4] DEBUG | Redacted.Caching.DatabaseCache | -- p__linq__0: 'Redacted.Caching.CachedResponseContext' (Type = String)
2015-03-24 08:51:03,314 [ 4] DEBUG | Redacted.Caching.DatabaseCache | -- Executing at 3/24/2015 8:51:03 AM -07:00
2015-03-24 08:51:03,315 [ 4] DEBUG | Redacted.Caching.DatabaseCache | -- Completed in 1 ms with result: SqlCeDataReader
2015-03-24 08:51:03,315 [ 4] DEBUG | Redacted.Caching.DatabaseCache | 
2015-03-24 08:51:03,332 [ 4] DEBUG | Redacted.Caching.DatabaseCache | Closed connection at 3/24/2015 8:51:03 AM -07:00
2015-03-24 08:51:03,355 [ 4] DEBUG | Redacted.Caching.DatabaseCache | Persistent cache initialization completed in 79601 milliseconds.

日志显示FAST连接已打开

请注意第一个日志语句与第二个日志之间的少量时间......

2015-03-24 08:34:03,529 [ 4] DEBUG | Redacted.Caching.DatabaseCache | Persistent cache initialization starting...
2015-03-24 08:34:04,471 [ 4] DEBUG | Redacted.Caching.DatabaseCache | Opened connection at 3/24/2015 8:34:04 AM -07:00
2015-03-24 08:34:04,475 [ 4] DEBUG | Redacted.Caching.DatabaseCache | 
SELECT Count(*)
FROM INFORMATION_SCHEMA.TABLES AS t
WHERE t.TABLE_NAME IN ('CachedItems','edmMetadataContextTableName')
2015-03-24 08:34:04,475 [ 4] DEBUG | Redacted.Caching.DatabaseCache | 
2015-03-24 08:34:04,477 [ 4] DEBUG | Redacted.Caching.DatabaseCache | -- Executing at 3/24/2015 8:34:04 AM -07:00
2015-03-24 08:34:04,513 [ 4] DEBUG | Redacted.Caching.DatabaseCache | -- Completed in 33 ms with result: 1
2015-03-24 08:34:04,513 [ 4] DEBUG | Redacted.Caching.DatabaseCache | 
2015-03-24 08:34:04,517 [ 4] DEBUG | Redacted.Caching.DatabaseCache | Closed connection at 3/24/2015 8:34:04 AM -07:00
2015-03-24 08:34:04,662 [ 4] DEBUG | Redacted.Caching.DatabaseCache | Opened connection at 3/24/2015 8:34:04 AM -07:00
2015-03-24 08:34:05,725 [ 4] DEBUG | Redacted.Caching.DatabaseCache | SELECT 
    [GroupBy1].[A1] AS [C1]
    FROM ( SELECT 
        COUNT(1) AS [A1]
        FROM [__MigrationHistory] AS [Extent1]
        WHERE [Extent1].[ContextKey] = @p__linq__0
    )  AS [GroupBy1]
2015-03-24 08:34:05,726 [ 4] DEBUG | Redacted.Caching.DatabaseCache | 
2015-03-24 08:34:05,727 [ 4] DEBUG | Redacted.Caching.DatabaseCache | -- p__linq__0: 'Redacted.Caching.CachedResponseContext' (Type = String)
2015-03-24 08:34:05,727 [ 4] DEBUG | Redacted.Caching.DatabaseCache | -- Executing at 3/24/2015 8:34:05 AM -07:00
2015-03-24 08:34:05,743 [ 4] DEBUG | Redacted.Caching.DatabaseCache | -- Completed in 15 ms with result: SqlCeDataReader
2015-03-24 08:34:05,743 [ 4] DEBUG | Redacted.Caching.DatabaseCache | 
2015-03-24 08:34:05,754 [ 4] DEBUG | Redacted.Caching.DatabaseCache | Closed connection at 3/24/2015 8:34:05 AM -07:00
2015-03-24 08:34:05,770 [ 4] DEBUG | Redacted.Caching.DatabaseCache | Opened connection at 3/24/2015 8:34:05 AM -07:00
2015-03-24 08:34:05,842 [ 4] DEBUG | Redacted.Caching.DatabaseCache | SELECT TOP (1) 
    [Project1].[C1] AS [C1], 
    [Project1].[MigrationId] AS [MigrationId], 
    [Project1].[Model] AS [Model], 
    [Project1].[ProductVersion] AS [ProductVersion]
    FROM ( SELECT 
        [Extent1].[MigrationId] AS [MigrationId], 
        [Extent1].[Model] AS [Model], 
        [Extent1].[ProductVersion] AS [ProductVersion], 
        1 AS [C1]
        FROM [__MigrationHistory] AS [Extent1]
        WHERE [Extent1].[ContextKey] = @p__linq__0
    )  AS [Project1]
    ORDER BY [Project1].[MigrationId] DESC
2015-03-24 08:34:05,842 [ 4] DEBUG | Redacted.Caching.DatabaseCache | 
2015-03-24 08:34:05,842 [ 4] DEBUG | Redacted.Caching.DatabaseCache | -- p__linq__0: 'Redacted.Caching.CachedResponseContext' (Type = String)
2015-03-24 08:34:05,842 [ 4] DEBUG | Redacted.Caching.DatabaseCache | -- Executing at 3/24/2015 8:34:05 AM -07:00
2015-03-24 08:34:05,844 [ 4] DEBUG | Redacted.Caching.DatabaseCache | -- Completed in 1 ms with result: SqlCeDataReader
2015-03-24 08:34:05,844 [ 4] DEBUG | Redacted.Caching.DatabaseCache | 
2015-03-24 08:34:05,851 [ 4] DEBUG | Redacted.Caching.DatabaseCache | Closed connection at 3/24/2015 8:34:05 AM -07:00
2015-03-24 08:34:05,875 [ 4] DEBUG | Redacted.Caching.DatabaseCache | Persistent (database) response cache initialization completed in 2345 milliseconds.

我非常感谢任何建议。当然,我计划强烈建议所有系统都应用最新的Windows更新。但是,我非常想知道问题可能是什么以及哪些特定的Windows更新可能是有效的。

非常感谢,

1 个答案:

答案 0 :(得分:0)

很可能是由于代理配置无效,但也有其他原因http://erikej.blogspot.dk/2013/08/faq-why-is-opening-my-sql-server.html

此外,在使用EF而不是原始ADO.NET或直接表访问时,您通常会遇到一定的开销