簡體   English   中英

SQL查詢優化/重構

[英]SQL query optimisation / refactoring

只是想知道任何人都可以看到一種更簡潔的方式來編寫這個查詢。 對我來說似乎有很多重復,但我看不出更簡單的方法來寫它。 它正在拉回年初至今的數據。

輸出如下:

name  Total New Drives  Total New Sales  Total Used Drives Total Used Sales    
Alan    41                31                 15               93    
Pascal  45                51                 35               33   

查詢是:

select sp.name, x.ttlNewTestDrives, x.ttlNewSales, y.ttlUsedTestDrives, y.TtlUsedSales
from
(
    select ts.SalesPersonID, ttd.TotalTestDrives as TtlNewTestDrives ,ts.TotalSales as TtlNewSales
    from
    (       
            select  sp.[SalesPersonID], count([TestDriveID]) as TotalTestDrives
            from SalesPeople sp
            join TestDrives td 
            on sp.[SalesPersonID] = td.[SalesPerson_SalesPersonID]
            join cars c on
            c.[CarID] = td.[Car_CarID]
            where c.CarType = 'New'
            group by sp.[SalesPersonID]
        ) as ttd
    full outer join
        (
            select sp.[SalesPersonID], count(SaleID) as TotalSales
            from Sales s
            join SalesPeople sp
            on s.[Salesperson_SalesPersonID] = sp.[SalesPersonID]
            join cars c on
            s.[Car_CarID] = c.[CarID]
            where c.CarType = 'New'
            group by sp.[SalesPersonID]
        ) as ts
    on ts.[SalesPersonID] = ttd.[SalesPersonID]
    ) as x
full outer join
(   
        select ttd.SalesPersonID, ttd.TotalTestDrives as TtlUsedTestDrives ,ts.TotalSales as TtlUsedSales
        from
        (       
                select  sp.[SalesPersonID], count([TestDriveID]) as TotalTestDrives
                from SalesPeople sp
                join TestDrives td 
                on sp.[SalesPersonID] = td.[SalesPerson_SalesPersonID]
                join cars c on
                c.[CarID] = td.[Car_CarID]
                where c.CarType = 'Used'
                group by sp.[SalesPersonID]
            ) as ttd
        full outer join
            (
                select sp.[SalesPersonID], count(SaleID) as TotalSales
                from Sales s
                join SalesPeople sp
                on s.[Salesperson_SalesPersonID] = sp.[SalesPersonID]
                join cars c on
                s.[Car_CarID] = c.[CarID]
                where c.CarType = 'Used'
                group by sp.[SalesPersonID]
            ) as ts
        on ts.[SalesPersonID] = ttd.[SalesPersonID]
    ) y
on x.[SalesPersonID] = y.[SalesPersonID]
join SalesPeople sp
on x.[SalesPersonID] = sp.[SalesPersonID]

您當然可以使用以下內容減少查詢中的代碼量:

select sp.name, td.NewTestDrives, s.NewSales, td.UsedTestDrives, s.NewSales
from SalesPeople sp
  left join
  (
    select td.[SalesPerson_SalesPersonID]
      , NewTestDrives = sum(case when c.CarType = 'New' then 1 else 0 end)
      , UsedTestDrives = sum(case when c.CarType = 'Used' then 1 else 0 end)
    from TestDrives td 
      inner join cars c on c.[CarID] = td.[Car_CarID]
  ) td on sp.SalesPersonID = td.[SalesPerson_SalesPersonID]
  left join
  (
    select s.[SalesPerson_SalesPersonID]
      , NewSales = sum(case when c.CarType = 'New' then 1 else 0 end)
      , UsedSales = sum(case when c.CarType = 'Used' then 1 else 0 end)
    from Sales s 
      inner join cars c on c.[CarID] = s.[Car_CarID]
  ) s on sp.SalesPersonID = s.[SalesPerson_SalesPersonID]

我已經淘汰了很多重復的連接,並從完全連接移動到左邊將兩個匯總結果集(測試驅動器和銷售)連接到銷售人員表。

您沒有提供表格或任何示例數據的詳細信息,因此無法測試上述查詢,但希望它能為您提供有關如何更簡潔地編寫它的一些想法。

就實際性能而言,我認為簡化代碼可能會有助於優化器,但這是您需要在自己的環境中確認的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM