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存储大数据量的问题,主要是数据库中的两张历史数据表,一张模拟量历史数据和一张开 ...
随机推荐
- paxos算法—今生
Paxos 定义2.1 票:即弱化形式的锁.它具备下面几个性质: 可重新发布:服务器可以重新发布新票,即使前面发布的票没有释放. 票可以过期:客户端用一张票来给服务器发送命令请求时,只有当这张票是最 ...
- 洛谷 P5564: [Celeste-B]Say Goodbye
题目传送门:洛谷 P5564. 题意简述: 有 \(n\) 个点,染 \(m\) 种颜色,第 \(i\) 种颜色染恰好 \(cnt_i\) 个节点,满足 \(cnt_1+cnt_2+\cdots+cn ...
- 第 33课 C++中的字符串(上)
历史的遗留问题在C语言中没有真正意义上的字符串,为了表达字符串这个概念利用字符数组来模拟字符串.C语言不支持真正意义上的字符串 (C++也不支持)C语言用字符数组和一组函数实现字符串操作 (C++中同 ...
- Nginx 环境搭建 (windows)
Nginx 环境搭建 (windows) 资源 # nginx在线文档和支持 For online documentation and support please refer to nginx.or ...
- Nginx日志管理(四)
Nginx日志对于统计.系统服务排错很有用.Nginx日志主要分为两种:access_log(访问日志)和error_log(错误日志).通过访问日志我们可以得到用户的IP地址.浏览器的信息,请求的处 ...
- 论文阅读笔记六十三:DeNet: Scalable Real-time Object Detection with Directed Sparse Sampling(CVPR2017)
论文原址:https://arxiv.org/abs/1703.10295 github:https://github.com/lachlants/denet 摘要 本文重新定义了目标检测,将其定义为 ...
- Python搭建HTTP服务
我们平时可能会需要HTTP服务,本机搭建一个服务器来完成有些过于繁琐,这时就可以用python帮我们搭建一个HTTP服务器,省时高效. python 2.x 输入命令:python -m Simple ...
- requests--传递参数
传递参数 传递URL参数 data = {'city': '北京'} # 参数有中文如果发送不了,必须要编码 city = parse.urlencode(data).encode('utf-8') ...
- 洛谷 P3742 umi的函数
传送门 思路 \(loceaner\)已经蔡虚鲲到连红题都不会做了 因为有\(special\ judge\)所以我们就可以瞎搞了! 由题目可知,只要有一个\(y[i] > x[i]\)则一定没 ...
- bootstrap-table 列拖动
1.页面js/css <!-- bootstrap 插件样式 --> <link th:href="@{/common/bootstrap-3.3.6/css/bootst ...