title: How to improve Skip() Take() performance in paging?
body: <p>According to this link</p>
  <p><a href="https://docs.microsoft.com/pl-pl/aspnet/core/data/ef-mvc/sort-filter-page?view=aspnetcore-3.1" rel="nofollow noreferrer">https://docs.microsoft.com/pl-pl/aspnet/core/data/ef-mvc/sort-filter-page?view=aspnetcore-3.1</a></p>
  <p>I created a pagination in my asp.net core with EF application.</p>
  <p>Becouse I have huge amount of records (over 1 milion) when I'm trying to get last pages it takes enormous amount of time (over 30 sec).</p>
  <p>Is there a way to speed this up?</p>
  <p>Here is my Method which returns PaginatedList</p>
  <pre><code>        public static async Task&lt;PaginatedList&lt;Entry&gt;&gt; CreateAsync(IQueryable&lt;Entry&gt; source, int pageIndex, int pageSize)
          {
              var indexToGet = (pageIndex - 1) * pageSize;
              var count = await source.CountAsync();
              var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
              return new PaginatedList&lt;Entry&gt;(items, count, pageIndex, pageSize);
          }
  </code></pre>
  <p>This line is responsible for slow performance. It seems like Skip and Take methods take a long time to proceed.</p>
  <pre><code>var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
  </code></pre>
  <p><strong>EDIT</strong></p>
  <p>My queries step by step.</p>
  <ol>
  <li>Getting all the records.</li>
  </ol>
  <pre><code>        public async Task&lt;IQueryable&lt;Entry&gt;&gt; GetAll()
          {
              var res = _context.Entries;
              return res;
          }
  </code></pre>
  <ol start="2">
  <li>Sorting.</li>
  </ol>
  <pre><code>switch (sortOrder)
              {
                  case &quot;who_desc&quot;:
                      entries = entries.OrderByDescending(r =&gt; r.Who);
                      break;
                  case &quot;Who&quot;:
                      entries = entries.OrderBy(r =&gt; r.Who);
                      break;
                  case &quot;date_desc&quot;:
                      entries = entries.OrderByDescending(r =&gt; r.DateUtc);
                      break;
                  default:
                      entries = entries.OrderBy(r =&gt; r.DateUtc);
                      break;
              }
  </code></pre>
  <ol start="3">
  <li>Returning to the View</li>
  </ol>
  <pre><code>return View(await PaginatedList&lt;Entry&gt;.CreateAsync(entries.AsNoTracking(), pageNumber ?? 1, pageSize));
  </code></pre>
  <p>And here is my Entry</p>
  <pre><code>    public class Entry
      {
          public long Id { get; set; }
          public string Number { get; set; }
          public string Who { get; set; }
          public long Date { get; set; }
      }
  </code></pre>
  <p>Here is SQL query produced by EF Core</p>
  <pre><code>Microsoft.EntityFrameworkCore.Database.Command[20101]
        Executed DbCommand (59,703ms) [Parameters=[@__p_1='?', @__p_0='?'], CommandType='Text', CommandTimeout='30']
        SELECT &quot;e&quot;.&quot;Id&quot;, &quot;e&quot;.&quot;Date&quot;, &quot;e&quot;.&quot;Number&quot;, &quot;e&quot;.&quot;Who&quot;
        FROM &quot;Entries&quot; AS &quot;e&quot;
        ORDER BY &quot;e&quot;.&quot;Date&quot;
        LIMIT @__p_1 OFFSET @__p_0
  </code></pre>