重写使用条件连接的视图[OR]

时间:2017-12-22 20:16:17

标签: performance tsql join view

我有这个表现非常糟糕的问题,问题是它在使用或条件的连接上构建

我尝试使用CTE和UNION重写这个过去2天,但结果不正确

也许你们可以提供帮助,只有一些想法如何去做,因为我完全迷失了。

CREATE VIEW [dbo].[V_Amazon_Listings]
AS

SELECT
DISTINCT
    ISNULL(a.MerchantId, b.MerchantId) AS MerchantId
    , ISNULL(a.SKU, b.[seller-sku]) AS SKU
    , ISNULL(a.Quantity, 0) AS NONFBAQTY
    , ISNULL(b.[Quantity Available], 0) AS FBAQTY
    , ISNULL(a.[ASIN], b.[ASIN]) AS [ASIN]

    , CASE
        WHEN salePrices.SKU IS NOT NULL THEN ISNULL(CONVERT(DECIMAL(18, 2), (salePrices.SalePrice * vamm.USDConversionRate)), 9999)
        ELSE ISNULL(CONVERT(DECIMAL(18, 2), (a.Price * vamm.USDConversionRate)), 9999)
        END AS Price
    , CASE ISNULL(a.Quantity, 0)
        WHEN 0 THEN ISNULL(b.[Quantity Available], 0)
        ELSE ISNULL(a.Quantity, 0)
        END AS Quantity
    --a.ReportRequestId,
    , CASE WHEN ISNULL(a.Quantity, 0) = 0 AND ISNULL(b.[Quantity Available], 0) > 0 THEN 1
        ELSE 0
        END AS IsFBAed
    , CASE WHEN c.[seller-sku] IS NULL THEN 0
        ELSE 1
        END AS IsRestricted--,
--a.ReportRequestId RegReportRequestID, b.ReportRequestId FBAReportRequestID, c.ReportRequestID CancReportRequestID
FROM 
    (
        SELECT
            ID
            , MerchantId
            , SKU
            , [ASIN]
            , Price
            , CASE Quantity
                WHEN '' THEN 0
                ELSE Quantity
                END AS Quantity
            , ReportRequestId
        FROM dbo.Amazon_Listings_Raw WITH (NOLOCK)
        WHERE RIGHT(SKU,4) <> '__ON'  
            AND ReportRequestId IN 
            (
                SELECT 
                MAX(ReportRequestId)
                FROM v_Amazon_Listings_Raw WITH (NOLOCK)
                GROUP BY MerchantId
            )
    ) AS a
FULL JOIN 
    (
        SELECT
            b.ID
            , b.MerchantId
            , b.[seller-sku]
            , b.[fulfillment-channel-sku]
            , b.[ASIN]
            , b.[condition-type]
            , b.[Warehouse-Condition-code]
            , b.[Quantity Available]
            , b.ReportRequestId
        FROM dbo.[Amazon_FBA_Listings_Raw] AS b WITH (NOLOCK)
        WHERE 
            ReportRequestId IN 
                (
                    SELECT
                    MAX(ReportRequestId)
                    FROM [v_Amazon_FBA_Listings_Raw] WITH (NOLOCK)
                    GROUP BY MerchantId
                )
            AND [Warehouse-Condition-code] = 'Sellable'
    ) AS b
    ON a.MerchantId = b.MerchantId
    AND a.SKU = b.[seller-sku]
LEFT JOIN 
    (
        SELECT
            ID
            , MerchantId
            , [item-name]
            , [item-description]
            , [seller-sku]
            , Price
            , Quantity
            , [image-url]
            , [item-is-marketplace]
            , [product-id-type]
            , [zshop-shipping-fee]
            , [item-note]
            , [item-condition]
            , [zshop-category1]
            , [zshop-browse-path]
            , [zshop-storefront-feature]
            , asin1
            , asin2
            , asin3
            , [will-ship-internationally]
            , [expedited-shipping]
            , [zshop-boldface]
            , [product-id]
            , ReportRequestId
        FROM dbo.[Amazon_Cancelled_Listings_Raw] WITH (NOLOCK)
        WHERE RIGHT([seller-sku],4) <> '__ON'
            AND ReportRequestId IN 
            (
                SELECT MAX(ReportRequestId)
                FROM [v_Amazon_Cancelled_Listings_Raw] WITH (NOLOCK)
                GROUP BY MerchantId
            )
    ) AS c
    ON (a.MerchantId = c.MerchantId OR b.MerchantId = c.MerchantId)
    AND (b.[seller-sku] = c.[seller-sku] OR a.SKU = c.[seller-sku])

INNER JOIN V_Amazon_Marketplace_Merchants vamm
    ON (a.MerchantId = vamm.MerchantID OR b.MerchantId = vamm.MerchantId)
LEFT JOIN AmazonOurPrices salePrices WITH (NOLOCK)
    ON vamm.MerchantId = salePrices.MerchantId
    AND a.SKU = salePrices.SKU
    AND salePrices.SalePrice <> salePrices.RegularPrice

这就是我想出来的,它的运行速度非常快20秒, 但数据已关闭,不确定我遗失或做错了什么。 任何帮助都会受到赞赏

CREATE VIEW AMZ_NOTUSED_12_22_2017
as

