2012-12-12 58 views
0

我使用實體框架使用.skip.take實施分頁。這工作正常,但是當我獲取10萬條記錄的數據庫中的記錄數(對於網格頁碼而言UIClient需要計數)時,它佔用大量時間,大約600毫秒,這是很大的。使用實體框架進行分頁時的性能問題

如果我不使用計數只有尋呼實現,那麼它幾乎需要20到25毫秒。如何進行高效計數?我如何從600毫秒降低到50毫秒左右?我用

示例查詢:

int count = (from c in dbcontext.Customer 
      where c.customerName ='xyz' && c.date >= 'dateTime' 
      select c.CustomerId).Count(); 

我有索引上NamedateTimeCustomerId是主鍵。

由於提前,

阿希奈

+0

這是''dateTime''值的佔位符?什麼是'c.date'屬性的類型? – rcdmk

回答

1

你可以使用SQL Server Profiler來抓住從實體框架生成的查詢,通過查詢分析器在SQL Server Management Studio中運行呢?

從那裏您可以看到執行計劃並查看瓶頸發生的位置,並調整您的代碼以更好地執行。

我認爲這個信息會越來越明顯。

0

嘗試使用你已經在使用分頁之前的記錄數,看是否performes更好:

var records = (from c in dbcontext.Customer where c.customerName ='xyz' && c.date >= 'dateTime' select c.CustomerId); 

var total = records.Count; 
var pagedRecors = records.Skip((currentPage - 1) * recordsPerPage).Take(recordsPerPage).ToList(); 
+0

好主意 - **但是:**因爲'CustomerId'是主鍵,最有可能它也是集羣鍵,因此您的語句將導致*聚簇索引掃描*(即**全表掃描** )所以我不認爲這會節省時間,真的。 –