简体   繁体   English

LINQ-To-SQL - 慢查询

[英]LINQ-To-SQL - slow query

I'm just wondering if anyone can offer any advice on how to improve my query. 我只是想知道是否有人可以就如何改进我的查询提供任何建议。

Basically, it'll be merging 2 rows into 1. The only thing the rows will differ by is a 'Type' char column ('S' or 'C') and the Value. 基本上,它将2行合并为1.行中唯一不同的是'Type'字符列('S'或'C')和值。 What I want to do is select one row, with the 'S' value and the 'C' value, and calculate the difference (SC). 我想要做的是选择一行,使用'S'值和'C'值,并计算差值(SC)。

My query works, but it's pretty slow - it takes around 8 seconds to get the results, which is not ideal for my application. 我的查询有效,但速度很慢 - 获得结果需要大约8秒钟,这对我的应用程序来说并不理想。 I wish I could change the database structure but I can't sadly! 我希望我能改变数据库结构,但我不能遗憾!

Here is my query: 这是我的查询:

var sales = (from cm in dc.ConsignmentMarginBreakdowns
            join sl in dc.SageAccounts on new { LegacyID = cm.Customer, Customer = true } equals new { LegacyID = sl.LegacyID, Customer = sl.Customer }
            join ss in dc.SageAccounts on sl.ParentAccount equals ss.ID
            join vt in dc.VehicleTypes on cm.ConsignmentTripBreakdown.VehicleType.Trim() equals vt.ID.ToString() into vtg
            where cm.ConsignmentTripBreakdown.DeliveryDate >= dates.FromDate && cm.ConsignmentTripBreakdown.DeliveryDate <= dates.ToDate
            where (customer == null || ss.SageID == customer)
            where cm.BreakdownType == 'S'
            orderby cm.Depot, cm.TripNumber
            select new
            {
                NTConsignment = cm.NTConsignment,
                Trip = cm.ConsignmentTripBreakdown,
                LegacyID = cm.LegacyID,
                Costs = dc.ConsignmentMarginBreakdowns.Where(a => a.BreakdownType == 'C' && a.NTConsignment == cm.NTConsignment && a.LegacyID == cm.LegacyID && a.TripDate == cm.TripDate && a.Depot == cm.Depot && a.TripNumber == cm.TripNumber).Single().Value,
                Sales = cm.Value ?? 0.00m,
                Customer = cm.Customer,
                SageID = ss.SageID,
                CustomerName = ss.ShortName,
                FullCustomerName = ss.Name,
                Vehicle = cm.ConsignmentTripBreakdown.Vehicle ?? "None",
                VehicleType = vtg.FirstOrDefault().VehicleTypeDescription ?? "Subcontractor"
            });

There really isn't enough information supplied to make an informed opinion. 实际上没有足够的信息来提供明智的意见。 For example, how many rows in each of the tables? 例如,每个表中有多少行? What does the generated T-SQL look like? 生成的T-SQL是什么样的?

One thing I would suggest first is to take the outputted T-SQL, generate a query plan and look for table or index scans. 我首先建议的一件事是获取输出的T-SQL,生成查询计划并查找表或索引扫描。

A good place to start when optimizing Linq to SQL queries is the SQL Server Profiler. SQL Server Profiler是优化Linq to SQL查询时的一个好起点。 There you can find what SQL code is being generated by Linq to SQL. 在那里,您可以找到Linq to SQL正在生成的SQL代码。 From there, you can toy around with the linq query to see if you can get it to write a better query. 从那里,你可以玩linq查询,看看你是否可以让它写出更好的查询。 If that doesn't work, you can always write a stored procedure by hand, and then call it from Linq to SQL. 如果这不起作用,您可以随时手动编写存储过程,然后将其从Linq调用到SQL。

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

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