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的更多相关文章

  1. SQL TUNNING

    In a Nested Loops Join, for example, the first accessed table is called the outer table and the seco ...

  2. advisor调优工具优化sql(基于sql_id)

    advisor调优工具优化sql(基于sql_id) 问题背景:客户反馈数据库迁移后cpu负载激增,帮忙查看原因 解决思路:1> 查看问题系统发现有大量的latch: cache buffers ...

  3. 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 ...

  4. 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 ...

  5. 为什么需要SQL Profile

    为什么需要SQL Profile Why oracle need SQL Profiles,how it work and what are SQL Profiles... 使用DBMS_XPLAN. ...

  6. Oracle - PL/SQL Commands

    第一章:日志管理 1.forcing log switches sql> alter system switch logfile; 2.forcing checkpoints sql> a ...

  7. ORACLE SQL TUNING ADVISOR 使用方法

    sql tunning advisor 使用的主要步骤: 1 建立tunning task 2 执行task 3 显示tunning 结果 4 根据建议来运行相应的调优方法  下面来按照这个顺序来实施 ...

  8. SQL Server 2012 books

    SQL Server 2012 Introducing Microsoft SQL Server 2012 Microsoft SQL Server 2012 High-Performance T-S ...

  9. Performance Tunning - OCP

    This artical is forcused on Oracle 11g Release 2.  It is an summary from the OCP documentation. The ...

随机推荐

  1. 转: HHVM at Baidu

    评注: 一个项目迁移的问题考虑与实现使用.非常之详细. 转:http://lamp.baidu.com/2014/11/04/hhvm-in-baidu/ 在这之前我们介绍了我们为什么要迁移PHP到H ...

  2. 转: Android入门及效率开发

    评注: android第三方开源框架介绍不错 转:https://segmentfault.com/a/1190000004495351 入门 Android官方培训课程中文版:http://huka ...

  3. 关于Css选择器优先级

    今天练习css的时候,重叠后的style发现不起作用,原来css选择器优先级大有文章. 声明: yi下内容选自 51cto.com  --加以自己的理解 以备日后参照使用,毕竟自己理解的才是自己的. ...

  4. Intent.ACTION_PICK

    在常见的Activity Action Intent常量中,ACTION_PICK  android.intent.action.PICK 是“选择数据”的意思,来简单的分享一下我知道的Intent. ...

  5. Cocos2d-JS使用CocosDenshion引擎

    Cocos2d-JS提供了一个音频CocosDenshion引擎.具体使用的API是cc.AudioEngine.cc.AudioEngine有几个常用的函数:playMusic(url, loop) ...

  6. ASP.NET MVC自定义路由 - 实现IRouteConstraint限制控制器名(转载)

    自定义约束前 namespace MvcApplication2 { public class RouteConfig { public static void RegisterRoutes(Rout ...

  7. FKP,一套全栈框架,基于react、webpack、koa1、babel

    FKP-REST是一套前后端分离,基于javascript的全栈实现,基于node的高性能,易部署性及javascript前后端语言的一致性,学习成本,时间成本及项目快速启动等等方面,FKP都是一种不 ...

  8. 使用Linux系统中的SSH服务

    使用Linux系统中的SSH服务 1.SSH服务应用场景 ① 可以实现对文件的上传与下载 ② 实现远程管理Linux 2.安装SSH服务器 服 务:sshd 位 置:光盘2 软 件:openssh-s ...

  9. 杭电ACM2092--整数解

    杭电ACM2092--整数解    分析 http://acm.hdu.edu.cn/showproblem.php?pid=2092 一个YES,一个Yes.试了10几次..我也是无语了..哪里都不 ...

  10. RSS 订阅

    <?xml version="1.0"?><%@ Page Language="C#" AutoEventWireup="true& ...