服务器之家

服务器之家 > 正文

MySql批量插入优化Sql执行效率实例详解

时间:2020-07-26 18:24     来源/作者:MYSQL教程网

MySql批量插入优化Sql执行效率实例详解

itemcontractprice数量1万左右,每条itemcontractprice 插入5条日志。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
updateInsertSql.AppendFormat("UPDATE itemcontractprice AS p INNER JOIN foreigncurrency AS f ON p.ForeignCurrencyId = f.ContractPriceId SET p.RemainPrice = f.RemainPrice * {0},p.BuyOutPrice = f.BuyOutPrice * {0},p.ReservedPrice = f.ReservedPrice * {0},p.CollectedPrice = f.CollectedPrice * {0},p.AccessPrice = f.AccessPrice * {0} WHERE p.CurrencyId = {1} AND p.date BETWEEN '{2:yyyy-MM-dd}' AND '{3:yyyy-MM-dd}';", rate.ExchangeRate, exchangeRate.CurrencyId, rate.BeginDate, rate.EndDate);
 
updateInsertSql.AppendFormat("INSERT INTO `itemcontractpricelog`(`ContractPriceType`,`ContractPrice`,`FcContractPrice`,`IsExpire`,`LogRemark`,`CreatedByName`,`CreatedById`,`CreatedDate`,`LogTypeId`,`ProviderId`,`StageId`,`Date`,`CurrencyId`,`ContractPriceId`,`StockPattern`,`ItemId`) SELECT 0,c.RemainPrice,f.RemainPrice,c.RemainIsExpire,'外币汇率调整,重新计算人民币底价','job',0,NOW(),5,c.ProviderId,c.StageId,c.Date,c.CurrencyId,c.ContractPriceId,0,c.ItemId FROM itemcontractprice AS c INNER JOIN foreigncurrency AS f ON c.ForeignCurrencyId = f.ContractPriceId WHERE c.CurrencyId={0} AND c.date BETWEEN '{1:yyyy-MM-dd}' AND '{2:yyyy-MM-dd}';", exchangeRate.CurrencyId, rate.BeginDate, rate.EndDate);
 
updateInsertSql.AppendFormat(" INSERT INTO `itemcontractpricelog`(`ContractPriceType`,`ContractPrice`,`FcContractPrice`,`IsExpire`,`LogRemark`,`CreatedByName`,`CreatedById`,`CreatedDate`,`LogTypeId`,`ProviderId`,`StageId`,`Date`,`CurrencyId`,`ContractPriceId`,`StockPattern`,`ItemId`) SELECT 1,c.BuyOutPrice,f.BuyOutPrice,c.BuyOutIsExpire,'外币汇率调整,重新计算人民币底价','job',0,NOW(),5,c.ProviderId,c.StageId,c.Date,c.CurrencyId,c.ContractPriceId,0,c.ItemId FROM itemcontractprice AS c INNER JOIN foreigncurrency AS f ON c.ForeignCurrencyId = f.ContractPriceId WHERE c.CurrencyId={0} AND c.date BETWEEN '{1:yyyy-MM-dd}' AND '{2:yyyy-MM-dd}';", exchangeRate.CurrencyId, rate.BeginDate, rate.EndDate);
 
updateInsertSql.AppendFormat("INSERT INTO `itemcontractpricelog`(`ContractPriceType`,`ContractPrice`,`FcContractPrice`,`IsExpire`,`LogRemark`,`CreatedByName`,`CreatedById`,`CreatedDate`,`LogTypeId`,`ProviderId`,`StageId`,`Date`,`CurrencyId`,`ContractPriceId`,`StockPattern`,`ItemId`) SELECT 2,c.ReservedPrice,f.ReservedPrice,c.ReservedIsExpire,'外币汇率调整,重新计算人民币底价','job',0,NOW(),5,c.ProviderId,c.StageId,c.Date,c.CurrencyId,c.ContractPriceId,0,c.ItemId FROM itemcontractprice AS c INNER JOIN foreigncurrency AS f ON c.ForeignCurrencyId = f.ContractPriceId WHERE c.CurrencyId={0} AND c.date BETWEEN '{1:yyyy-MM-dd}' AND '{2:yyyy-MM-dd}';", exchangeRate.CurrencyId, rate.BeginDate, rate.EndDate);
 
updateInsertSql.AppendFormat("INSERT INTO `itemcontractpricelog`(`ContractPriceType`,`ContractPrice`,`FcContractPrice`,`IsExpire`,`LogRemark`,`CreatedByName`,`CreatedById`,`CreatedDate`,`LogTypeId`,`ProviderId`,`StageId`,`Date`,`CurrencyId`,`ContractPriceId`,`StockPattern`,`ItemId`) SELECT 3,c.CollectedPrice,f.CollectedPrice,c.CollectedIsExpire,'外币汇率调整,重新计算人民币底价','job',0,NOW(),5,c.ProviderId,c.StageId,c.Date,c.CurrencyId,c.ContractPriceId,0,c.ItemId FROM itemcontractprice AS c INNER JOIN foreigncurrency AS f ON c.ForeignCurrencyId = f.ContractPriceId WHERE c.CurrencyId={0} AND c.date BETWEEN '{1:yyyy-MM-dd}' AND '{2:yyyy-MM-dd}';", exchangeRate.CurrencyId, rate.BeginDate, rate.EndDate);
updateInsertSql.AppendFormat("INSERT INTO `itemcontractpricelog`(`ContractPriceType`,`ContractPrice`,`FcContractPrice`,`IsExpire`,`LogRemark`,`CreatedByName`,`CreatedById`,`CreatedDate`,`LogTypeId`,`ProviderId`,`StageId`,`Date`,`CurrencyId`,`ContractPriceId`,`StockPattern`,`ItemId`) SELECT 4,c.AccessPrice,f.AccessPrice,c.AccessIsExpire,'外币汇率调整,重新计算人民币底价','job',0,NOW(),5,c.ProviderId,c.StageId,c.Date,c.CurrencyId,c.ContractPriceId,0,c.ItemId FROM itemcontractprice AS c INNER JOIN foreigncurrency AS f ON c.ForeignCurrencyId = f.ContractPriceId WHERE c.CurrencyId={0} AND c.date BETWEEN '{1:yyyy-MM-dd}' AND '{2:yyyy-MM-dd}';", exchangeRate.CurrencyId, rate.BeginDate, rate.EndDate);
//var curContractPriceList = itemContractPriceList.Where(o => o.CurrencyId == exchangeRate.CurrencyId && o.Date >= rate.BeginDate && o.Date <= rate.EndDate).ToList();
logger.InfoFormat("底价更新和日志sql:{0}", updateInsertSql.ToString());
//if (curContractPriceList.Count == 0) continue;
int effctRows = 0;
using (var tran = UnitOfWorkManager.Begin())
{
  effctRows = taskRepository.ExecuteSql(updateInsertSql.ToString(), false);
  tran.Complete();
}
logger.InfoFormat("底价更新影响行数:{0}", effctRows);

正常情况下大概20秒钟就ok.

之前是用EF操作,查询出来 ,要耗时,然后再组装 update语句 ,然后再插入日志(每条数据5条日志),这个网络交互的时间加上数据库连接打开关闭的时间,总的执行时间,大概10多分钟。

用sql语句批量操作,可以说效率提升了 40倍,就是大量数据的传输和数据库的处理次数耗时。

所以说,软件开发不是开发完成就行,而是要解决性能上的问题,这才是开发的进阶。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意 2019-07-07
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
返回顶部