jeudi 19 mars 2015

Entity Framework Takes a longer time to get the resposne the first time using within the same context


I am having an issues with the EF that the first query takes a long time. I thought the query itself was taking a long time. So, I used



context.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);


to see what query is being sent. It only took only 1 ms but from the open connection to close connection, it took 18 second. The following is the message from the debug message.



**Opened connection at 3/19/2015 9:25:49 PM +06:30
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[ItemId] AS [ItemId],
[Extent1].[SerialNumber] AS [SerialNumber],
[Extent1].[SimNumber] AS [SimNumber],
[Extent1].[ItemStatusId] AS [ItemStatusId],
[Extent1].[StoreId] AS [StoreId]
FROM [dbo].[ItemDetail] AS [Extent1]
-- Executing at 3/19/2015 9:25:49 PM +06:30
-- Completed in 1 ms with result: SqlDataReader
Closed connection at 3/19/2015 9:26:07 PM +06:30**


Within the same context, another query similar to the previous one was sent. It only took 1 second from the Open to Close connection.



**Opened connection at 3/19/2015 9:26:10 PM +06:30

SELECT
[Extent1].[Id] AS [Id],
[Extent1].[ItemId] AS [ItemId],
[Extent1].[SerialNumber] AS [SerialNumber],
[Extent1].[SimNumber] AS [SimNumber],
[Extent1].[ItemStatusId] AS [ItemStatusId],
[Extent1].[StoreId] AS [StoreId]
FROM [dbo].[ItemDetail] AS [Extent1]
INNER JOIN [dbo].[Item] AS [Extent2] ON [Extent1].[ItemId] = [Extent2].[Id]
WHERE ([Extent1].[ItemStatusId] = @p__linq__0) AND ([Extent2].[CategoryId] = @p__linq__1) AND ([Extent1].[StoreId] = @p__linq__2)


-- p__linq__0: '1' (Type = Int32, IsNullable = false)

-- p__linq__1: '2' (Type = Int32, IsNullable = false)

-- p__linq__2: '1' (Type = Int32, IsNullable = false)

-- Executing at 3/19/2015 9:26:10 PM +06:30

-- Completed in 1 ms with result: SqlDataReader

Closed connection at 3/19/2015 9:26:11 PM +06:30**


Why does the first query take longer time to close the connection? I know that the first query usually take time because of loading the meta data. But this is different that the open connection and executing query are so close and after getting the results, it takes a long time to close the connection in the first query.


Somebody share some lights on me for this issue.





Aucun commentaire:

Enregistrer un commentaire