使SQL查询更有效

时间:2013-11-08 16:29:56

标签: sql performance sql-server-2008

好的,所以我们这里的服务器工作并不是最好的,当我运行这个查询时,它确实限制了每个人在我们的服务器上使用任何东西。无论如何我可以使这个查询更有效率?我希望能够运行查询,而不是每个人都吓坏了,并说我们进展缓慢 查询基本上输出

Order Number | OrderDate | Sku | quantity | item price | our shipping $ | number indicating if it is paid | authorizeddate | amazonorderid | ebaybuyerid | Tax | TaxAmount | Shipping |Shipping Amt

查询返回我需要的所有内容,但我觉得它肯定可以重写。我试过但它一直在减慢服务器的速度,查询无法正常工作任何帮助都会受到赞赏!

SELECT g.ordernumber, 
       g.orderdate, 
       p.sku, 
       p.quantity, 
       p.name, 
       p.unitprice, 
       s.shipmentcost AS OurPrice, 
       e.rollupeffectivecheckoutstatus, 
       ' '            AS wonder, 
       ' '            AS AmazonorderID, 
       e.ebaybuyerid, 
       f.type         AS TAX, 
       f.amount       AS TAXCHARGE, 
       x.type         AS SHIPPING, 
       x.amount       AS ShippingCharge 
FROM   [ShipWorks].[dbo].[orderitem] p, 
       [ShipWorks].[dbo].[order] g, 
       [Shipworks].[dbo].[shipment] s, 
       [Shipworks].[dbo].[ebayorder] e, 
       [ShipWorks].[dbo].[ordercharge] f, 
       [ShipWorks].[dbo].[ordercharge] x 
WHERE  g.orderid = s.orderid 
       AND f.orderid = g.orderid 
       AND x.orderid = g.orderid 
       AND g.orderid = p.orderid 
       AND e.orderid = g.orderid 
       AND f.type = 'TAX' 
       AND x.type = 'SHIPPING' 
       AND CONVERT(CHAR(10), orderdate, 120)BETWEEN 
           ( '2013-09-16' ) AND ( '2013-09-17' ) 
UNION 
SELECT t.ordernumber, 
       t.orderdate, 
       i.sku, 
       i.quantity, 
       i.name, 
       i.unitprice, 
       g.shipmentcost, 
       ' ' AS RollupEffectiveCheckoutStatus, 
       r.authorizeddate, 
       ' ' AS AmazonorderID, 
       ' ' AS EbayBuyerID, 
       f.type, 
       f.amount, 
       x.type, 
       x.amount 
FROM   [ShipWorks].[dbo].[order] t, 
       [ShipWorks].[dbo].[prostoresorder] r, 
       [ShipWorks].[dbo].[orderitem] i, 
       [ShipWorks].[dbo].[shipment] g, 
       [ShipWorks].[dbo].[ordercharge] f, 
       [ShipWorks].[dbo].[ordercharge] x 
WHERE  t.orderid = r.orderid 
       AND t.orderid = i.orderid 
       AND t.orderid = g.orderid 
       AND t.orderid = f.orderid 
       AND t.orderid = x.orderid 
       AND f.type = 'TAX' 
       AND x.type = 'SHIPPING' 
       AND CONVERT(CHAR(10), orderdate, 120)BETWEEN 
           ( '2013-09-16' ) AND ( '2013-09-17' ) 
UNION 
SELECT t.ordernumber, 
       t.orderdate, 
       i.sku, 
       i.quantity, 
       i.name, 
       i.unitprice, 
       q.shipmentcost, 
       ' ' AS RollupEffectiveCheckoutStatus, 
       ' ' AS AuthorizedDate, 
       a.amazonorderid, 
       ' ' AS EbayBuyerID, 
       f.type, 
       f.amount, 
       x.type, 
       x.amount 
FROM   [ShipWorks].[dbo].[order] t, 
       [ShipWorks].[dbo].[amazonorder] a, 
       [ShipWorks].[dbo].[orderitem] i, 
       [ShipWorks].[dbo].[shipment] q, 
       [ShipWorks].[dbo].[ordercharge] f, 
       [ShipWorks].[dbo].[ordercharge] x 
WHERE  t.orderid = a.orderid 
       AND t.orderid = f.orderid 
       AND t.orderid = x.orderid 
       AND t.orderid = i.orderid 
       AND t.orderid = q.orderid 
       AND f.type = 'TAX' 
       AND x.type = 'SHIPPING' 
       AND CONVERT(CHAR(10), orderdate, 120)BETWEEN 
           ( '2013-09-16' ) AND ( '2013-09-17' ) ` 

2 个答案:

答案 0 :(得分:0)

首先进行典型的数据库优化,比如我会查看为数据库分配了多少内存。这可能会有所帮助,因为它可能会阻止数据在处理过程中写入磁盘。

您是否需要增加服务器硬件?

此查询是否需要每天运行一次?如果是这样的话,请设置一个cron作业,以便在没有人工作时运行它。

如果您仍然遇到问题,则可能需要归档数据,使其不在活动表中,您还可以将数据提取到可以执行联合的应用程序中。

答案 1 :(得分:0)

UNION的所有三个分支非常相似,并在6个表格中的5个上重复相同的JOINWHERE

你可以尝试

SELECT o.ordernumber,
       o.orderdate,
       oi.sku,
       oi.quantity,
       oi.name,
       oi.unitprice,
       s.shipmentcost AS OurPrice,
       ca.rollupeffectivecheckoutstatus,
       ca.wonder,
       ca.AmazonorderID,
       ca.ebaybuyerid,
       oc1.type       AS TAX,
       oc1.amount     AS TAXCHARGE,
       oc2.type       AS SHIPPING,
       oc2.amount     AS ShippingCharge
FROM   [dbo].[order] o
       JOIN [dbo].[orderitem] oi
         ON o.orderid = oi.orderid
       JOIN [dbo].[shipment] s
         ON o.orderid = s.orderid
       JOIN [dbo].[ordercharge] oc1
         ON oc1.orderid = o.orderid
            AND oc1.type = 'TAX'
       JOIN [dbo].[ordercharge] oc2
         ON oc2.orderid = o.orderid
            AND oc2.type = 'SHIPPING'
       CROSS APPLY (SELECT rollupeffectivecheckoutstatus, NULL, NULL, ebaybuyerid
                    FROM   [dbo].[ebayorder] e
                    WHERE  e.orderid = o.orderid
                    UNION ALL
                    SELECT NULL, authorizeddate, NULL, NULL
                    FROM   [dbo].[prostoresorder] p
                    WHERE  p.orderid = o.orderid
                    UNION ALL
                    SELECT NULL, '', amazonorderid, NULL
                    FROM   [dbo].[amazonorder] a
                    WHERE  a.orderid = o.orderid) ca(rollupeffectivecheckoutstatus, wonder, AmazonorderID, ebaybuyerid)
WHERE  orderdate >= '20130916'
       AND orderdate < '20130918'