www.leccotech.com Where the experts go for performance DM10:Sybase SQL Expert 12.5 DM10:Sybase SQL Expert 12.5 新 新新新 一 新 新新新 一 SQL SQL 新新新新新新 新新新新新新 The New Generation of Intelligent SQL Tuning The New Generation of Intelligent SQL Tuning Technology Technology 新新新 ( Richard To) [email protected]新新新新 新新新新新新新新
46
Embed
杜伟业 ( Richard To) Richardto@leccotech 技术总监 灵高科研有限公司
DM10:Sybase SQL Expert 12.5 新一代智能 SQL 语句优化技术 The New Generation of Intelligent SQL Tuning Technology. 杜伟业 ( Richard To) [email protected] 技术总监 灵高科研有限公司. 什么是反应时间. 1/10秒是用户认为系统能够立即反应的极限. 1 秒是用户觉得没有被中断的极限. 10秒是用户能将注意力继续集中在与计算机的对话上的时间极限. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
www.leccotech.com Where the experts go for performance
www.leccotech.com Where the experts go for performance
服务器收到 SQL语句SQL语句解析
数据库的优化器决定执行路径
变量赋值执行
数据库是如何处理 SQL 语句的 ?
www.leccotech.com Where the experts go for performance
SQL
数据库的优化器是如何工作的 ?内部重写并产生多种执行计划
执行计划1执行计划2执行计划3成本估计
执行计划 1 成本 =1000执行计划 2 成本 =3000执行计划 3 成本 = 500
执行
它是否尝试了所有的的途径来重写你的 SQL语句 ?
成本的估计有多精确 ?
www.leccotech.com Where the experts go for performance
优化器说明什么是执行计划SQL 语法如何影响优化器的决定
www.leccotech.com Where the experts go for performance
什么是 SQL 优化 ?
www.leccotech.com Where the experts go for performance
解决这个问题的传统途径
www.leccotech.com Where the experts go for performance
执行计划SET SHOWPLAN ONGO
SELECT * FROM EMPLOYEE WHERE EMP_ID > 73712GO
SET SHOWPLAN OFFGO
QUERY PLAN FOR STATEMENT 1 (at line 1).
STEP 1 The type of query is SELECT.
FROM TABLE EMPLOYEE Nested iteration. Table Scan. Forward scan. Positioning at start of table. Using I/O Size 2 Kbytes for data pages. With LRU Buffer Replacement Strategy for data pages.
Total estimated I/O cost for statement 1 (at line 1): 20600.
iSQL
www.leccotech.com Where the experts go for performance
IO 和时间统计信息SET STATISTICS IO ONGOSET STATISTICS TIME ONGO
SELECT * FROM EMPLOYEE WHERE EMP_ID > 73712GO
SET STATISTICS IO OFFGOSET STATISTICS TIME OFFGO
iSQL Server Message: Number 3631, Severity 10Line 1:Total actual I/O cost for this command: 0. Total writes for this command: 0 Server Message: Number 3631, Severity 10Line 1:Total actual I/O cost for this command: 0. Total writes for this command: 0 Execution Time 0. SQL Server cpu time: 0 ms. SQL Server elapsed time: 3614 ms.Parse and Compile Time 0. SQL Server cpu time: 0 ms.Table: EMPLOYEE scan count 1, logical reads: (regular=1030 apf=0 total=1030), physical reads: (regular=0 apf=0 total=0), apf IOs used=0 Server Message: Number 3631, Severity 10Line 1:Total actual I/O cost for this command: 2060. Total writes for this command: 0 Execution Time 16. SQL Server cpu time: 1600 ms. SQL Server elapsed time: 7730 ms.(18371 rows affected)Parse and Compile Time 0. SQL Server cpu time: 0 ms. Execution Time 0. SQL Server cpu time: 0 ms. SQL Server elapsed time: 0 ms.Parse and Compile Time 0. SQL Server cpu time: 0 ms.
www.leccotech.com Where the experts go for performance
Traceon
DBCC TRACEON (3604, 302, 310)GO
SELECT * FROM EMPLOYEE WHERE EMP_ID > 73712GO
iSQL Beginning selection of qualifying indexes for table 'EMPLOYEE',varno = 0, objectid 1593824790. The table (Allpages) has 18373 rows, 1030 pages, Data Page Cluster Ratio 0.998890
Table scan cost is 18373 rows, 1030 pages, using no data prefetch (size 2K I/O), in data cache 'default data cache' (cacheid 0) with LRU replacement
Selecting best index for the SEARCH CLAUSE:EMPLOYEE.EMP_ID > 73712.000000
Estimated selectivity for EMP_ID, selectivity = 0.999946.
Estimating selectivity of index 'EMPLOYEE_1961957491', indid 1 scan selectivity 0.999946, filter selectivity 0.999946 18372 rows, 1032 pages, index height 2, Data Row Cluster Ratio 1.000000, Index Page Cluster Ratio 0.000000, Data Page Cluster Ratio 0.998890
…..
www.leccotech.com Where the experts go for performance
使用 EXIST 还是 IN ?
SELECT * FROM A WHERE A.CITY IN (SELECT B.CITY FROM B)
SELECT * FROM A WHERE EXISTS (SELECT ‘X’ FROM B WHERE B.CITY = A.CITY)
EXIST or IN
www.leccotech.com Where the experts go for performance
表连接与子查询通情况下表连接 (Table join) 要好过子查询 (sub-query)
源语句 SELECT * FROM A WHERE A.CITY IN (SELECT B.CITY FROM B)重写后
SELECT A.* FROM A, B
WHERE A.CITY = B.CITY /* if B.CITY is unique in B */
www.leccotech.com Where the experts go for performance
如果你确实知道问题所在,可以强制使用一个特殊的表操纵路径( driving path ) 。源语句SELECT * FROM A, B WHERE A.STATE = B.STATE
重写后的语句/* 禁止使用 B 的索引来改变表操纵路径 (B drive A) */SELECT * FROM A, B WHERE A.STATE = ISNULL(B.STATE,B.STATE)
Joins — Change Table Driving Path
www.leccotech.com Where the experts go for performance
要使用索引就不能在该索引字段上进行操作源语句 /* A.key1 上的索引被使用 */SELECT * FROM A WHERE A.Key1 = @ver1 AND A.Key2 = @ver2
重写后 /* 禁止使用字段 A.key1 上的索引,使用字段 A.Key2 上的索引 */SELECT * FROM A WHERE ISNULL(A.Key1, A.key1) = @ver1 AND A.Key2 = @ver2
Use Indexes
www.leccotech.com Where the experts go for performance
www.leccotech.com Where the experts go for performance
使用 FORCEPLAN 强制选项强制使用特殊的表操纵路径 (driving path) 。
原来的方法 /* 禁止使用 B 的索引来改变表操纵路径 (B drive A)*/SELECT * FROM A, B WHERE A.STATE = ISNULL(B.STATE,B.STATE)
用 FORCEPLAN重写后SET FORCEPLAN ONGOSELECT * FROM B, A WHERE A.STATE = B.STATEGO
Joins — Change Table Driving Path
www.leccotech.com Where the experts go for performance
强制索引 (Force Index)
SELECT * FROM EMPLOYEE WHERE EMP_ID > 73712
Force Index
备注:仅在事先知道使用使用索引 x 将会较快的情况下采用
(INDEX EMP_ID_INX) QUERY PLAN FOR STATEMENT 1 (at line 1).
STEP 1 The type of query is SELECT.
FROM TABLE EMPLOYEE Nested iteration. Table Scan. Forward scan. Positioning at start of table. Using I/O Size 2 Kbytes for data pages. With LRU Buffer Replacement Strategy for data pages.
Total estimated I/O cost for statement 1 (at line 1): 20600.
QUERY PLAN FOR STATEMENT 1 (at line 1).
STEP 1 The type of query is SELECT.
FROM TABLE EMPLOYEE Nested iteration. Index : EMP_ID_INX Forward scan. Positioning by key. Keys are: EMP_ID ASC Using I/O Size 2 Kbytes for index leaf pages. With LRU Buffer Replacement Strategy for index leaf pages. Using I/O Size 2 Kbytes for data pages. With LRU Buffer Replacement Strategy for data pages.
Total estimated I/O cost for statement 1 (at line 1): 23382.
www.leccotech.com Where the experts go for performance
变量使用上的普遍问题SELECT * FROM employee WHERE (emp_id > @range_lower OR @range_lower IS NULL) AND (emp_id < @range_upper OR @range_upper IS NULL)
使用配合输入范围边界的索引范围搜索 (index range search)
SELECT * FROM employee WHERE emp_id > ISNULL(@range_lower , min of emp_id) AND emp_id < ISNULL(@range_upper, max of emp_id)
www.leccotech.com Where the experts go for performance
SELECT * FROM A WHERE EXISTS (SELECT ‘x’
FROM B WHERE A.key = B.key)
SELECT * FROM A a1 WHERE EXISTS IN (SELECT ‘x’ FROM A a2, B
WHERE a1.key = B.key AND a1.Unique_Key = a2.Unique_Key)
复杂的 SQL 转化
语义上等同于 :
www.leccotech.com Where the experts go for performance
这条 SQL 语句有多少个不同的等价写法 ?
SELECT * FROM DEPARTMENT WHERE DPT_ID IN (SELECT EMP_DEPT FROM EMPLOYEE WHERE EMP_ID > 50000)
提交给数据库的 SQL语句会跟已存于数据库内的 SQL语句进行比较,如果匹配,存于数据库的 abstract plan 将会用来执行该查询。
www.leccotech.com Where the experts go for performance
Abstract Plan
优化器优化器 执行基于保存的
abstract plan 来产生执行计划
基于 数据库统计信息 来产生执行计划SQL
Sybase
www.leccotech.com Where the experts go for performance
使用 Abstract Plan- /* 创建 abstract plan 组 demo1 */- sp_add_qpgroup demo1- Go- /* 保存 abstract plan 到组 demo1 */- set plan dump demo1 on- Go- /* 执行 SQL */- Select * from A, B
- /* 从组 demo1 加载计划 (Plan) */- set plan load demo1 on- Go
- Execution Plan /* 下一次你重新执行同一条 SQL 语句 */- QUERY PLAN FOR STATEMENT 1 (at line 1).- Optimized using an Abstract Plan (ID : 1989579095).
www.leccotech.com Where the experts go for performance