WITH A ( MerchantID,Sku,Asin,Price,Quantity, [Seller-Sku],SalePrice,USDConversionRate,salePricesSKU,[Quantity Available])
AS
(

SELECT  a.MerchantId,a.SKU,a.ASIN,a.Price,a.Quantity, c.[seller-sku],salePrices.SalePrice,vamm.USDConversionRate,salePrices.SKU AS salePricesSKU,'' [Quantity Available]

FROM 
    (
        SELECT
            ID
            , MerchantId
            , SKU
            , [ASIN]
            , Price
            , CASE Quantity
                WHEN '' THEN 0
                ELSE Quantity
                END AS Quantity
            , ReportRequestId
        FROM dbo.Amazon_Listings_Raw WITH (NOLOCK)
        WHERE RIGHT(SKU,4) <> '__ON' 
            AND ReportRequestId IN 
            (
                SELECT 
                MAX(ReportRequestId)
                FROM v_Amazon_Listings_Raw WITH (NOLOCK)
                GROUP BY MerchantId
            )
    ) AS a 
LEFT JOIN 
    (
        SELECT
            ID
            , MerchantId
            , [item-name]
            , [item-description]
            , [seller-sku]
            , Price
            , Quantity
            , [image-url]
            , [item-is-marketplace]
            , [product-id-type]
            , [zshop-shipping-fee]
            , [item-note]
            , [item-condition]
            , [zshop-category1]
            , [zshop-browse-path]
            , [zshop-storefront-feature]
            , asin1
            , asin2
            , asin3
            , [will-ship-internationally]
            , [expedited-shipping]
            , [zshop-boldface]
            , [product-id]
            , ReportRequestId
        FROM dbo.[Amazon_Cancelled_Listings_Raw] WITH (NOLOCK)
        WHERE RIGHT([seller-sku],4) <> '__ON'
            AND ReportRequestId IN 
            (
                SELECT MAX(ReportRequestId)
                FROM [v_Amazon_Cancelled_Listings_Raw] WITH (NOLOCK)
                GROUP BY MerchantId
            )
    ) AS c
    ON a.MerchantId = c.MerchantId and a.SKU = c.[seller-sku] 
INNER JOIN V_Amazon_Marketplace_Merchants vamm
    ON a.MerchantId = vamm.MerchantID 
LEFT JOIN AmazonOurPrices salePrices WITH (NOLOCK)
    ON vamm.MerchantId = salePrices.MerchantId
    AND a.SKU = salePrices.SKU
    AND salePrices.SalePrice <> salePrices.RegularPrice

    )

    ,


    B AS 
    (

SELECT b.MerchantId,
b.[Seller-SKU] AS Sku,
b.ASIN, 
c.Price, 
c.quantity , 
c.[seller-sku], 
salePrices.SalePrice,
vamm.USDConversionRate,
salePrices.SKU AS salePricesSKU,
b.[Quantity Available]

FROM 
    (
        SELECT
            b.ID
            , b.MerchantId
            , b.[seller-sku]
            , b.[fulfillment-channel-sku]
            , b.[ASIN]
            , b.[condition-type]
            , b.[Warehouse-Condition-code]
            , b.[Quantity Available]
            , b.ReportRequestId
        FROM dbo.[Amazon_FBA_Listings_Raw] AS b WITH (NOLOCK)
        WHERE 
            ReportRequestId IN 
                (
                    SELECT
                    MAX(ReportRequestId)
                    FROM [v_Amazon_FBA_Listings_Raw] WITH (NOLOCK)
                    GROUP BY MerchantId
                )
            AND [Warehouse-Condition-code] = 'Sellable'
    ) AS b
LEFT JOIN 
    (
        SELECT
            ID
            , MerchantId
            , [item-name]
            , [item-description]
            , [seller-sku]
            , Price
            , Quantity
            , [image-url]
            , [item-is-marketplace]
            , [product-id-type]
            , [zshop-shipping-fee]
            , [item-note]
            , [item-condition]
            , [zshop-category1]
            , [zshop-browse-path]
            , [zshop-storefront-feature]
            , asin1
            , asin2
            , asin3
            , [will-ship-internationally]
            , [expedited-shipping]
            , [zshop-boldface]
            , [product-id]
            , ReportRequestId
        FROM dbo.[Amazon_Cancelled_Listings_Raw] WITH (NOLOCK)
        WHERE RIGHT([seller-sku],4) <> '__ON'
            AND ReportRequestId IN 
            (
                SELECT MAX(ReportRequestId)
                FROM [v_Amazon_Cancelled_Listings_Raw] WITH (NOLOCK)
                GROUP BY MerchantId
            )
    ) AS c
    ON 
    b.[seller-sku] = c.[seller-sku] and b.MerchantId = c.MerchantId


INNER JOIN V_Amazon_Marketplace_Merchants vamm
    ON b.MerchantId = vamm.MerchantId
LEFT JOIN AmazonOurPrices salePrices WITH(NOLOCK)
    ON vamm.MerchantId = salePrices.MerchantId
    AND b.[Seller-SKU] = salePrices.SKU
    AND salePrices.SalePrice <> salePrices.RegularPrice 
    )
SELECT
DISTINCT
    a.MerchantId AS MerchantId
    , a.SKU
    , a.Quantity AS NONFBAQTY
    , ISNULL(a.[Quantity Available],0) AS FBAQTY
    , a.[ASIN] AS [ASIN]

    , CASE
        WHEN salePricesSKU IS NOT NULL THEN ISNULL(CONVERT(DECIMAL(18, 2), (SalePrice * USDConversionRate)), 9999)
        ELSE ISNULL(CONVERT(DECIMAL(18, 2), (a.Price * USDConversionRate)), 9999)
        END AS Price
    , CASE ISNULL(a.Quantity, 0)
        WHEN 0 THEN ISNULL(a.[Quantity Available], 0)
        ELSE ISNULL(a.Quantity, 0)
        END AS Quantity
    , CASE WHEN ISNULL(a.Quantity, 0) = 0 AND ISNULL(a.[Quantity Available], 0) > 0 THEN 1
        ELSE 0
        END AS IsFBAed
    , CASE WHEN a.[seller-sku] IS NULL THEN 0
        ELSE 1
        END AS IsRestricted
FROM 
(
SELECT * FROM a 
UNION 
SELECT * FRom B
) a

0 个答案:

没有答案