请选择 进入手机版 | 继续访问电脑版

马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

.NET Core ORM 类库Petapoco中对分页Page添加Order By对查询的影响

[复制链接]
查看2564 | 回复1 | 2019-10-24 09:50:32 | 显示全部楼层 |阅读模式

最近不停在使用Petapoco+Entity Framework Core联合开辟一套系统。

使用EFCore举行Code First编码,使用PMC命令生成数据库表的信息。

使用Petapoco举行数据库的通例操纵。而且联合PetaPoco.SqlKata的使用,淘汰了编写SQL语句的工作量,对提拔开辟服从有很大的帮助。Petapoco对数据库的支持非常的全,包罗通例的一下数据库:SQL Server,SQL Server CE,MS Access,SQLite,MySQL,MariaDB,PostgreSQL,Firebird DB和Oracle。当然SQL Server为默认的支持。PetaPoco.SqlKata支持的数据库也黑白常全的,包罗:SqlServer, MySql, Postgres, Firebird, SQLite, Oracle。

在数据库操纵过程中,发现每个Controller的Index页面加载的非常缓慢,加载完成约莫须要5s的时间,在浏览器端等候的时间相对来说黑白常长的一个时间。对于出现的问题终于有时间举行办理一下了。

先来看一下未使用Order By加载页面的耗时环境,第一个图中涉及的表的主键为guid类型,第二个图中涉及的主键为ulong类型,对于差别的主键举行分页查询时也有较大影响。

095033kfof0z0l0yjzrf1o.png

图一
095033eq39p5qucjco3iaq.png

图二
使用Order by加载页面的耗时环境
095033zgs3qt9wvy9ecj4v.png

图三
095034bsngsn3sjrmg7nat.png

图四


对出现的问题,使用StopWatch举行监督运行时间的长短,使用了分页的两种方法,区别是否加Order By语句,构成成如下四种环境:

  • PageAsync Order by
  • PageAsync
  • Page Order by
  • Page

代码如下:

  1. <code>Stopwatch stop = new Stopwatch();
  2. stop.Start();
  3. var pages = await _context.PageAsync<productdto>(page, itemsPerPage, "order by id");
  4. stop.Stop();
  5. _logger.Information($" Order By Async查询的实行时间:{stop.Elapsed}");
  6. stop.Restart();
  7. var pages2 = await _context.PageAsync<productdto>(page, itemsPerPage );
  8. stop.Stop();
  9. _logger.Information($"Async查询的实行时间:{stop.Elapsed}");
  10. //_logger.Information($"SQL:{_context.LastSQL}");
  11. stop.Restart();
  12. var ps = _context.Page<productdto>(page, itemsPerPage, "order by id");
  13. stop.Stop();
  14. _logger.Information($"Order By查询的实行时间:{stop.Elapsed}");
  15. stop.Restart();
  16. var ps2 = await _context.PageAsync<productdto>(page, itemsPerPage);
  17. stop.Stop();
  18. _logger.Information($"查询的实行时间:{stop.Elapsed}");
  19. stop.Restart();
  20. var x = _mapper.Map<page<productviewmodel>&gt;(pages);
  21. stop.Stop();
  22. _logger.Information($"Mapper的实行时间:{stop.Elapsed}");
  23. </code>
复制代码

运行背景输出的日记信息,可以看到对于是否加Order By对查询耗时的影响黑白常大的,对是否使用异步方法对耗时也有部分的影响

095034ehay5y3v6jcy3aj3.png

对于上述的四种环境再次使用Benchmark举行一次性能测试,对使用的数据表的实体类不在列出

  1. <code>namespace PetaPocoPageBenchMark
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. var summary = BenchmarkRunner.Run<petapocopage>();
  8. Console.WriteLine("Hello World!");
  9. }
  10. }
  11. public class PetapocoPage
  12. {
  13. public static IDatabase Database =&gt;
  14. new Database(DatabaseConfiguration.Build()
  15. .UsingConnectionString(
  16. "server=192.168.88.3;port=3306;uid=root;pwd=biobase;database=BiobaseProductionQrCode;")
  17. .UsingProvider<mariadbdatabaseprovider>());
  18. [Benchmark]
  19. public void PageOrderBy()
  20. {
  21. Database.Page<productmanufacturelinedetaildto>(1, 20, "order by CreateDate");
  22. }
  23. [Benchmark]
  24. public void Page()
  25. {
  26. Database.Page<productmanufacturelinedetaildto>(1, 20);
  27. }
  28. [Benchmark]
  29. public void PageOrderByAsync()
  30. {
  31. Database.PageAsync<productmanufacturelinedetaildto>(1, 20, "order by CreateDate");
  32. }
  33. [Benchmark]
  34. public void PageAsync()
  35. {
  36. Database.PageAsync<productmanufacturelinedetaildto>(1, 20);
  37. }
  38. }
  39. }
  40. </code>
复制代码

对性能测试效果可以看到,使用Order By对性能的影响确实黑白常大。

095035byrgkic29kig1yy8.png







来源:https://www.cnblogs.com/sesametech-netcore/p/11726779.html
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则