数据库-培训-如何提高查询性能剖析

上传人:今*** 文档编号:105977515 上传时间:2019-10-14 格式:DOCX 页数:20 大小:291.23KB
返回 下载 相关 举报
数据库-培训-如何提高查询性能剖析_第1页
第1页 / 共20页
数据库-培训-如何提高查询性能剖析_第2页
第2页 / 共20页
数据库-培训-如何提高查询性能剖析_第3页
第3页 / 共20页
数据库-培训-如何提高查询性能剖析_第4页
第4页 / 共20页
数据库-培训-如何提高查询性能剖析_第5页
第5页 / 共20页
点击查看更多>>
资源描述

《数据库-培训-如何提高查询性能剖析》由会员分享,可在线阅读,更多相关《数据库-培训-如何提高查询性能剖析(20页珍藏版)》请在金锄头文库上搜索。

1、如何提高查询性能减少IO减少CPU运算使用内存1. 索引、提示、查询优化器和执行计划提示是指定的强制选项或策略,由 SQL Server 查询处理器针对 SELECT、INSERT、UPDATE 或 DELETE 语句执行。提示将覆盖查询优化器可能为查询选择的任何执行计划。因为 SQL Server 查询优化器通常会为查询选择最优执行计划,因此我们建议,只有在万般无奈的情况下才由经验丰富的开发人员和数据库管理员使用 、 和 。-创建表Users 并主键约束聚集索引Create table Users( UserID int identity, UserName nvarchar(50), Ag

2、e int, Gender bit, CreateTime datetime,constraint PK_UserID primary key clustered (UserID)-在UserName创建非聚集索引IX_UserNamecreate index IX_UserName on Users(UserName)-插入示例数据insert into Users(UserName,Age,Gender,CreateTime)select NBob,20,1,2012-5-1union allselect NJack,23,0,2012-5-2union allselect NRobert

3、,28,1,2012-5-3union allselect NJanet,40,0,2012-5-9union allselect NMichael,22,1,2012-5-2union allselect NLaura,16,1,2012-5-1union allselect NAnne,36,1,2012-5-7-查询1:select UserID,UserName from Users with(index(IX_UserName) where UserName=Robertselect UserID,UserName from Users where UserName=Robert-查

4、询2:select UserID,UserName,Age from Users with(index(IX_UserName) where UserName=Robertselect UserID,UserName,Age from Users where UserName=Robert-查询3select * from Users with(index(IX_UserName) where UserName=Robertselect * from users where UserName=Robert2. 如何书写高效SQL2.1. 需要多少检索多少select * from users;

5、(聚集索引中检索数据)select userid,username from users; (非聚集索引中检索数据,io少) 快2.2. where条件中不要使用 orwhere id=1 or id=5?Select 1 id Union allSelect 2 id?2.3. 避免使用 like %abcd%select top 10 UserID, CreateTimefrom Users where UserName like %abcd%select top 10 UserID,CreateTime from Users where UserName like abcd%select

6、 top 10 UserID, CreateTimefrom Users where UserName like %abcd% and CreateTime =2010-01-31 12:12:16.297 如不能避免,一定要在其它列上有范围的限定分词也个解决CONTAINS(names,免考*)2.4. 避免或简化order by索引列上order by建议在代码内部排序2.5. 改写 inselect userid,username from userswhere UserID in (select UserID from orderform where OrderTpye=5)selec

7、t a.userid,a.username from users a inner join orderform b on a.userid=b.useridwhere b. OrderTpye=5select a.userid,a.username from users a inner join (select UserID from orderform where OrderTpye=5) b on a.userid=b.useridselect a.userid,a.usernamefrom users a inner join orderform b on a.userid=b.user

8、id and b. OrderTpye=52.6. Where 子句在索引列上避免使用函数select COUNT(*)from orderform where DATEPART(YEAR,PostTime)=2010select COUNT(*)from orderform where DATEDIFF(YEAR,postTime,2010)=0select COUNT(*)from orderform where postTime=2010 and postTime 0 ) BEGIN SELECT TOP 1 code=ReturnsCode FROM #temp ORDER BY Au

9、ditTime ASC -select * from ReturnsDetail where ReturnsCode=code -DELETE FROM #temp WHERE ReturnsCode=code SET i=i-1 END 2.10. 合理使用临时表变量和临时表关于临时表和表变量的用法,需要注意:A 如果语句很复杂,连接太多,可以考虑用临时表和表变量分步完成,B 如果需要多次用到一个大表的同一部分数据,考虑用临时表和表变量暂存这部分数据,C 如果需要合并多个表的数据,形成一个结果,可以考虑用临时表和表变量分步汇总这多个表的数据,D 为了避免使用游标而使用关于临时表和表变量的选择

10、,很多说法是表变量在内存,速度快,应该首选表变量,但是在实际使用中发现,这个选择需要考虑放在临时表的数据量,在数据量较多的情况下,临时表的速度反而更快2.11. CTE (common_table_expression)“With table as ”指定临时命名的结果集,这些结果集称为公用表表达式 (CTE)。CTE 之后必须跟随引用部分或全部 CTE 列的单条 SELECT、INSERT、UPDATE 或 DELETE 语句。也可以在 CREATE VIEW 语句中将 CTE 指定为视图中 SELECT 定义语句的一部分公用表表达式可以包括对自身的引用。这种表达式称为递归公用表表达式。-

11、Create an Employee table. CREATE TABLE dbo.MyEmployees ( EmployeeID smallint NOT NULL, FirstName nvarchar(30) NOT NULL, LastName nvarchar(40) NOT NULL, Title nvarchar(50) NOT NULL, DeptID smallint NOT NULL, ManagerID int NULL, CONSTRAINT PK_EmployeeID PRIMARY KEY CLUSTERED (EmployeeID ASC) ); - Popu

12、late the table with values. INSERT INTO dbo.MyEmployees VALUES (1, NKen, NSnchez, NChief Executive Officer,16,NULL) ,(273, NBrian, NWelcker, NVice President of Sales,3,1) ,(274, NStephen, NJiang, NNorth American Sales Manager,3,273) ,(275, NMichael, NBlythe, NSales Representative,3,274) ,(276, NLinda, NMitchell, NSales Representative,3,274) ,(285, NSyed, NAbbas, NPacific Sales Manager,3,273) ,(286, NLynn, NTsoflias, NSales Representative,3,285) ,(16,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号