Dapper学习(四)之Dapper Plus的大数据量的操作
这篇文章主要讲 Dapper Plus,它使用用来操作大数量的一些操作的。比如插入1000条,或者10000条的数据时,再使用Dapper的Execute方法,就会比较慢了。这时候,可以使用Dapper Plus中的方法进行操作,提高速度。
主要包括下面:
- Bulk Insert
- Bulk Update
- Bulk Delete
- Bulk Merge
使用之前,需要在Nuget中,安装 Z.Dapper.Plus
注意:这个组件是收费的,每个月会有一个试用版本,没有免费版本
另外一种提高批量插入速度的方式:通过把多个插入语句通过字符串拼接使成为一个长的sql语句来实现。
1. Bulk Insert:批量插入
1.1 Insert Single : 使用Bulk插入单个实体
DapperPlusManager.Entity<Customer>().Table("Customers");
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
connection.BulkInsert(new List<Customer>() { new Customer() { CustomerName = "ExampleBulkInsert", ContactName = "Example Name :" + }});
}
1.2 Insert Many :插入多个实体
DapperPlusManager.Entity<Customer>().Table("Customers");
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
connection.BulkInsert(customers);
}
1.3 Insert with relation(One to One)
插入一对一关系的实体
DapperPlusManager.Entity<Supplier>().Table("Suppliers").Identity(x => x.SupplierID);
DapperPlusManager.Entity<Product>().Table("Products").Identity(x => x.ProductID);
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
connection.BulkInsert(suppliers).ThenForEach(x => x.Product.SupplierID = x.SupplierID).ThenBulkInsert(x => x.Product);
}
1.4 Insert with relation (One to Many)
插入一对多关系的实体
DapperPlusManager.Entity<Supplier>().Table("Suppliers").Identity(x => x.SupplierID);
DapperPlusManager.Entity<Product>().Table("Products").Identity(x => x.ProductID);
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
connection.BulkInsert(suppliers).ThenForEach(x => x.Products.ForEach(y => y.SupplierID = x.SupplierID)).ThenBulkInsert(x => x.Products);
}
2. Bulk Update
2.1 Update Single 和 Update Many
DapperPlusManager.Entity<Customer>().Table("Customers");
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
connection.BulkUpdate(customers);
}
2.2 Update with relation(One to One) 和 Update with relation(One to Many)
更新一对一关系的实体
DapperPlusManager.Entity<Supplier>().Table("Suppliers").Identity(x => x.SupplierID);
DapperPlusManager.Entity<Product>().Table("Products").Identity(x => x.ProductID);
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
connection.BulkUpdate(suppliers, x => x.Product);
}
更新一对多关系的实体
DapperPlusManager.Entity<Supplier>().Table("Suppliers").Identity(x => x.SupplierID);
DapperPlusManager.Entity<Product>().Table("Products").Identity(x => x.ProductID);
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
connection.BulkUpdate(suppliers, x => x.Products);
}
3. Bulk Delete
3.1 Delete Single
删除单个实体
DapperPlusManager.Entity<Customer>().Table("Customers").Key("CustomerID");
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
connection.BulkDelete(connection.Query<Customer>("Select * FROM CUSTOMERS WHERE CustomerID in (53,57) ").ToList());
}
3.2 Delete Many
删除多个实体
DapperPlusManager.Entity<Customer>().Table("Customers").Key("CustomerID");
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
connection.BulkDelete(connection.Query<Customer>("Select * FROM CUSTOMERS WHERE CustomerID in (53,57) ").ToList());
}
3.3 Delete with relation(One to One)
删除一对一关系的实体
DapperPlusManager.Entity<Supplier>().Table("Suppliers").Identity(x => x.SupplierID);
DapperPlusManager.Entity<Product>().Table("Products").Identity(x => x.ProductID);
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
connection.BulkDelete(suppliers.Select(x => x.Product)).BulkDelete(suppliers);
}
3.4 Delete with relation(One to Many)
删除一对多关系的实体
DapperPlusManager.Entity<Supplier>().Table("Suppliers").Identity(x => x.SupplierID);
DapperPlusManager.Entity<Product>().Table("Products").Identity(x => x.ProductID);
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
connection.BulkDelete(suppliers.SelectMany(x => x.Products)).BulkDelete(suppliers);
}
4.Bulk Merge
4.1 Merge Single
DapperPlusManager.Entity<Customer>().Table("Customers");
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
connection.BulkMerge(new List<Customer>() { new Customer() { CustomerName = "ExampleBulkMerge", ContactName = "Example Name :" + }});
}
4.2 Merge Many
DapperPlusManager.Entity<Customer>().Table("Customers");
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
connection.BulkMerge(customers);
}
4.3 Merge with relation(One to One)
DapperPlusManager.Entity<Supplier>().Table("Suppliers").Identity(x => x.SupplierID);
DapperPlusManager.Entity<Product>().Table("Products").Identity(x => x.ProductID);
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
connection.BulkMerge(suppliers).ThenForEach(x => x.Product.SupplierID = x.SupplierID).ThenBulkMerge(x => x.Product);
}
4.4 Merge with relation(One to Many)
DapperPlusManager.Entity<Supplier>().Table("Suppliers").Identity(x => x.SupplierID);
DapperPlusManager.Entity<Product>().Table("Products").Identity(x => x.ProductID);
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
connection.BulkMerge(suppliers).ThenForEach(x => x.Products.ForEach(y => y.SupplierID = x.SupplierID)).ThenBulkMerge(x => x.Products);
}
Dapper学习(四)之Dapper Plus的大数据量的操作的更多相关文章
- ODAC (V9.5.15) 学习笔记(二十)大数据量获取处理
ODAC获取数据的效率比较高,在Web程序中希望能够更快获取第一页的数据时,可以有几种方式: 1.在数据库中进行分页处理: 2.获取所有数据,只是快速返回第一页数据. 第一种方案对应用服务器资源消耗最 ...
- MERGE INTO 解决大数据量复杂操作更新慢的问题
现我系统中有一条复杂SQL,由于业务复杂需要关联人员的工作离职三个表,并进行分支判断,再计算人员的字段信息,由于人员多,分支多,计算复杂等原因,一次执行需要5min,容易卡死,现在使用MERGE IN ...
- c#中@标志的作用 C#通过序列化实现深表复制 细说并发编程-TPL 大数据量下DataTable To List效率对比 【转载】C#工具类:实现文件操作File的工具类 异步多线程 Async .net 多线程 Thread ThreadPool Task .Net 反射学习
c#中@标志的作用 参考微软官方文档-特殊字符@,地址 https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/toke ...
- 大数据量高并发的数据库优化,sql查询优化
一.数据库结构的设计 如果不能设计一个合理的数据库模型,不仅会增加客户端和服务器段程序的编程和维护的难度,而且将会影响系统实际运行的性能.所以,在一个系统开始实施之前,完备的数据库模型的设计是必须的. ...
- MySQL随机获取数据的方法,支持大数据量
最近做项目,需要做一个从mysql数据库中随机取几条数据出来. 总所周知,order by rand 会死人的..因为本人对大数据量方面的只是了解的很少,无解,去找百度老师..搜索结果千篇一律.特发到 ...
- mysql innobackupex xtrabackup 大数据量 备份 还原
大数据量备份与还原,始终是个难点.当MYSQL超10G,用mysqldump来导出就比较慢了.在这里推荐xtrabackup,这个工具比mysqldump要快很多. 一.Xtrabackup介绍 1, ...
- asp.net中绘制大数据量的可交互的图表
在一个asp.net项目中要用到能绘制大数据量信息的图表,并且是可交互的(放大.缩小.导出.打印.实时数据),能够绘制多种图形. 为此进行了多方调查预研工作,预研过微软的MsChart图表组件.基于j ...
- mysql innobackupex xtrabackup 大数据量 备份 还原(转)
原文:http://blog.51yip.com/mysql/1650.html 作者:海底苍鹰 大数据量备份与还原,始终是个难点.当MYSQL超10G,用mysqldump来导出就比较慢了.在这里推 ...
- MySQL数据库如何解决大数据量存储问题
利用MySQL数据库如何解决大数据量存储问题? 各位高手您们好,我最近接手公司里一个比较棘手的问题,关于如何利用MySQL存储大数据量的问题,主要是数据库中的两张历史数据表,一张模拟量历史数据和一张开 ...
随机推荐
- mysql update运行超时解决方案
问题描述: 今天update(修改)mysql数据库中一张表时,发现时间很长,而且会失败.报错:Error Code: 1205. Lock wait timeout exceeded; try re ...
- Deep Q Network(DQN)原理解析
1. 前言 在前面的章节中我们介绍了时序差分算法(TD)和Q-Learning,当状态和动作空间是离散且维数不高时可使用Q-Table储存每个状态动作对的Q值,而当状态和动作空间是高维连续时,使用Q- ...
- pdfium 之二
https://www.foxitsoftware.cn/products/premium-pdfium/feature.php 基于谷歌PDFium开源代码 谷歌采用福昕的PDF技术为其PDF开源项 ...
- 代码审计-数组返回NULL绕过
<?php $flag = "flag"; if (isset ($_GET['password'])) { if (ereg ("^[a-zA-Z0-9]+$&q ...
- Spring Boot中以代码方式配置Tomcat
在Spring Boot2.0以上配置嵌入式Servlet容器时EmbeddedServletContainerCustomizer类不存在,经网络查询发现被WebServerFactoryCusto ...
- 【oracle】update select语句
- 学习-JVM命令
jstat jstat (JVM statistics Monitoring)是用于监视虚拟机运行时状态信息的命令,它可以显示出虚拟机进程中的类装载.内存.垃圾收集.JIT编译等运行数据. 格式:js ...
- DBLog: A Generic Change-Data-Capture Framework
转自:https://medium.com/netflix-techblog/dblog-a-generic-change-data-capture-framework-69351fb9099b 期待 ...
- 日常笔记3关于bool类型数组初始化的问题
一般会有两种考虑,全为true或全为false 赋值方式: <1>memset(boolArray,0,sizeof(Array)); 头文件:#include<cstring> ...
- 学习vue_01
目录 vue 框架: 框架介绍 基础格式 插值表达式: 文本指令: 事件指令: 属性指令: 小结: js 补充(面向对象): vue 框架: --构建虚拟的DOM结构,(内存内改变对象)- 操作数据的 ...