title: How do I improve query speed on a paged grid with a large number of results?
body: <p>I am querying data from our IBM i and displaying it in a grid. The purpose of displaying all records is for a couple reasons:</p>
  
  <ol>
  <li>The existing software isn't used properly and people aren't closing out the items. (user/training issue yes, but see other items). So narrowing down the list to just open items isn't accurate.</li>
  <li>It allows a user to query all history (this is property based and history can be important)</li>
  </ol>
  
  <p>However, there currently is 28,000 items and will ever increase. Right now, I am using MvcContrib grid. Here is my code:</p>
  
  <pre><code>    public ActionResult Index(GridSortOptions gridSortOptions, int? page, int? filterPropertyUniqueKey, int? filterPermitNumber)
      {
          #region Filter and Sort
          var permits = buildingPermitRepository.GetOpenPermits();
  
          // Set default sort and apply filters
          if (filterPermitNumber.HasValue)
          {
              permits = permits.Where(w =&gt; w.PermitId == filterPermitNumber.Value);
          }
  
          // TODO add more filters
  
          if (String.IsNullOrEmpty(gridSortOptions.Column))
          {
              gridSortOptions.Column = "DateApplied";
              gridSortOptions.Direction = SortDirection.Descending;
          }
  
          var permitsPagedList = permits.OrderBy(gridSortOptions.Column, gridSortOptions.Direction).AsPagination(page ?? 1, 20);
          #endregion
  
          var viewModel = new PermitIndexViewModel
          {
              BuildingPermits = permitsPagedList,
              GridSortOptions = gridSortOptions
          };
  
          return View(viewModel);
      }
  </code></pre>
  
  <p>What would you suggest I do differently to improve the display speed? At least for subsequent views.</p>