简体   繁体   English

如何优化此嵌套SQL查询

[英]How to optimize this nested SQL query

Here is the database schema: 这是数据库架构:

[redacted] [已编辑]

I'll describe what I'm doing with the query below: 我将在下面的查询中描述我的工作方式:

Innermost query : Select all the saleId s satisfying the WHERE conditions 最里面的查询 :选择所有满足WHERE条件的saleId

Middle query : Select all the productId s that were a part of the saleId 中间查询 :选择属于saleId所有productId

Outermost query : SUM the products.cost and select the vendors.name . 最外面的查询SUMproducts.cost并选择vendors.name

And here is the SQL query I came up with: 这是我想出的SQL查询:

SELECT vendors.name AS Company
, SUM(products.cost) AS Revenue
FROM
    products
    INNER JOIN sold_products
        ON (products.productId = sold_products.productId)
    INNER JOIN vendors
    ON (products.vendorId = vendors.vendorId)
            WHERE sold_products.productId IN (
                    SELECT sold_products.productId
                    FROM
                        sold_products
                    WHERE sold_products.saleId IN (
                            SELECT sales.saleId     
                            FROM
                                markets
                                INNER JOIN vendors
                                ON (markets.vendorId = vendors.vendorId)
                                INNER JOIN sales_campaign
                                ON (sales_campaign.marketId = markets.marketId)
                                INNER JOIN packet_headers
                                ON (sales_campaign.packetHeaderId = packet_headers.packetHeaderId)
                                INNER JOIN packet_details
                                ON (packet_details.packetHeaderId = packet_headers.packetHeaderId)
                                INNER JOIN sales
                                ON (sales.packetDetailsId = packet_details.packetDetailsId)
                            WHERE vendors.customerId=60
                            )
                    )
GROUP BY Company
ORDER BY Revenue DESC;

Any help in optimizing this? 任何优化方面的帮助吗?

Since you are just using inner joins you normally simplify the query to smth like this: 由于您仅使用内部联接,因此通常将查询简化为如下所示:

SELECT  ve.name     AS Company
,       SUM(pr.cost) AS Revenue
FROM    products        pr
,       sold_products   sp
,       vendors         ve
,       markets         ma
,       sales_campaign  sc
,       packet_headers  ph
,       packet_details  pd
,       sales           sa
Where pr.productId = sp.productId
And   pr.vendorId  = ve.vendorId
And   ve.vendorId  = ma.vendorId
And   sc.marketId = ma.marketId
And   sc.packetHeaderId = ph.packetHeaderId
And   pd.packetHeaderId = ph.packetHeaderId)
And   sa.packetDetailsId = pd.packetDetailsId
And   ve.customerId = 60
GROUP BY ve.Company
ORDER BY pr.Revenue DESC;

Please try if this works and if it is faster and let me know. 请尝试此方法是否可行,是否更快,并让我知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM