繁体   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