惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Privacy & Cybersecurity Law Blog
V
V2EX
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Register - Security
The Register - Security
MongoDB | Blog
MongoDB | Blog
P
Privacy International News Feed
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
K
Kaspersky official blog
S
Secure Thoughts
T
Tenable Blog
Security Latest
Security Latest
The Cloudflare Blog
S
Security @ Cisco Blogs
H
Heimdal Security Blog
aimingoo的专栏
aimingoo的专栏
TaoSecurity Blog
TaoSecurity Blog
Blog — PlanetScale
Blog — PlanetScale
Microsoft Security Blog
Microsoft Security Blog
Schneier on Security
Schneier on Security
Webroot Blog
Webroot Blog
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
IT之家
IT之家
Latest news
Latest news
The Hacker News
The Hacker News
C
Check Point Blog
T
The Exploit Database - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
腾讯CDC
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
N
News | PayPal Newsroom
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
S
Security Affairs
S
Securelist
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
A
About on SuperTechFans

博客园 - 拖拉机大叔

个人犯的一个golang routine错误 .NET实现自动编译 做为一个.net码农,打开公司的一个项目,大叔我哭了 Yahoo!团队实践分享:网站性能优化的35条黄金守则 [转载]高性能web开发 利用存储过程导出数据到sql脚本 如何让iframe背景色透明,以及loation.href不起作用 - 拖拉机大叔 - 博客园 SQL大全2 SQL大全 文件以二进制流保存到数据库及下载(转载) DataList中TextBox的OnTextChange事件 - 拖拉机大叔 - 博客园 CommunityServer 2.1初步学习 TreeView高效绑定数据 Javascript常用代码 - 拖拉机大叔 - 博客园 Web页面打印及GridView导出到Excel GridView编辑、删除为英文 SqlServer一些用法 压缩图片上传到数据库 移除DataTable中的行
Sql Server2005 利用XML一次更新多条记录
拖拉机大叔 · 2010-04-09 · via 博客园 - 拖拉机大叔

很久么写博客了,惭愧。今天终于搞定了很久以来一直郁闷的一个问题,写个日志备注下。
我想很多人都知道,在oracle里面,存储过程里面可以传入数组(如int[]),也就是说,可以传多条记录到数据,从而一起更新。减少数据库的请求次数。
但SqlServer呢?bulk Insert这个很多人都知道,我也知道,但可惜,我从来没用过,只有导数据的时候才会考虑,但导数据DTS不是更方便吗?
手头的一个项目,有几个功能,每次需要更新N(N<1000)条记录,记录不多,但如果每次只更新一条,循环insert,那每个功能需要N次请求数据库,如果有1000个并发,那数据库除了做你这个事情,其他的活不用干了。所以,需要尽量减少数据库请求,做到一次更新所有的记录。
幸好,SqlServer给我们提供了一个新功能,利用XML(2000好像是没有这个功能的)。
先来假定一个这样的需求:用户更新一个book,同时需要更新N个章节。
一般的思路是这样,先更新book,然后循环章节数,N次更新数据的章节表。大家可以看下这个性能。

那我们用XML试试

利用XML更新的存储过程

Create PROCEDURE UP_Book_Insert
(
    
@BookId        INT,
    
@ChapterXml    XML

AS
BEGIN
CREATE TABLE #table
(
    ChapterId        
INT,
    ChapterName        
VARCHAR(255),
    Price            
INT
);
INSERT #table
    
SELECT *
    
FROM   (
               
SELECT X.C.value('Id[1]''int'AS ChapterId,
                      X.C.value(
'Name[1]''varchar(255)'AS ChapterName,
                      X.C.value(
'Price[1]','int'AS Price
               
FROM   @ChapterXml.nodes('Chapter'AS X(C)    --注意:这里的X(C)命名空间是需要的
           ) t;    
    
INSERT INTO tbChapter(BookId,ChapterId,ChapterName,Price) 
    
SELECT @BookId,ChapterId,ChapterName,Price from #table;
END

其实,在存储过程里面可以把临时表去掉的。

然后我们执行下看看

执行存储过程

exec UP_Book_Insert 10000,'<Chapter><Id>268</Id><Name>第268章</Name><Price>100</Price></Chapter><Chapter><Id>273</Id><Name>第273章</Name><Price>100</Price></Chapter><Chapter><Id>275</Id><Name>第275章</Name><Price>100</Price></Chapter>'

怎么样?不错吧。只需要在存储过程里面对XML格式进行解析。

而在c#里面,XML格式可以传入DbType.String类型就可以了。

再写一个函数来生成XML格式的字符串

生成XML格式的函数

public static string FormatXmlInfo(List<ChapterInfo> list)
        {
            
if (list==null||list.Count<=0)
            {
                
return String.Empty;
            }
            StringBuilder sb = new StringBuilder();
            
foreach (ChapterInfo info in list)
            {
                sb.AppendFormat("<Chapter><Id>{0}</Id><Name>{1}</Name><Price>{2}</Price></Chapter>", info.ChapterId, info.ChapterName, info.Price);
            }
            
return sb.ToString();
        }

 好了,完成了。

性能具体怎么样,还没进行测试,但肯定的一点是,比多次请求数据库,或者在存储过程里面循环分割字符串效率要高。