0

好的,所以我们这里的服务器不是最好的,当我运行这个查询时,它真的会限制使用我们服务器上任何东西的每个人。无论如何我可以使这个查询更有效率吗?我希望能够运行查询,而不会让每个人都吓坏并说我们会减慢查询的速度,基本上是输出

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' ) ` 
4

2 回答 2

0

首先做典型的数据库优化事情,比如我会检查你的数据库分配了多少内存。这确实可以帮助解决问题,因为它可能会阻止数据在处理过程中写入磁盘。

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

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

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

于 2013-11-08T16:39:30.957 回答
0

所有三个分支UNION都非常相似,JOIN并且WHERE在 6 张桌子中的 5 张桌子上重复相同的内容。

你可以试试

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' 
于 2013-11-08T18:29:02.680 回答