creating indexing for SQL tunning
1. Not so long time ago, I got a report from customer. It's reported that they had a report getted very slow and finally throw an error. I finded fout the problem sql in source code and excuted it in sql managment studio. It costs 1 minute and 46 seconds to run this sql. Indeed, it's quite slowly.
orginal sql:
- SELECT distinct case when tblShopPayment.AccountID=0 then 1 else 0 end as Category, tblShopPayment.PaymentID, tblShopPayment.OperatorID, tblShopPayment.AccountID,
- tblShopPayment.PaymentCreateDate, tblShopPayment.PaymentModeID, tblShopPayment.FlagPaid, tblShopPaymentDetail.ProductName, tblShopPaymentDetail.Quantity, tblShopPaymentDetail.Price,
- vw_ShopAccountInfo.FirstName, vw_ShopAccountInfo.LastName, tblShopPaymentMode.PaymentModeName FROM tblShopPaymentMode INNER JOIN tblShopPayment INNER JOIN tblShopPaymentDetail
- ON tblShopPayment.PaymentID = tblShopPaymentDetail.PaymentID ON tblShopPaymentMode.PaymentModeID = tblShopPayment.PaymentModeID INNER JOIN TblShopProduct
- ON tblShopPaymentDetail.ProductID = TblShopProduct.ProductID INNER JOIN tblShopProductCategory ON TblShopProduct.CategoryID = tblShopProductCategory.ProductCategoryID
- LEFT OUTER JOIN vw_ShopAccountInfo INNER JOIN tblShopAccount ON vw_ShopAccountInfo.UniqueNo = tblShopAccount.UniqueNo ON tblShopPayment.AccountID = tblShopAccount.AccountID
- WHERE tblShopPayment.PaymentCreateDate >=convert(datetime,'9.1.2015',104) and tblShopPayment.PaymentCreateDate <=convert(datetime,'10.1.2015',104) and SysTypeID = 3 order by LastName
2. solving the problem
There are 2 solutions to solve the problem.
One way is rebuild the sql , One way is to create index.
I don't want to rebuild the sql. It's so complex and hard to rebuild.
So , I chose to create index.
After creating following indexes, the query reduce to 1 second. Yes, 1s.
--creating following index to improve performance
- --creating following index to improve performance
- --CREATE NONCLUSTERED INDEX [IXZY_tblShopPaymentDetail1]
- --ON [dbo].[tblShopPaymentDetail] ([PaymentID])
- --INCLUDE ([ProductID],[ProductName],[Quantity],[Price])
- --CREATE NONCLUSTERED INDEX [IXZY_1Students1]
- --ON [dbo].[Students] ([Enter_schooltime],[Leave_schooltime])
- --INCLUDE ([First_name],[Last_name],[UniqueNo])
- --CREATE NONCLUSTERED INDEX [IXZY_1Students12]
- --ON [dbo].[Students] ([UniqueNo],[Enter_schooltime],[Leave_schooltime])
- --INCLUDE ([First_name],[Last_name])
- --CREATE NONCLUSTERED INDEX [IXZY_STAFF1]
- --ON [dbo].[Staff] ([AttendStart],[AttendEnd])
- --INCLUDE ([FirstName],[LastName],[UniqueNO])
- --CREATE NONCLUSTERED INDEX [IXZY_Staff2]
- --ON [dbo].[Staff] ([UniqueNO],[AttendStart],[AttendEnd])
- --INCLUDE ([FirstName],[LastName])
- --CREATE NONCLUSTERED INDEX [IXZY_Parent1]
- --ON [dbo].[Parent] ([UniqueNo])
- --INCLUDE ([FirstName],[LastName],[Family_ID])
creating indexing for SQL tunning的更多相关文章
- SQL TUNNING
In a Nested Loops Join, for example, the first accessed table is called the outer table and the seco ...
- advisor调优工具优化sql(基于sql_id)
advisor调优工具优化sql(基于sql_id) 问题背景:客户反馈数据库迁移后cpu负载激增,帮忙查看原因 解决思路:1> 查看问题系统发现有大量的latch: cache buffers ...
- Indexed (materialized) views in SQL Server,different with Oracle (materialized) views
Thanks to MS sql could have materialized views ,similar with oracle MVs, using indexed views. what i ...
- P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1
P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1 May ...
- 为什么需要SQL Profile
为什么需要SQL Profile Why oracle need SQL Profiles,how it work and what are SQL Profiles... 使用DBMS_XPLAN. ...
- Oracle - PL/SQL Commands
第一章:日志管理 1.forcing log switches sql> alter system switch logfile; 2.forcing checkpoints sql> a ...
- ORACLE SQL TUNING ADVISOR 使用方法
sql tunning advisor 使用的主要步骤: 1 建立tunning task 2 执行task 3 显示tunning 结果 4 根据建议来运行相应的调优方法 下面来按照这个顺序来实施 ...
- SQL Server 2012 books
SQL Server 2012 Introducing Microsoft SQL Server 2012 Microsoft SQL Server 2012 High-Performance T-S ...
- Performance Tunning - OCP
This artical is forcused on Oracle 11g Release 2. It is an summary from the OCP documentation. The ...
随机推荐
- hdu 2473 并查集
思路:每次合并时,都是给一个虚拟的根. #include<map> #include<Set> #include<cmath> #include<queue& ...
- 初识 Asp.Net内置对象之Session对象
Session对象 Session对象用于存储在多个页面调用之间特定用户的信息.Session对象只针对单一网站使用者,不同的客户端无法相互访问.Session对象中止联机机器离现时,,也就是当网站使 ...
- missing required architecture x86_64 in file 不支持64位
( slices) 解决方法:
- MySQL分区表的使用
MySQL使用分区表的好处: 1,可以把一些归类的数据放在一个分区中,可以减少服务器检查数据的数量加快查询. 2,方便维护,通过删除分区来删除老的数据. 3,分区数据可以被分布到不同的物理位置,可以做 ...
- 每天一道LeetCode--374. Guess Number Higher or Lower
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
- Ubuntu14.04 Chromium 编译
1.下载depot_tools: 首先安装 git-core: sudo apt-get install git-core 执行命令: git clone https://chromium.googl ...
- JQuery处理json与ajax返回JSON实例
一.JSON的一些基础知识. JSON中对象通过“{}”来标识,一个“{}”代表一个对象,如{“AreaId”:”123”},对象的值是键值对的形式(key:value). “[]”,标识数组,数组内 ...
- 项目经理PPT演讲意见
1.语速 2.互动 3.平常语气,聊天的感觉去讲解 4.脱稿演讲,不要照着PPT读,PPT展示仅仅是一个重点提示,更多在于自己讲解 5.如果是验收等相关的内容,劲量多讲解用户能够得到的利益,如“钱” ...
- js 正则表达式 手机号
js--手机号验证 //注册验证 function RegsiterBtn() { var regphone = $("#regphone").val(); var regreal ...
- (转)RabbitMQ消息队列的小伙伴: ProtoBuf(Google Protocol Buffer)
什么是ProtoBuf? 一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化.它很适合做数据存储或 RPC 数据交换格式.可用于通讯协议.数据存储等领域的语言无关.平台无关.可扩 ...