EF paging query optimization

According to the usual paging query, you need to query the data at least twice, one operation is to query the total number, and the other is to query the data, which is a bit time-consuming.

Here is an EF-based plug-in  EntityFramework.Extended . Of course, this plug-in has many functions, such as batch deletion, batch modification, batch query, cache query, etc.

Only the method of batch query is introduced here

Monitor the generated SQL code through SQL Server Profilter and find that the database is only connected once, which is very powerful

 


Do not explain directly to the code

General paging query method:

copy code
1         /// <summary>
 2 /// Paging query
 3         /// </summary>
 4 /// <typeparam name="TKey">sort type</typeparam>
 5 /// <param name="pageIndex">current page</param>
 6 /// <param name="pageSize">size of each page</param>
 7 /// <param name="isAsc">Whether to sort in ascending order</param>
 8 /// <param name="predicate">conditional expression</param>
 9 /// <param name="keySelector">sort expression</param>
10         /// <returns></returns>
11         public virtual IPage<TEntity> Page<TKey>(int pageIndex, int pageSize, Expression<Func<TEntity, bool>> predicate, bool isAsc,
12             Expression<Func<TEntity, TKey>> keySelector)
13         {
14             if (pageIndex <= 0 && pageSize <= 0)
15             {
16 throw new Exception("pageIndex or pageSize cannot be less than or equal to 0!");
17             }
18             IPage<TEntity> page = new Page<TEntity>()
19             {
20                 PageIndex = pageIndex,
21                 PageSize = pageSize
22             };
23             int skip = (pageIndex - 1) * pageSize;
24             if (predicate == null)
25             {
26                 FutureCount fcount = this.dbSet.FutureCount();
27                 FutureQuery<TEntity> futureQuery = isAsc
28                     ? this.dbSet.OrderBy(keySelector).Skip(skip).Take(pageSize).Future()
29                     : this.dbSet.OrderByDescending(keySelector).Skip(skip).Take(pageSize).Future();
30                 page.TotalItems = fcount.Value;
31                 page.Items = futureQuery.ToList();
32                 page.TotalPages = page.TotalItems / pageSize;
33                 if ((page.TotalItems % pageSize) != 0) page.TotalPages++;
34             }
35             else
36             {
37                 var queryable = this.dbSet.Where(predicate);
38                 FutureCount fcount = queryable.FutureCount();
39                 FutureQuery<TEntity> futureQuery = isAsc
40                     ? queryable.OrderBy(keySelector).Skip(skip).Take(pageSize).Future()
41                     : queryable.OrderByDescending(keySelector).Skip(skip).Take(pageSize).Future();
42                 page.TotalItems = fcount.Value;
43                 page.Items = futureQuery.ToList();
44                 page.TotalPages = page.TotalItems / pageSize;
45                 if ((page.TotalItems % pageSize) != 0) page.TotalPages++;
46             }
47             return page;
48         }
copy code

 

 Pagination entity:

copy code
1     /// <summary>
 2 /// Paging entity
 3     /// </summary>
 4     /// <typeparam name="T">实体</typeparam>
 5     public class Page<T> : IPage<T>
 6     {
 7         /// <summary>
 8 /// Current page
 9         /// </summary>
10         public int PageIndex { get; set; }
11         /// <summary>
12 /// total pages
13         /// </summary>
14         public int TotalPages { get; set; }
15         /// <summary>
16 /// The total number of query sets
17         /// </summary>
18         public int TotalItems { get; set; }
19         /// <summary>
20 /// Items per page
21         /// </summary>
22         public int PageSize { get; set; }
23         /// <summary>
24 /// Query collection
25         /// </summary>
26         public IList<T> Items { get; set; }
27     }
28
29     /// <summary>
30 /// Paging entity interface
31     /// </summary>
32     /// <typeparam name="T">实体</typeparam>
33     public interface IPage<T>
34     {
35         /// <summary>
36 /// Current page
37         /// </summary>
38         int PageIndex { get; set; }
39         /// <summary>
40 /// total pages
41         /// </summary>
42         int TotalPages { get; set; }
43         /// <summary>
44 /// The total number of query sets
45         /// </summary>
46         int TotalItems { get; set; }
47         /// <summary>
48 /// Items per page
49         /// </summary>
50         int PageSize { get; set; }
51         /// <summary>
52 /// Query collection
53         /// </summary>
54         IList<T> Items { get; set; }
55     }
copy code

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325133032&siteId=291194637