简体   繁体   English

SQL查询优化/重构

[英]SQL query optimisation / refactoring

Just wondering can anyone see a neater way to write this query. 只是想知道任何人都可以看到一种更简洁的方式来编写这个查询。 It seems like a lot of repetition to me but I can't see an easier way to write it. 对我来说似乎有很多重复,但我看不出更简单的方法来写它。 It is pulling back Year-to-date data. 它正在拉回年初至今的数据。

Output looks like this: 输出如下:

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

The query is: 查询是:

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]

You could certainly reduce the amount of code in the query by using something like: 您当然可以使用以下内容减少查询中的代码量:

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]

I've eliminated a lot of the duplicated joins and moved from full joins to just left joining the two summary result sets (test drives and sales) to the sales person table. 我已经淘汰了很多重复的连接,并从完全连接移动到左边将两个汇总结果集(测试驱动器和销售)连接到销售人员表。

You haven't given details of your tables or any sample data so it's impossible to test the above query, but hopefully it will give you some ideas on how to write it a bit more concisely. 您没有提供表格或任何示例数据的详细信息,因此无法测试上述查询,但希望它能为您提供有关如何更简洁地编写它的一些想法。

In terms of actual performance, I would imagine simplifying the code will likely help the optimiser, but this is something you would need to confirm in your own environment. 就实际性能而言,我认为简化代码可能会有助于优化器,但这是您需要在自己的环境中确认的。

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

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