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

推荐订阅源

N
News and Events Feed by Topic
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Hugging Face - Blog
Hugging Face - Blog
S
SegmentFault 最新的问题
IT之家
IT之家
M
MIT News - Artificial intelligence
博客园_首页
aimingoo的专栏
aimingoo的专栏
C
Check Point Blog
B
Blog
人人都是产品经理
人人都是产品经理
爱范儿
爱范儿
宝玉的分享
宝玉的分享
Martin Fowler
Martin Fowler
L
LangChain Blog
Last Week in AI
Last Week in AI
Engineering at Meta
Engineering at Meta
Microsoft Security Blog
Microsoft Security Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
F
Fortinet All Blogs
罗磊的独立博客
博客园 - 叶小钗
H
Help Net Security
Google Online Security Blog
Google Online Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
The Last Watchdog
The Last Watchdog
S
Security @ Cisco Blogs
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
月光博客
月光博客
C
Cybersecurity and Infrastructure Security Agency CISA
博客园 - 司徒正美
S
Schneier on Security
V
Vulnerabilities – Threatpost
T
Troy Hunt's Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
博客园 - 【当耐特】
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Forbes - Security
Forbes - Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
雷峰网
雷峰网
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Secure Thoughts
V
Visual Studio Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
T
Tailwind CSS Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Hacker News: Ask HN
Hacker News: Ask HN
The GitHub Blog
The GitHub Blog

博客园 - 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条面向对象设计的经验原则 需求工程16字方针 SQL2000视图问题[请教] 讨论String与string的区别. 一道终身受用的测试题
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