UsefulSQL
FindObject:
---在当前Server上找某某object,注意只需修改"要找的object"就可以使用
EXEC sp_MSforeachdb 'use ? ;
IF EXISTS(SELECT top 1 1 FROM sys.syscomments WHERE text LIKE ''%要找的object%'')
SELECT ''?'' as dbname,object_name(id) as object FROM sys.syscomments
WHERE text LIKE ''%要找的object%'''
XML处理:
INSERT INTO IMK.[dbo].[ClaimAttachment]
([ClaimNumber]
,[AttachmentName]
,[Path]
,[Status]
,[InDate]
,[InUser]
,[CompanyCode]
,[LanguageCode])
SELECT
@ClaimNumber
,T.c.value('(AttachmentName/text())[1]', 'nvarchar(300)') AS AttachmentName
,T.c.value('(Path/text())[1]', 'nvarchar(50)') AS [Path]
,T.c.value('(Status/text())[1]', 'char(1)') AS [Status] -- 'O' AS Status
,GETDATE() AS [InDate]
,@IssueUser
,@CompanyCode
,@LanguageCode
FROM
@AttachmentList.nodes('/ArrayOfClaimAttachmentEntity/ClaimAttachmentEntity') T (c)
XML简单序列化:
XmlSerializer ser = new XmlSerializer(typeof(CreateTaskListRequest));
StringBuilder xml = new StringBuilder();
TextWriter stream = new StringWriter(xml);
ser.Serialize(stream, requestList.CreateTaskByTemplateIDRequests[0]);
string s = xml.ToString();
SQL Training:
1、建议将:
insert into log_SOInvoice (sonumber, wh ,invoicenumber)
select top 5000 sonumber,'50' , 1000 from newsql.newegg.dbo.changeorderloghistory
where sonumber in ( select sonumber from act..newegg_invoicemaster(nolock))
and changeordertime> getdate() - 30
and sonumber not in ( select sonumber from log_SOInvoice (nolock) where wh = '50')
CPU time = 484 ms, elapsed time = 2230 ms.
修改为:
insert into log_SOInvoice (sonumber, wh ,invoicenumber)
select top 5000 sonumber,'50' , 1000 from newsql.newegg.dbo.changeorderloghistory
where changeordertime> getdate() - 30
and sonumber not in ( select sonumber from log_SOInvoice (nolock) where wh = '50')
and sonumber in ( select sonumber from act..newegg_invoicemaster(nolock))
2、select ReferenceSoNumber,ItemNumber,SerialNumber,ScanDate,ScanUser,LargeItemFlag,WarehouseNumber,
ShippingUser,ShippingDate,TransactionNumber
from DropShipSerialNumber07
where shippingDate>'12/18/2003'
and transactionnumber not in (select transactionnumber from warehouse4..UploadSerialnumberLog07
where datediff(day,indate,getdate())<8)
and datediff(day,shippingdate,getdate())<7
执行结果:CPU time = 21437 ms, elapsed time = 33927 ms.
修改为:
select ReferenceSoNumber,ItemNumber,SerialNumber,ScanDate,ScanUser,LargeItemFlag,WarehouseNumber,
ShippingUser,ShippingDate,TransactionNumber
from DropShipSerialNumber07 (nolock index(ShippingDate))
where shippingdate>dateadd(day,-7,getdate())
--shippingDate>'12/18/2003'
and transactionnumber not in (select transactionnumber from warehouse4..UploadSerialnumberLog07(nolock)
where indate>dateadd(day,-8,getdate()))
执行结果: CPU time = 875 ms, elapsed time = 2005 ms.
3.建议将:
insert into log_SOInvoice (sonumber, wh ,invoicenumber)
select top 5000 a.sonumber,a.warehousenumber ,b.invoicenumber from newegg_sotransaction a(nolock)
inner join newegg_somaster b (nolock) on a.sonumber = b.sonumber
where b.status <> 'v' and a.warehousenumber is not null
and b.invoicenumber is not null and b.invoicenumber <> 0
and a.sonumber not in ( select sonumber from log_SOInvoice (nolock))
and warehousenumber > '' and warehousenumber is not null and warehousenumber <>'00' and warehousenumber <'90'
group by a.sonumber,a.warehousenumber,b.invoicenumber
CPU time = 18594 ms, elapsed time = 6215 ms.
修改为:
insert into log_SOInvoice (sonumber, wh ,invoicenumber)
select top 5000 a.sonumber,a.warehousenumber ,b.invoicenumber from newegg_sotransaction a(nolock)
inner join newegg_somaster b (nolock) on a.sonumber = b.sonumber
where not exists( select sonumber from log_SOInvoice (nolock) where a.sonumber = sonumber )
and b.status <> 'v' and a.warehousenumber is not null
and b.invoicenumber is not null and b.invoicenumber <> 0
and warehousenumber > '' and warehousenumber is not null and warehousenumber <>'00' and warehousenumber <'90'
group by a.sonumber,a.warehousenumber,b.invoicenumber
4.建议将:
update GCI set GCI.LeftAmount = GCI.LeftAmount + GD.SOAmount
From GiftCertificateItem GCI ,giftredeem GD
Where GCI.giftcode = GD.RedeemGiftCode
And GD.SONumber = @SONumber
And GD.Status ='O'
update GCI set GCI.status = Case when GCI.giftunitprice < GCI.leftamount Then 'A' Else 'U' End
From GiftCertificateItem GCI ,giftredeem GD
Where GCI.giftcode = GD.RedeemGiftCode
And GD.SONumber = @SONumber
And GD.Status ='O'
合并为:
update GCI set GCI.LeftAmount = GCI.LeftAmount + GD.SOAmount,
GCI.status = Case when GCI.giftunitprice < GCI.leftamount Then 'A' Else 'U' End
From GiftCertificateItem GCI ,giftredeem GD
Where GCI.giftcode = GD.RedeemGiftCode
And GD.SONumber = @SONumber
And GD.Status ='O'
5.建议将SQL语句修改为UNION ALL连接;
SELECT distinct a.ItemNumber,
b.itemcatalog,
ISNULL(c.ComboID,
0) AS ComboID,
c.ItemNumber AS ComboItem,
ISNULL(c.ItemCatalog,
0) AS comboItemCatalog
FROM ECommerce2005.dbo.IM_ItemBuyAlong a (nolock)
INNER JOIN abs.dbo.Arinvt01 b (nolock)
ON a.ItemNumber = b.Item
LEFT JOIN ECOMMERCE2005.DBO.IM_ComboTransaction c (NOLOCK)
ON ((c.ItemNumber = a.itemNumber
OR c.ItemCatalog = b.Itemcatalog)
AND c.ComboGroupid <> a.comboGroupid)
OR c.combogroupid IN (SELECT combogroupid
FROM ECOMMERCE2005.DBO.IM_ComboGiftItem(NOLOCK)
WHERE itemNumber = a.itemNumber
AND comboGroupid <> a.comboGroupid)
修改为:
SELECT a.ItemNumber,
b.itemcatalog,
ISNULL(c.ComboID,
0) AS ComboID,
c.ItemNumber AS ComboItem,
ISNULL(c.ItemCatalog,
0) AS comboItemCatalog
FROM ECommerce2005.dbo.IM_ItemBuyAlong a (nolock)
INNER JOIN abs.dbo.Arinvt01 b (nolock)
ON a.ItemNumber = b.Item
LEFT JOIN ECOMMERCE2005.DBO.IM_ComboTransaction c (NOLOCK)
ON ((c.ItemNumber = a.itemNumber
OR c.ItemCatalog = b.Itemcatalog)
AND c.ComboGroupid <> a.comboGroupid)
union
SELECT a.ItemNumber,
b.itemcatalog,
ISNULL(c.ComboID,
0) AS ComboID,
c.ItemNumber AS ComboItem,
ISNULL(c.ItemCatalog,
0) AS comboItemCatalog
FROM ECommerce2005.dbo.IM_ItemBuyAlong a (nolock)
INNER JOIN abs.dbo.Arinvt01 b (nolock)
ON a.ItemNumber = b.Item
LEFT JOIN ECOMMERCE2005.DBO.IM_ComboTransaction c (NOLOCK)
ON c.combogroupid IN (SELECT combogroupid
FROM ECOMMERCE2005.DBO.IM_ComboGiftItem(NOLOCK)
WHERE itemNumber = a.itemNumber
AND comboGroupid <> a.comboGroupid)
原SQL语句执行结果: CPU time = 11187 ms, elapsed time = 11264 ms.
修改为UNION ALL后执行结果:CPU time = 3486 ms, elapsed time = 2412 ms.
6.建议将SQL语句:
select * from scm..potran01 (nolock) where purno = 912926 order by TransactionNumber
修改为:
select * from scm..potran01 (nolock) where purno = '912926'
order by TransactionNumber
结果:修改前:CPU time = 1109 ms, elapsed time = 5319 ms.
修改后:CPU time = 0 ms, elapsed time = 40 ms.
7.可以考虑将SQL语句:
Select * From CodeCenter..ViewPropertiesInAdvSearch
Where CatalogID=22
Order By Priority,ValuePriority,ValueDescription
修改为:
Select * From CodeCenter..ViewPropertiesInAdvSearch
Where CatalogID=22
Order By Priority,ValuePriority,ValueDescription
option(maxdop 1)
结果:修改前:CPU time = 3284 ms, elapsed time = 14832 ms.
修改后:CPU time = 766 ms, elapsed time = 1183 ms.
8.建议将SQL语句:
SELECT ACount = (SELECT COUNT(*)
FROM ABS.dbo.arinvt01 a (nolock)
INNER JOIN codecenter..itemdescription b (nolock)
ON a.item = b.itemnumber
WHERE NEWEGGITEMMARK > 0
AND checked = 1),
VCount = (SELECT COUNT(*)
FROM ABS.dbo.arinvt01 a (nolock)
INNER JOIN codecenter..itemdescription b (nolock)
ON a.item = b.itemnumber
WHERE NEWEGGITEMMARK > 0
AND (ITEM LIKE '%sf'
OR ITEMCATALOG = 346
OR ITEM LIKE '50-%')
AND checked = 1),
InStock = (SELECT COUNT(*)
FROM ABS.dbo.arinvt01 A (nolock)
INNER JOIN inventory..ItemInventory B (NOLOCK)
ON A.ITEM = B.Item
INNER JOIN codecenter..itemdescription c (nolock)
ON a.item = c.itemnumber
WHERE A.NEWEGGITEMMARK > 0
AND B.newegg_avail > 0
AND A.ITEM NOT LIKE '%sf'
AND A.ITEMCATALOG <> 346
AND A.ITEM NOT LIKE '50-%'
AND checked = 1),
VirtualInStock = (SELECT COUNT(*)
FROM ABS.dbo.arinvt01 A (nolock)
INNER JOIN inventory..ItemInventory B (NOLOCK)
ON A.ITEM = B.Item
INNER JOIN codecenter..itemdescription c (nolock)
ON A.item = c.itemnumber
WHERE A.NEWEGGITEMMARK > 0
AND B.newegg_avail > 0
AND (A.ITEMCATALOG = 346
OR A.ITEM LIKE '%SF'
OR A.ITEM LIKE '50-%')
AND checked = 1),
Refurbish = (SELECT COUNT(*)
FROM ABS.dbo.arinvt01 A (nolock)
INNER JOIN inventory..ItemInventory B (NOLOCK)
ON A.ITEM = B.Item
INNER JOIN codecenter..itemdescription c (nolock)
ON a.item = c.itemnumber
WHERE A.NEWEGGITEMMARK > 0
AND B.newegg_avail > 0
AND A.ITEM LIKE '%R'
AND checked = 1)
修改为:
SELECT ACount = SUM(ACount),
VCount = SUM(VCount),
InStock = SUM(InStock),
VirtualInStock = SUM(VirtualInStock),
Refurbish = SUM(Refurbish)
FROM (SELECT ACount = COUNT(*),
VCount = COUNT(CASE
WHEN (ITEM LIKE '%sf'
OR ITEMCATALOG = 346
OR ITEM LIKE '50-%') THEN 1
END),
InStock = 0,
VirtualInStock = 0,
Refurbish = 0
FROM ABS.dbo.arinvt01 a (nolock)
INNER JOIN codecenter..itemdescription b (nolock)
ON a.item = b.itemnumber
WHERE NEWEGGITEMMARK > 0
AND checked = 1
UNION ALL
SELECT ACount = 0,
VCount = 0,
InStock = COUNT(CASE
WHEN (A.ITEM NOT LIKE '%sf'
AND A.ITEMCATALOG <> 346
AND A.ITEM NOT LIKE '50-%') THEN 1
END),
VirtualInStock = COUNT(CASE
WHEN (A.ITEMCATALOG = 346
OR A.ITEM LIKE '%SF'
OR A.ITEM LIKE '50-%') THEN 1
END),
Refurbish = COUNT(CASE
WHEN A.ITEM LIKE '%R' THEN 1
END)
FROM ABS.dbo.arinvt01 A (nolock)
INNER JOIN inventory..ItemInventory B (NOLOCK)
ON A.ITEM = B.Item
INNER JOIN codecenter..itemdescription c (nolock)
ON A.item = c.itemnumber
WHERE A.NEWEGGITEMMARK > 0
AND B.newegg_avail > 0
AND checked = 1) A
结果:修改前:CPU time = 13375 ms, elapsed time = 9772 ms.
修改后:CPU time = 8916 ms, elapsed time = 4776 ms.
9.建议将SQL语句:
SELECT SoNumber
FROM [CNSLS].[dbo].[SOMaster](NOLOCK)
WHERE CompanyCode = 1006
AND Status = 'O'
AND CreditCardVerifyMark = 'G'
AND status <> 'V'
AND (InvoiceNumber IS NULL
OR InvoiceNumber = 0)
AND AcctPostDate IS NOT NULL
AND SoNumber NOT IN (SELECT SoNumber
FROM [CNSLS].[dbo].[DownloadSO](NOLOCK))
修改为:
SELECT SoNumber
FROM [CNSLS].[dbo].[SOMaster] a(NOLOCK)
WHERE CompanyCode = 1006
AND Status = 'O'
AND CreditCardVerifyMark = 'G'
-- AND status <> 'V'
AND (InvoiceNumber IS NULL
OR InvoiceNumber = 0)
AND AcctPostDate IS NOT NULL
AND not exists (SELECT SoNumber
FROM [CNSLS].[dbo].[DownloadSO](NOLOCK) where SoNumber=a.SoNumber)
option (maxdop 1)
结果:修改前:CPU time = 3200 ms, elapsed time = 4496 ms.
修改后:CPU time = 1297 ms, elapsed time = 1382 ms.
10.由于abs..PO_Detail_Query中的potran01表中的ITEM字段的值长度最大为10,因此建议将:
SELECT TOP 200 *
FROM abs..PO_Detail_Query
WHERE Item LIKE '[0-9]%'
AND Item LIKE '%19-103-759%'
AND purdate >= '10/16/2006 7:21:39 AM'
AND purdate <= '1/17/2007 7:21:39 AM'
修改为:
SELECT TOP 200 *
FROM abs..PO_Detail_Query
WHERE Item = '19-103-759'
AND purdate >= '10/16/2006 7:21:39 AM'
AND purdate <= '1/17/2007 7:21:39 AM'
结果:修改前:CPU time = 6047 ms, elapsed time = 10633 ms.
修改后:CPU time = 0 ms, elapsed time = 5 ms.
2、Personal表后未加NOLOCK;
11.建议将SQL语句:
SELECT Item = rtrim(b.ItemNumber) + 'R',
RefurbishIn = SUM(b.Quantity),
RefurbishOut = 0,
RefurbishAdjust = 0
FROM [abs].dbo.RefurbishMaster AS a (NOLOCK)
INNER JOIN [abs].dbo.RefurbishTransaction AS b (NOLOCK)
ON a.RTNumber = b.RTNumber
WHERE a.ReceivingDate >= @pThisMonthBeginDate
AND a.ReceivingDate < @pOneDayAfterEffectDate
AND b.ItemNumber LIKE '%-%'
GROUP BY b.ItemNumber
UNION ALL
SELECT Item = b.ItemNumber,
RefurbishIn = 0,
RefurbishOut = SUM(b.Quantity),
RefurbishAdjust = 0
FROM abs.dbo.RefurbishMaster AS a (nolock)
INNER JOIN [abs].dbo.RefurbishTransaction AS b (nolock)
ON a.RTNumber = b.RTNumber
WHERE a.ReceivingDate >= @pThisMonthBeginDate
AND a.ReceivingDate < @pOneDayAfterEffectDate
AND a.Source = '3'
AND b.ItemNumber LIKE '%-%'
GROUP BY b.ItemNumber
修改为:
SELECT Item =CASE WHEN a.Source = '3' THEN b.ItemNumber ELSE rtrim(b.ItemNumber) + 'R' END,
RefurbishIn = CASE WHEN a.Source = '3' THEN 0 ELSE SUM(b.Quantity) END,
RefurbishOut = CASE WHEN a.Source = '3' THEN SUM(b.Quantity) ELSE 0 END,
RefurbishAdjust = 0
FROM [abs].dbo.RefurbishMaster AS a (NOLOCK)
INNER JOIN [abs].dbo.RefurbishTransaction AS b (NOLOCK)
ON a.RTNumber = b.RTNumber
WHERE a.ReceivingDate >= '2007-01-01'--@pThisMonthBeginDate
AND a.ReceivingDate < '2007-01-18'--@pOneDayAfterEffectDate
AND b.ItemNumber LIKE '%-%'
GROUP BY b.ItemNumber, a.Source
经查,调整后结果是一致的;
结果:调整前:CPU time = 846 ms, elapsed time = 2161 ms.
调整后:CPU time = 484 ms, elapsed time = 901 ms.
12.由于使用sp_executesql函数时,字符型的参数变量是nchar或nvarchr的,但由于ZipCode,ShippingAddress是char型,因此查询时是不会使用索引的。因此建议将SQL:
exec sp_executesql N'
SELECT *
FROM imk.dbo.viewDetectFrud (nolock)
WHERE ZipCode like rtrim(@ZipCode) + ''%''
and (ShippingAddress like rtrim(@FirstAddress) + ''%''
or ShippingAddress like rtrim(@SecondAddress) + ''%'') ',
N'@ZIPCODE nvarchar(10),@FIRSTADDRESS nvarchar(80),@SECONDADDRESS nvarchar(80)',
@ZIPCODE = N'97070', @FIRSTADDRESS = N'287', @SECONDADDRESS = N''
修改为:
declare @ZIPCODE varchar(10),@FIRSTADDRESS varchar(80),@SECONDADDRESS varchar(80)
set @ZIPCODE = '97070'
set @FIRSTADDRESS = '287'
set @SECONDADDRESS = ''
--exec sp_executesql N'
exec ('
SELECT *
FROM imk.dbo.viewDetectFrud (nolock)
WHERE ZipCode like ''' + @ZipCode + '%''
and (ShippingAddress like ''' + @FirstAddress + '%''
or ShippingAddress like '''+ @SecondAddress + '%'')')
结果:修改前:CPU time = 15781 ms, elapsed time = 6395 ms.
修改后:CPU time = 13 ms, elapsed time = 13 ms.
UsefulSQL的更多相关文章
随机推荐
- CodeIgniter2.2.0-在控制器里调用load失败报错的问题
报错如下: hello A PHP Error was encountered Severity: Notice Message: Undefined property: Test::$load Fi ...
- Jexus针对Asp.net core应用程序的六大不可替代的优势
1,配置简便:在Jexus上,Asp.net core只是Jexus上的一个"站点",因此,只需在Jexus上配置这个站点就行,无需其它配置: 2,操作统一:Jexus停止这个站点 ...
- ENode 2.0 - 整体架构介绍
前言 今天是个开心的日子,又是周末,可以轻轻松松的写写文章了.去年,我写了ENode 1.0版本,那时我也写了一个分析系列.经过了大半年的时间,我对第一个版本做了很多架构上的改进,最重要的就是让ENo ...
- 手机淘宝UWP
各位园主好! bug 走势: 哪天bug 足够少,哪天就可以发布了 :) 2015/10/23: 49 2015/10/26: 40 2015/10/27: 36 2015/10/28: 30 20 ...
- 【译】AS3利用CPU缓存
利用CPU缓存 计算机有随机存取存储器RAM(译注:即我们常说的内存),但有更快形式的存储器.如果你希望你的应用程序的快速运行,你需要知道这些其他的存储器.今天的文章中讨论了它们,并给出了两个AS ...
- java提高篇(二十)-----集合大家族
在编写java程序中,我们最常用的除了八种基本数据类型,String对象外还有一个集合类,在我们的的程序中到处充斥着集合类的身影!java中集合大家族的成员实在是太丰富了,有常用的Arra ...
- Redis中统计各种数据大小的方法
转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/125.html?1455853369如果 MySQL 数据库比较大的话,我 ...
- Atitit 数据存储的数据表连接attilax总结
Atitit 数据存储的数据表连接attilax总结 1.1. 三种物理连接运算符:嵌套循环连接.合并连接以及哈希连接1 1.2. a.嵌套循环连接(nested loops join)1 1.3. ...
- C#并行编程-相关概念
菜鸟初步学习,不对的地方请大神指教,参考<C#并行编程高级教程.pdf> 目录 C#并行编程-相关概念 C#并行编程-Parallel C#并行编程-Task C#并行编程-并发集合 C# ...
- salesforce 零基础开发入门学习(十一)sObject及Schema深入
sObject在salesforce中占有举足轻重的位置,除了在数据库中数据以外,我们还应该关心一下他的元信息.元信息封装在Schema命名空间内. 作为面向对象语言,我们可以畅想一下如果我们是设计人 ...