C#Linq to Entities使用&&添加第二个条件杀死查询

时间:2017-07-31 16:22:32

标签: c# sql performance entity-framework linq-to-entities

我在使用EF 6的C#6中有一个非常简单的查询,我需要检查两个日期之间的记录。如果我只使用一次检查> =它会在几秒钟内执行,只要我添加第二次测试就需要几乎整整一分钟。 要解决它,我必须针对第一个运行第二个查询,但我不喜欢它。

以下是当前的工作解决方案:

var stopEvents = (from s in dbContext.stop_event
                           join d in dbContext.stop_details on s.stop_id equals d.id
                           where (d.ship_date >= startDate)
                           select new
                                        {
                                            deliveryID = s.id,
                                            stopID = d.id,
                                            d.ship_date,
                                            d.verified_ship_date,
                                            d.cust_ref_1_BOL,
                                            d.cust_ref_2_OrderNum,
                                            d.cust_ref_3_stopID,
                                            d.con_name,
                                            d.con_address1,
                                            d.con_address2,
                                            d.con_city,
                                            con_zip = d.con_zip.Substring(0, 5),
                                            d.customer.customer_name,
                                            d.con_state,
                                            d.lading_qty_total,
                                            d.lading_wgt_total,
                                            d.lading_qty_haz,
                                            d.lading_wgt_haz,
                                            delivery_record_created = s.record_created,
                                            delivery_record_updated = s.record_updated,
                                            stop_record_created = d.record_created,
                                            d.stop_canceled,
                                            d.stop_changed,
                                            d.status,
                                            d.items_del_ytd,
                                            d.wgt_del_ytd,
                                            d.note_from_cust,
                                            noteTest = d.note_from_cust,
                                            d.con_contact_email,
                                            d.con_contact_name,
                                            d.con_contact_phone,
                                            d.del_req_date,
                                            d.customer_id,
                                            d.cust_unique_id,
                                            s.driver_id,
                                            s.event_datetime,
                                            s.received_by,
                                            s.stop_note,
                                            s.edi_shipment_status_codes.Description,
                                            s.exception_status_code,
                                            s.needs_EDI_gen_flag,
                                            s.review_level,
                                            d.eCourier_export_flag,
                                            s.ontime_performance,
                                            s.batch_number
                                        }).ToArray();

        stopEvents = (from s in stopEvents where s.ship_date <= endDate select s).ToArray();

如果我只是做出这个改变,它会“打破”它:

   var stopEvents = (from s in dbContext.stop_event
                           join d in dbContext.stop_details on s.stop_id equals d.id
                           where (d.ship_date >= startDate && d.ship_date <= endDate)
                           select new
                                        {

添加到strangness,运行正常:

  var stopEvents2 = (from s in dbContext.stop_event
                          join d in dbContext.stop_details on s.stop_id equals d.id
                          where d.ship_date >= startDate && d.ship_date <= endDate
                          select d).ToArray();

要检查数据完整性,以下在SSMS中运行良好:

    SELECT *
  FROM FlexEDI.dbo.stop_event
  join FlexEDI.dbo.stop_details on stop_details.id = stop_event.stop_id
  where ship_date >= '7/1/2017' and ship_date <= '7/31/2017'

使用诊断工具,只有一个条件的查询显示了这一点(两者都有相同的选择部分):

 FROM    [dbo].[stop_event] AS [Extent1]
INNER JOIN [dbo].[stop_details] AS [Extent2] ON [Extent1].[stop_id] = [Extent2].[id]
LEFT OUTER JOIN [dbo].[customers] AS [Extent3] ON [Extent2].[customer_id] = [Extent3].[id]
LEFT OUTER JOIN [dbo].[edi_shipment_status_codes] AS [Extent4] ON [Extent1].[exception_status_code] = [Extent4].[code]
WHERE [Extent2].[ship_date] >= @p__linq__0

虽然两者的查询都显示了这个:

    FROM    [dbo].[stop_event] AS [Extent1]
INNER JOIN [dbo].[stop_details] AS [Extent2] ON [Extent1].[stop_id] = [Extent2].[id]
LEFT OUTER JOIN [dbo].[customers] AS [Extent3] ON [Extent2].[customer_id] = [Extent3].[id]
LEFT OUTER JOIN [dbo].[edi_shipment_status_codes] AS [Extent4] ON [Extent1].[exception_status_code] = [Extent4].[code]
WHERE ([Extent2].[ship_date] >= @p__linq__0) AND ([Extent2].[ship_date] <= @p__linq__1)

0 个答案:

没有答案