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

推荐订阅源

Help Net Security
Help Net Security
G
Google Developers Blog
雷峰网
雷峰网
WordPress大学
WordPress大学
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Engineering at Meta
Engineering at Meta
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
F
Full Disclosure
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
V
V2EX
C
Cisco Blogs
博客园 - 司徒正美
Project Zero
Project Zero
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
Scott Helme
Scott Helme
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
S
Securelist
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
T
Threatpost
Recorded Future
Recorded Future
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
The Register - Security
The Register - Security
S
Security Archives - TechRepublic
博客园 - Franky
N
News | PayPal Newsroom
Simon Willison's Weblog
Simon Willison's Weblog
S
SegmentFault 最新的问题
W
WeLiveSecurity
A
Arctic Wolf
B
Blog

博客园 - lsyyx

SQL Server 2008中SQL增强之三:Merge(在一条语句中使用Insert,Update,Delete) SQL Server 2008中SQL增强之一:Values新用途 SQL Server 2008中增强的汇总技巧 需要弥补的那部分SQL oracle递归查询\sqlserver递归 - lsyyx - 博客园 如何写SQL,计算上下记录同一字段相差值 SQL语句整理资料 Sqlserver Update 用法 可判断datatable的列是否有重复 Oracle:ODP.NET Managed 小试牛刀 (转) Asp.Net4.0/VS2010新变化(4):SEO的改进 Asp.Net4.0/VS2010新变化(2):网站自动预热 Asp.Net4.0/VS2010新变化(3):webform中也可以直接url路由 NET4.0中非常好用的一个东西Tuple 关于.net4.0中的Action委托 NET4.0多线程编程---Tasks .NET4.0多线程编程---Cooperative Cancellation net4.0新特性之线程同步 C#4.0语言新功能及应用 (1)
SQL Server 2008中SQL增强之二:Top新用途
lsyyx · 2015-01-13 · via 博客园 - lsyyx

一、TOP替代Set RowCount

在SQL Server 2005之前的传统SQL语句中,top语句是不支持局部变量的。见http://www.cnblogs.com/downmoon/archive/2007/12/29/1019686.html

此时可以使用Set RowCount,但是在SQL Server 2005/2008中,TOP通常执行得更快,所以应该用TOP关键字来取代Set RowCount。

复制代码

/***************创建测试表********************* ****************downmoo 3w@live.cn ***************/
IF NOT OBJECT_ID('[Demo_Top]') IS NULL DROP TABLE [Demo_Top] GO Create table [Demo_Top] (PID int identity(1,1) primary key not null ,PName nvarchar(100) null ,AddTime dateTime null ,PGuid Nvarchar(40) ) go
truncate table [Demo_Top]
/***************创建1002条测试数据********************* ****************downmoo 3w@live.cn ***************/
declare @d datetime set @d=getdate()
declare @i int set @i=1 while @i<=1002 begin insert into [Demo_Top] select cast(datepart(ms,getdate()) as nvarchar(3))+Replicate('A',datepart(ss,getdate())) ,getdate() ,NewID() set @i=@i+1 end

复制代码

--注意TOP关键字可以用于Select,Update和Delete语句中

Declare @percentage float set @percentage=1 select Top (@percentage) percent PName from [Demo_Top] order by PName --注意是11行。(11 row(s) affected)

邀月注:如果只是需要一些样本,也可以使用TableSample,以下语句返回表Demo_Top的一定百分比的随机行

select PName,AddTime, PGuid from [Demo_Top] TableSample System(10 percent) --(77 row(s) affected)

注意这个百分比是表数据页的百分比,而不是记录数的百分比,因此记录数目是不确定的。

二、TOP分块修改数据

TOP的第二个关键改进是支持数据的分块操作。换句话说,避免在一个语句中执行非常大的操作,而把修改分成多个小块,这大大改善了大数据量、大访问量的表的并发性,可以用于大的报表或数据仓库应用程序。此外,分块操作可以避免日志的快速增长,因为前一操作完成后,可能会重用日志空间。如果操作中有事务,已经完成的修改数据已经可以用于查询,而不必等待所有的修改完成。

仍以上表为例:

复制代码

while (select count(1) from [Demo_Top])>0 begin delete top (202) from [Demo_Top] end
/* (202 row(s) affected)
(202 row(s) affected)
(202 row(s) affected)
(202 row(s) affected)
(194 row(s) affected) */

复制代码

注意是每批删除202条数据,TOP也可以用于Select和Update语句,其中后者更为实用。

--Select TOP(100) --Update TOP(100)