3

我为一堆不同的报告创建了一系列视图。它们相互建立最终视图,返回少于 5k 条记录。当我打开这个视图时,运行不到一秒钟。如果我添加一个WHERE collumn IS NOT NULL,则需要 2 分钟以上。虽然这对于我不需要经常运行的报告来说很好,但还有其他一些我必须每周、每天甚至更频繁地运行。

我的解决方案是创建一个存储过程,从视图的数据中创建一个表,并对其进行查询。我将存储过程设置为在计划的报告自动触发之前运行。这工作正常,但每次我想获得更新的报告时,我都必须手动运行存储过程。

有没有更好的办法?

查看 [MW_Submitted_ROY] 以查找已提交周财务。此视图将活动表与静态表中的历史数据相结合。我对静态表中的数据进行了按摩,使其与活动表中的数据格式相同。

SELECT     oe_po_no, cus_no, user_def_fld_1, user_def_fld_2, tot_sls_amt
FROM         MW_Submitted_Weeks_OldMacola
WHERE     MW_Submitted_Weeks_OldMacola.oe_po_no LIKE 'adv%'
UNION
SELECT     oe_po_no, cus_no, user_def_fld_1, user_def_fld_2, tot_sls_amt
FROM         oehdrhst_sql
WHERE     oehdrhst_sql.oe_po_no LIKE 'adv%'

查看 [MW_Company_Weeks] 以生成每家公司将提交当前和上一年财务数据的所有可能周数。

SELECT     MW.MW_Weeks.Year, MW.MW_Weeks.Week, MW.MW_CompanyCodes.cmp_code
FROM         MW.MW_Weeks CROSS JOIN
                  MW.MW_CompanyCodes
 WHERE     (MW.MW_Weeks.WEDate <= GETDATE()) AND (MW.MW_Weeks.Year > YEAR(GETDATE()) - 2)

查看 [MW_Submitted_Weeks_By_RBC_ROY] 结合了我需要生成报告的公司的更多信息

 SELECT     dbo.cicmpy.textfield2 AS [Group], dbo.cicmpy.cmp_code, dbo.cicmpy.cmp_name,          dbo.cicmpy.cmp_fadd3 AS Owner, dbo.cicmpy.SalesPersonNumber, 
                  MW.MW_Submitted_ROY.user_def_fld_1 AS Year, MW.MW_Submitted_ROY.user_def_fld_2 AS Week, MW.MW_Submitted_ROY.oe_po_no AS Description, 
                  MW.MW_Submitted_ROY.tot_sls_amt
 FROM         dbo.cicmpy INNER JOIN
                  MW.MW_Submitted_ROY ON dbo.cicmpy.cmp_code = MW.MW_Submitted_ROY.cus_no
 WHERE     (dbo.cicmpy.cmp_code LIKE 'C%') AND (dbo.cicmpy.cmp_status = 'a') AND (dbo.cicmpy.cmp_type = 'C')

这将我们带到最终视图 [MW_Missing_Weeks],它向我显示缺少哪些周,以及上一年提交的财务数据的价值。这就是添加“不为空”的地方,这需要永远。

 SELECT     MW.MW_Company_Weeks.Year, MW.MW_Company_Weeks.Week, MW.MW_Company_Weeks.cmp_code, bPrev.tot_sls_amt, bPrev.[Group], bPrev.cmp_name, bPrev.Owner, 
                  bPrev.SalesPersonNumber, bPrev.Description
 FROM         MW.MW_Company_Weeks LEFT OUTER JOIN
                  MW.MW_Submitted_Weeks_By_RBC_ROY ON MW.MW_Company_Weeks.cmp_code = MW.MW_Submitted_Weeks_By_RBC_ROY.cmp_code AND 
                  MW.MW_Company_Weeks.Year = MW.MW_Submitted_Weeks_By_RBC_ROY.Year AND MW.MW_Company_Weeks.Week = MW.MW_Submitted_Weeks_By_RBC_ROY.Week LEFT OUTER JOIN
                  MW.MW_Submitted_Weeks_By_RBC_ROY AS bPrev ON MW.MW_Company_Weeks.cmp_code = bPrev.cmp_code AND MW.MW_Company_Weeks.Year = bPrev.Year + 1 AND 
                  MW.MW_Company_Weeks.Week = bPrev.Week
 WHERE     (MW.MW_Submitted_Weeks_By_RBC_ROY.cmp_code IS NULL) AND bPrev.tot_sls_amt IS NOT NULL
4

1 回答 1

7

In SSMS go to Query > Display Estimated Execution Plan, or hit Ctrl + L. Find the part of the plan that takes the highest percentage and see if there is "Missing Index" in green. Right click and choose "Missing Index Details." That will create the code to make the index. You just need to add the index name.

It will look something like this:

/*
USE [Your Database]
GO
CREATE NONCLUSTERED INDEX [<Name of Missing Index, sysname,>]
ON [dbo].[YourTable] ([ColA])
INCLUDE ([ColB],[ColC])
GO
*/

That is a quick and dirty way to create an index.

You can use the Query Execution Plan to troubleshoot of issues too. It'll take some practice.

于 2013-10-02T18:16:50.033 回答