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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - WinkSky

索引 This is a test post by Windows Live Writer [提问]升级至SQL2005后续问题讨论. [提问]SQL2005关于点对点事务型映射 李开复:21世纪7种人才最抢手 SQL2000中奇怪的NULL值记录问题[求助] SQL2005常见性能问题排错演示代码[收藏] 上海Oracle高峰会感悟 Microsoft® Visual Studio® .NET™ 2003 Service Pack 1 存储过程编写小工具[收藏] ASP.NET(C#) 編碼規範[整理讨论] 颜色代码表[转] Gmail的图片签名[收藏] 精妙SQL语句收集[转载] [转载]61条面向对象设计的经验原则 - WinkSky 需求工程16字方针 SQL2000视图问题[请教] 讨论String与string的区别. - WinkSky 一道终身受用的测试题
SQL各种写法的效率问题-转载自邹建专栏
WinkSky · 2006-07-21 · via 博客园 - WinkSky

问:

(1)一次插入多条数据时:

CREATE TABLE tb(ID int, 名称 NVARCHAR(30), 备注 NVARCHAR(1000))

INSERT tb   SELECT 1,'DDD',1

UNION  ALL        SELECT 1,'5100','D'

UNION  ALL        SELECT 1,'5200','E'

也可以这样:

CREATE TABLE tb1(ID int, 名称 NVARCHAR(30), 备注 NVARCHAR(1000))

INSERT TB1 (ID,名称,备注)VALUES(1,'DDD',1)

INSERT TB1 (ID,名称,备注)VALUES(1,'5100','D')

INSERT TB1 (ID,名称,备注)VALUES(1,'5200','E')

_________________________________

上面两种方法,哪种方法效率高?

答:

1种好一些, 但也得有个量的控制, 因为第1种的union all是作为一个语句整体, 查询优化器会尝试做优化, 同时, 也要先算出这个结果再插入的.

问:

(2)赋值时:

SELECT @a=N'aa'

SET @a=N'aa'

_________________________________

上面两种方法,哪种方法效率高?

答:

如果是单个赋值, 没有什么好比较的话.

不过, 如果是为多个变量赋值, 经测试, SELECT 一次性赋值, 比用SET 逐个赋值效率好..

问:

(3)取前几条数据时

set ROWCOUNT 2 select * from tb order by fd

select Top 2 * from tb order by fd

_________________________________

上面两种方法,哪种方法效率高?

答:

SET ROWCOUNTTOP 是一样的, 包括执行的计划等都是一样的

问:

(4)条件判断时

 where 0<(select count(*) from tb where ……)

 where exists(select * from tb where ……)

_________________________________

上面两种方法,哪种方法效率高?

答:

这个一般是exists, 当然, 具体还要看你后面的子查询的条件, 是否会引用外层查询中的对象的列.

exists检查到有值就返回, 而且不返回结果集, count需要统计出所有满足条件的, 再返回一个结果集, 所以一般情况下exists.

问:

(5)NULLIF的使用----->同理它的反函数ISNULL的使用

update tb set fd=case when fd=1 then null else fd end

update tb set fd=nullif(fd,1)

_________________________________

上面两种方法,哪种方法效率高?

答:

应该是一样的

问:

6)从字符串中取子字符串时

substring('abcdefg',1,3)

left('abcderg',3)_

________________________________

上面两种方法,哪种方法效率高?

答:

基本上是一样的

问:

(7)EXCEPTNot in的区别?

答:

except会去重复, not in 不会(除非你在select中显式指定)

except用于比较的列是所有列, 除非写子查询限制列, not in 没有这种情况

问:

(8)INTERSECTUNION的区别?

答:

intersect是两个查询都有的非重复值(交集), union是两个查询结果的所有不重复值(并集)

原文地址:http://blog.csdn.net/zjcxc/archive/2006/07/15/926622.aspx