- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以,我继承了某人的产品,虽然我查看了很多代码,但我觉得其中很多都可以改进。我的首要任务是优化以下存储过程,我希望有人能让我走上正轨。虽然我很菜,但我不禁觉得必须有更好的方法来做到这一点……运行需要4分钟以上。
在 sproc 中,多次进行相同的连接。我真的不是在要求某人做我的工作,但请有人给我一个关于如何更好地构建以下内容的开始吗?:
我应该创建一个临时表而不是执行这么多嵌套连接吗?
谢谢,
BEGIN
DECLARE @District VARCHAR(50)
SET @District = '42'
SET NOCOUNT ON;
DECLARE @today varchar(30)
DECLARE @ToDatestr varchar(20)
DECLARE @ToDate15 varchar(20)
DECLARE @BOYear varchar(30)
DECLARE @BOMonth varchar(30)
DECLARE @BOWeek varchar(30)
SET @today = RIGHT('00'+CAST(MONTH(getdate()) as varchar), 2) + '/' + RIGHT('00'+CAST(DAY(getdate()) as varchar), 2) + '/' + CAST(YEAR(getdate()) as varchar)
SELECT d.utilitydistrictnumber AS "District #",
emr.ExistingMeterID,
emr.isvc AS "ISVC #",
r.Utilityrouteid AS "Utility Route #",
emr.cyclenumber AS "Utility Cycle #",
pd."Name",
REPLACE(REPLACE(pd."Address",CHAR(10),''),',',';') AS 'Address',
CONVERT(float,(CASE WHEN ISNULL(p.Latitude,'.000000') = '.000000' THEN dw_p.Lat ELSE p.Latitude END)) AS 'Latitude',
CONVERT(float,(CASE WHEN ISNULL(p.Longitude,'.000000') = '.000000' THEN dw_p.Long ELSE p.Longitude END)) AS 'Longitude',
WeekendCustContact.mCount AS 'Weekend CustContact',
After5PMCustContact.mCount AS 'After 5PM CustContact',
TotalCustContact.mCount AS 'Total CustContact',
AppointmentArranged.mCount AS 'Appointment Arranged',
FieldUTC.mCount AS 'Total FieldUTCs',
Letters.TotalHTALetter ,
emr.UtilityOnHold,
emr.DeploymentOnHold,
emr.DeploymentOnHoldReason,
,o.ActivityName
From Product_CompanyProd_Repository.dbo.Existingmetersroutes emr (NOLOCK)
INNER JOIN
Product_CompanyProd_Repository.dbo.ExistingmetersPremises emp (NOLOCK)
ON emp.existingmeterid = emr.existingmeterid
INNER JOIN
Product_CompanyProd_Repository.dbo.Premises p (NOLOCK)
ON p.premiseid = emp.premiseid
LEFT JOIN
[ProductMAIN-ALIAS].[DW_Company].[dbo].[Premise_LatLongs] dw_p (NOLOCK)
ON dw_p.premiseid = p.premiseid
INNER JOIN
[Product_CompanyPROD_Repository].[dbo].[routes] AS r (NOLOCK)
ON r.routeid = emr.routeid
INNER JOIN
[Product_CompanyPROD_Repository].[dbo].[Districts] AS d (NOLOCK)
ON d.districtid = r.districtid AND d.utilitydistrictnumber = @District
LEFT JOIN [Product_CompanyProd].[dbo].[ODMorders] o
ON o.summary = emr.isvc AND o.StatusID < 9
LEFT JOIN
(SELECT oo.Summary AS ISVC, COUNT(*) AS mcount
FROM Product_CompanyProd.dbo.ODMOrders AS oo (NOLOCK)
INNER JOIN
Product_CompanyProd.dbo.ODMOrderAttributesUME AS oa (NOLOCK)
ON oa.Orderid = oo.Orderid AND oa.UTCCode <> ''
AND oa.district = @District
WHERE oo.StatusID IN (9,10)
GROUP BY oo.summary
) AS FieldUTC ON FieldUTC.isvc=emr.isvc
LEFT JOIN
(SELECT e.isvc, COUNT(*) AS mcount
FROM [Product_CompanyProd_Repository].[dbo].[existingmetersroutes] e (NOLOCK)
INNER JOIN
[Product_CompanyProd_Repository].[dbo].[existingmeterspremises] p (NOLOCK)
on e.existingmeterid = p.existingmeterid
INNER JOIN
[Product_CompanyProd_Repository].[dbo].[premisenotes] pn (NOLOCK)
on pn.premiseid = p.premiseid
AND DATEPART(dw, pn.autotimestamp) IN (7,1)
WHERE category = 'Call attempt'
GROUP BY e.isvc
) AS WeekendCustContact ON WeekendCustContact.isvc=emr.isvc
LEFT JOIN
(SELECT e.isvc, COUNT(*) AS mcount
FROM [Product_CompanyProd_Repository].[dbo].[existingmetersroutes] e (NOLOCK)
INNER JOIN
[Product_CompanyProd_Repository].[dbo].[existingmeterspremises] p (NOLOCK)
on e.existingmeterid = p.existingmeterid
INNER JOIN
[Product_CompanyProd_Repository].[dbo].[premisenotes] pn (NOLOCK)
on pn.premiseid = p.premiseid
AND datepart(hh,pn.autotimestamp) >= 17
WHERE category = 'Call attempt'
GROUP BY e.isvc
) AS "After5PMCustContact" ON After5PMCustContact.isvc=emr.isvc
LEFT JOIN
(SELECT e.isvc, COUNT(*) AS mcount
FROM [Product_CompanyProd_Repository].[dbo].[existingmetersroutes] e (NOLOCK)
INNER JOIN
[Product_CompanyProd_Repository].[dbo].[existingmeterspremises] p (NOLOCK)
on e.existingmeterid = p.existingmeterid
INNER JOIN
[Product_CompanyProd_Repository].[dbo].[premisenotes] pn (NOLOCK)
on pn.premiseid = p.premiseid
WHERE category IN ('Call attempt','Door hanger','Letter received by customer','Call to Company who referred the caller to OurCompany')
GROUP BY e.isvc
) AS "TotalCustContact" ON TotalCustContact.isvc=emr.isvc
LEFT JOIN
(SELECT oo.Summary AS ISVC, COUNT(*) AS mcount
FROM Product_CompanyProd.dbo.ODMOrders AS oo (NOLOCK)
WHERE oo.ActivityName = 'CompanyExchangeAppt' AND oo.StatusID < 9
GROUP BY oo.summary
) AS "AppointmentArranged" ON AppointmentArranged.isvc=emr.isvc
LEFT JOIN
(SELECT emr.ISVC,ema.ColumnValue AS "TotalHTALetter"
FROM
Product_CompanyProd_Repository.dbo.Existingmetersroutes emr (NOLOCK)
INNER JOIN
Product_CompanyProd_Repository.dbo.ExistingmetersAuxiliary ema (NOLOCK) on ema.existingmeterid = emr.existingmeterid
AND ema.ColumnName LIKE 'HTALetter%'
) AS "Letters" ON Letters.isvc=emr.isvc
LEFT JOIN
(SELECT * FROM
( SELECT o.summary AS isvc,
REPLACE(REPLACE([od].Information.query('data(OrderDetails/PremiseDetails/Name)').value('.','varchar(50)'),CHAR(10),''),',',';') AS "Name",
REPLACE(REPLACE([od].Information.query('data(OrderDetails/Location/StreetAddress)').value('.','varchar(50)'),CHAR(10),''),',',';') AS "Address",
[od].Information.query('data(OrderDetails/PremiseDetails/Phone)').value('.','varchar(50)') AS "Phone",
o.Autotimestamp
From Product_CompanyProd.dbo.ODMOrders AS o (NOLOCK)
INNER JOIN
Product_CompanyProd.dbo.ODMOrderAttributesUME AS oa (NOLOCK)
ON oa.Orderid = o.Orderid AND oa.district = @District AND oa.UTCCode <> ''
INNER JOIN
[Product_CompanyProd].[dbo].[ODMOrderdetails] od (NOLOCK)
ON od.Orderid = o.Orderid
WHERE o.StatusID IN (9,10)
) AS pd
WHERE
pd.Autotimestamp=(SELECT MAX(o.autotimestamp)
From Product_CompanyProd.dbo.ODMOrders AS o (NOLOCK)
INNER JOIN
Product_CompanyProd.dbo.ODMOrderAttributesUME AS oa (NOLOCK)
ON oa.Orderid = o.Orderid AND oa.district = @District
INNER JOIN
[Product_CompanyProd].[dbo].[ODMOrderdetails] od (NOLOCK)
ON od.Orderid = o.Orderid
WHERE o.summary = pd.isvc AND
o.StatusID IN (9,10)
)
) AS pd ON pd.isvc = emr.isvc
Where
emr.Status NOT IN ('Complete','Fieldcomplete','UTC')
END
最佳答案
这 3 个子查询可以组合在一起:
...
LEFT JOIN
(SELECT e.isvc, COUNT(*) AS mcount
FROM [Product_CompanyProd_Repository].[dbo].[existingmetersroutes] e (NOLOCK)
INNER JOIN
[Product_CompanyProd_Repository].[dbo].[existingmeterspremises] p (NOLOCK)
on e.existingmeterid = p.existingmeterid
INNER JOIN
[Product_CompanyProd_Repository].[dbo].[premisenotes] pn (NOLOCK)
on pn.premiseid = p.premiseid
AND DATEPART(dw, pn.autotimestamp) IN (7,1)
WHERE category = 'Call attempt'
GROUP BY e.isvc
) AS WeekendCustContact ON WeekendCustContact.isvc=emr.isvc
LEFT JOIN
(SELECT e.isvc, COUNT(*) AS mcount
FROM [Product_CompanyProd_Repository].[dbo].[existingmetersroutes] e (NOLOCK)
INNER JOIN
[Product_CompanyProd_Repository].[dbo].[existingmeterspremises] p (NOLOCK)
on e.existingmeterid = p.existingmeterid
INNER JOIN
[Product_CompanyProd_Repository].[dbo].[premisenotes] pn (NOLOCK)
on pn.premiseid = p.premiseid
AND datepart(hh,pn.autotimestamp) >= 17
WHERE category = 'Call attempt'
GROUP BY e.isvc
) AS "After5PMCustContact" ON After5PMCustContact.isvc=emr.isvc
LEFT JOIN
(SELECT e.isvc, COUNT(*) AS mcount
FROM [Product_CompanyProd_Repository].[dbo].[existingmetersroutes] e (NOLOCK)
INNER JOIN
[Product_CompanyProd_Repository].[dbo].[existingmeterspremises] p (NOLOCK)
on e.existingmeterid = p.existingmeterid
INNER JOIN
[Product_CompanyProd_Repository].[dbo].[premisenotes] pn (NOLOCK)
on pn.premiseid = p.premiseid
WHERE category IN ('Call attempt','Door hanger','Letter received by customer','Call to Company who referred the caller to OurCompany')
GROUP BY e.isvc
) AS "TotalCustContact" ON TotalCustContact.isvc=emr.isvc
...
LEFT JOIN
(SELECT
e.isvc,
COUNT(*) AS TotalCount,
COUNT(CASE WHEN DATEPART(dw, pn.autotimestamp) IN (7, 1) AND category = 'Call attempt' THEN 1 END) AS WeekendCount,
COUNT(CASE WHEN datepart(hh, pn.autotimestamp) >= 17 AND category = 'Call attempt' THEN 1 END) AS After5PMCount
FROM [Product_CompanyProd_Repository].[dbo].[existingmetersroutes] e (NOLOCK)
INNER JOIN
[Product_CompanyProd_Repository].[dbo].[existingmeterspremises] p (NOLOCK)
on e.existingmeterid = p.existingmeterid
INNER JOIN
[Product_CompanyProd_Repository].[dbo].[premisenotes] pn (NOLOCK)
on pn.premiseid = p.premiseid
WHERE category IN ('Call attempt','Door hanger','Letter received by customer','Call to Company who referred the caller to OurCompany')
GROUP BY e.isvc
) AS "CustContact" ON CustContact.isvc=emr.isvc
...
LEFT JOIN
(SELECT * FROM
( SELECT o.summary AS isvc,
REPLACE(REPLACE([od].Information.query('data(OrderDetails/PremiseDetails/Name)').value('.','varchar(50)'),CHAR(10),''),',',';') AS "Name",
REPLACE(REPLACE([od].Information.query('data(OrderDetails/Location/StreetAddress)').value('.','varchar(50)'),CHAR(10),''),',',';') AS "Address",
[od].Information.query('data(OrderDetails/PremiseDetails/Phone)').value('.','varchar(50)') AS "Phone",
o.Autotimestamp
From Product_CompanyProd.dbo.ODMOrders AS o (NOLOCK)
INNER JOIN
Product_CompanyProd.dbo.ODMOrderAttributesUME AS oa (NOLOCK)
ON oa.Orderid = o.Orderid AND oa.district = @District AND oa.UTCCode <> ''
INNER JOIN
[Product_CompanyProd].[dbo].[ODMOrderdetails] od (NOLOCK)
ON od.Orderid = o.Orderid
WHERE o.StatusID IN (9,10)
) AS pd
WHERE
pd.Autotimestamp=(SELECT MAX(o.autotimestamp)
From Product_CompanyProd.dbo.ODMOrders AS o (NOLOCK)
INNER JOIN
Product_CompanyProd.dbo.ODMOrderAttributesUME AS oa (NOLOCK)
ON oa.Orderid = o.Orderid AND oa.district = @District
INNER JOIN
[Product_CompanyProd].[dbo].[ODMOrderdetails] od (NOLOCK)
ON od.Orderid = o.Orderid
WHERE o.summary = pd.isvc AND
o.StatusID IN (9,10)
)
) AS pd ON pd.isvc = emr.isvc
...
LEFT JOIN
(SELECT
o.summary AS isvc,
REPLACE(REPLACE([od].Information.query('data(OrderDetails/PremiseDetails/Name)').value('.','varchar(50)'),CHAR(10),''),',',';') AS "Name",
REPLACE(REPLACE([od].Information.query('data(OrderDetails/Location/StreetAddress)').value('.','varchar(50)'),CHAR(10),''),',',';') AS "Address",
[od].Information.query('data(OrderDetails/PremiseDetails/Phone)').value('.','varchar(50)') AS "Phone",
o.Autotimestamp
From Product_CompanyProd.dbo.ODMOrders AS o (NOLOCK)
INNER JOIN
Product_CompanyProd.dbo.ODMOrderAttributesUME AS oa (NOLOCK)
ON oa.Orderid = o.Orderid AND oa.district = @District AND oa.UTCCode <> ''
INNER JOIN
[Product_CompanyProd].[dbo].[ODMOrderdetails] od (NOLOCK)
ON od.Orderid = o.Orderid
WHERE o.StatusID IN (9,10)
AND NOT EXISTS (
SELECT *
FROM Product_CompanyProd.dbo.ODMOrders o2
INNER JOIN Product_CompanyProd.dbo.ODMOrderAttributesUME AS oa2 (NOLOCK)
ON oa2.Orderid = o2.Orderid AND oa2.district = @District AND oa2.UTCCode <> ''
WHERE o.summary = o2.summary AND o2.StatusID IN (9,10) AND o.Autotimestamp < o2.Autotimestamp
)
) AS pd ON pd.isvc = emr.isvc
关于具有多个相似子查询的 sproc 的 TSQL 优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5901857/
从另一个存储过程中调用一个存储过程是否会影响性能?我一直无法找到有关此问题的任何答案。如果它确实对性能产生负面影响,我想唯一的选择是将这些过程合并为一个更大的过程。 我知道由于 SQL 计划查询统计信
有没有一种方法可以隐藏/保护/混淆MS SQL存储过程? 最佳答案 如果混淆代码在功能上非常先进,我可以模糊地理解它,但是我认为混淆您的SQL可能不值得麻烦。 无论如何,我在这里看到的许多SQL都是以
我写了一个触发器,它可以连续发送一次电子邮件 插入 被执行。 ALTER TRIGGER TR_SendMailOnDataRequest ON DataRequest AFTER INS
这是我的第一篇文章,所以如果您需要任何说明,请告诉我。 我的服务器详细信息如下:-Windows 2008 数据中心版 SQL 2008标准版(10.0.1600) 12GB内存 四核单处理器机 问题
我有一个已弃用的存储过程,它不应再从代码中调用,但仍有一些系统仍在调用它。这是一个生产服务器,所以我对它进行诊断的间接访问非常有限。 有没有办法确定从 sproc 中调用特定存储过程的机器?诸如@@C
我编写了一个存储过程,它将在向表中插入新记录时触发。 调用时会返回错误 1064 并指出: 'You have an error in your SQL syntax; check the manua
通过网络从 C# .Net 应用程序运行相同存储过程会随着每次后续执行而逐渐变慢。它似乎花费的时间是上次执行的两倍(达到最大值;继续阅读)。执行时间逐渐变慢,直到发生 2 种情况中的一种,此时 SPR
我有一个正在使用的 SQL 2k5 存储过程。 我需要引用一个 UDF 来根据一些变量和用户权限计算价格。我最初尝试过这个,但它没有用,因为我没有引用字段...... SELECT dbo.f_Get
我有一个 .NET 类(用于讨论,ClassA),它调用 SQL Server 存储过程(用于讨论,fooSproc),使用 SqlDataReader 处理结果。处理行,并使用返回结果集中列的名称引
我只能选择访问数据库,不能创建存储过程。当 where 子句一次过滤一个数据时,有关如何自动查询日期范围的任何想法? 例如....我需要为 4/1/2013 和 4/30/2013 之间的每个日期一次
所以,我继承了某人的产品,虽然我查看了很多代码,但我觉得其中很多都可以改进。我的首要任务是优化以下存储过程,我希望有人能让我走上正轨。虽然我很菜,但我不禁觉得必须有更好的方法来做到这一点……运行需要4
我想知道您如何确保从 SProc 返回并存储在数据表中的结果不会太大而无法在内存中处理。 我已经进行了挖掘并发现了以下 Find size of object instance in bytes in
我的 VS 2010 项目中有 AdventureWorks 数据库 mdf 文件。无论如何我可以在我的 AdventureWorks 数据库中创建成员资格表。我知道我可以在 SQL SERVER 2
t-sql 我试图在这里阅读 UDF 和存储过程之间的区别 http://realfantasy.wordpress.com/2009/01/05/sql-server-user-defined-fu
我正在使用 Mysql 学习存储过程,但中央查询没有“编译”,我收到从“count(language) into”开始的语法警告。 我正在发送一个 var,并期待返回 2 个、所有语言的字符串和一个
我正在尝试将表名传递到我的 mysql 存储过程中,以使用此存储过程从不同的表中进行选择,但它不起作用... 这就是我正在尝试的: CREATE PROCEDURE `usp_SelectFromTa
我有一个 SqlDataAdapter,它填充了 21 行数据(4 列)。驱动它的 sproc 在几秒钟内在 SQL Mgmt Studio 中返回,但 .Fill() 需要 5 分钟。 Ar
意向声明: 我正在尝试自动化工作流程,定期将数据从 Sharepoint 中的 CSV 移动到 Azure SQL 数据库中的表中。我到目前为止已经了解了 1) 格式化 JSON 数组,以及 2) 创
是否可以通过命令行为我的sql server 2005/8数据库生成sql脚本? 我想自动化构建过程,其中一部分需要我将整个模式(表/ sprocs / etc)保存在文件中。 最佳答案 以前有一个名
我在使用 nHibernate 和手写 ado.net/存储过程之间来回切换。 我目前将 codesmith 与我编写的模板一起使用,它吐出映射我的数据库表的简单类,它包装了我的数据层的存储过程,以及
我是一名优秀的程序员,十分优秀!