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

推荐订阅源

博客园_首页
C
Check Point Blog
B
Blog RSS Feed
G
Google Developers Blog
H
Help Net Security
博客园 - Franky
Blog — PlanetScale
Blog — PlanetScale
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
Recent Announcements
Recent Announcements
B
Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
DataBreaches.Net
小众软件
小众软件
T
The Blog of Author Tim Ferriss
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
Y
Y Combinator Blog
T
Tailwind CSS Blog
J
Java Code Geeks
MyScale Blog
MyScale Blog
雷峰网
雷峰网
有赞技术团队
有赞技术团队
博客园 - 聂微东

博客园 - 秋雨飘飘

显示日期的javascript日历 Iframe的基础应用——关于Iframe刷页问题的两种方法 关于left join的讨论 往表中允许为null的字段插入null值 - 秋雨飘飘 - 博客园 sql查询最大最小值 SQL Server CHARINDEX和PATINDEX详解 sql行列转换 存储过程的作用是自动生成编号 数据库管理常用SQL SQL Server 中多行多列连接成为单行单列 T-SQL 编程规范和优化技巧 (转)SQL Server 索引结构及其使用 动态查询SQL中变量的返回值 存储过程的编写经验 求薪水第三高的雇员的SQL题(图解) 表连接的比较left join/right join/inner join sql 视图 sql 日期函数之对于周的处理 如何提升checkbox的用户体验
sql语句优化的一些测试函数
秋雨飘飘 · 2007-07-20 · via 博客园 - 秋雨飘飘

Posted on 2007-07-20 08:02  秋雨飘飘  阅读(391)  评论(0)    收藏  举报

--Demo 1: 
use northwind 
go 
set statistics IO on 
go 
select count(*from northwind.dbo.employees 
go 
set statistics IO off 
go 

use northwind 
go 
exec sp_spaceused employees 
go 

--Demo 2: 
set statistics time on 
go 
select count(*from northwind.dbo.employees 
go 
set statistics time off 
go 

--Demo 3: 
set showplan_text on 
go 
select count(*from northwind.dbo.employees 
go 
set showplan_text off 
go 

--Demo 4: 
set nocount on 
go 
select count(*from northwind.dbo.employees 
go 
set nocount off 
go 


--Demo 5查询单条sql语句的执行时间: 
declare @start_time datetime 
select @start_time=getdate() 
select * from northwind.dbo.employees 
select '查询语句的执行时间(毫秒)'=datediff(ms,@start_time,getdate()) 

--Demo 6查询成批的sql语句的执行时间: 
create table #save_time(start_time datetime not null
insert #save_time values(getdate()) 
go 
select * from employees 
go 
select * from orders 
go 
select '查询语句的执行时间(毫秒)'=datediff(ms,start_time,getdate()) 
from #save_time 
drop table #save_time 
go 

--Demo 7返回语句的执行计划内容: 
set showplan_all on 
go 
select * from pubs.dbo.authors 
go 
set showplan_all off 
go 

--Demo 8从执行计划判断是否需要优化SQL: 
/**//*SEEK操作*/ 
set showplan_all on 
go 
select * from pubs.dbo.sales where stor_id>='7131' 
go 
set showplan_all off 
go 
/**//*SCAN操作*/ 
set showplan_all on 
go 
select * from pubs.dbo.sales where ord_date is not null 
go 
set showplan_all off 
go 

--Demo 9连接查询VS子查询: 
/**//*子查询*/ 
set statistics io on 
go 
select au_fname,au_lname from pubs.dbo.authors where au_id in 
(
select au_id from pubs.dbo.titleauthor) 
set statistics io off 
go 


/**//*连接查询*/ 
set statistics io on 
go 
select distinct au_fname,au_lname from pubs.dbo.authors as a inner join 
pubs.dbo.titleauthor 
as t on a.au_id=t.au_id 
go 
set statistics io off 
go 

--Demo 10智能优化: 
select p1.productname from northwind.dbo.products as p1 inner join 
northwind.dbo.products 
as p2 on (p1.unitprice=p2.unitprice) 
where p2.productname like 'Alice%' 

刷新页面返回顶部