2014-11-14 64 views
0

我試圖在超過10萬條記錄上實現搜索功能。這是使用PagedList時遇到速度問題。PagedList搜索遭受效率低下

public ActionResult CrmBlogGroupType(int? page, bool? Name, bool? AuthorTitle, bool? Description, string search, int? PageSize, string type) 
{ 
    try 
    { 
    if (type==null) 
    { 
     type = "A"; 
    } 
    IEnumerable<Usp_getBlogSetPosts_Result> _objBlogSet = _dataLayer.GetBlogSet(type); 
    //The above _objBlogSet has around 10 thousand records 

    ViewBag.CurrentPage = page; 

    ViewBag.Name = Name ==null?false:Name; 
    ViewBag.AuthorTitle = AuthorTitle == null ? false : AuthorTitle; 
    ViewBag.Description = Description == null ? false : Description; 

    ViewBag.Search = search; 
    ViewBag.type = type; 

    if (Name == true && AuthorTitle == false && Description == false) 
    { 
     _objBlogSet = _objBlogSet.Where(p => p.author_name.ToLower().Contains(search.ToLower())).ToPagedList(page ?? 1, PageSize ?? 10); 
    } 

    return View(_objBlogSet); 

    catch (Exception ex) 
    { 
     throw ex; 
    } 
} 

回答

1

我假設你正在使用特洛伊古德的分頁表(https://github.com/TroyGoode/PagedList

而是與IEnumerable的工作,嘗試使用IQueryable的工作。 這樣,分頁在服務器端完成,性能會更好。

+0

公開名單 Get_MVP_Consultants() { 列表 obj_Consultants = _dbContext.Tbl_MVPConsultant.Where(p值=> p.Visibility == TRUE).ToList(); return obj_Consultants; } 上面是數據層方法,我得到的數據..我如何修改該代碼爲IQueryable .. – Dathatreya

+0

不要轉換該列表。只要你這樣做,數據庫被擊中,一切都會被返回。 – lusocoding

+0

嗨@lusocoding我跟着你的意見,並改變了我的Datalayer方法返回IQueryable結果..但ToPagedList()方法不允許Iqueryable結果..如何現在進行.. – Dathatreya

0

這一切都取決於_dataLayer.GetBlogSet()發生了什麼。您當前的代碼很可能會拉入整個表格並過濾內存中的數據。

這種方法應該返回一個IQueryable<Usp_getBlogSetPosts_Result>,所以ToPagedList()的內部Skip()Take()在該集合將被轉換爲SQL查詢。

+0

您可以修改我的代碼,以便我能理解。 – Dathatreya