TimeOut expired : Sql Exception while using nHibernate - sql-server

After checking more details , I came to know about the below findings. I was unable to get the inner exceptions so I ran it in fiddler to check if I could get some information out of it.Fiddler had the below details
[GenericADOException: could not execute query [ SELECT this_.CompanyId
as CompanyId0_30_, this_.HooversCompanyId as HooversC2_0_30_,
this_.DUNS as DUNS0_30_, this_.CompanyName as CompanyN4_0_30_,
this_.AddressLine1 as AddressL5_0_30_, this_.AddressLine2 as
AddressL6_0_30_, this_.State as State0_30_, this_.City as City0_30_,
this_.ZipCode as ZipCode0_30_, this_.CountryTelephoneAccessCode as
Country10_0_30_, this_.TelephoneNumber as Telepho11_0_30_,
this_.FaxNumber as FaxNumber0_30_, this_.Revenue as Revenue0_30_,
this_.FinancialYearEndDate as Financi14_0_30_, this_.EmployeesTotal as
Employe15_0_30_, this_.EmployeesHereTotal as Employe16_0_30_,
this_.LegalStructure as LegalSt17_0_30_, this_.ProfitLoss as
ProfitLoss0_30_, this_.NetWorth as NetWorth0_30_,
this_.HasSubsidariesIndicator as HasSubs20_0_30_,
this_.ITSpendingUpdateDate as ITSpend21_0_30_, this_.ITSpending as
ITSpending0_30_, this_.ITSpendingNextYearPrediction as
ITSpend23_0_30_, this_.ITSpendingGrowth as ITSpend24_0_30_,
this_.AuditCreatedDate as AuditCr25_0_30_, this_.AuditModifiedDate as
AuditMo26_0_30_, this_.Website as Website0_30_, this_.OwnershipTypeId
as Ownersh28_0_30_, this_.CompanyStatusId as Company29_0_30_,
this_.RecordTypeId as RecordT30_0_30_, this_.LocationTypeId as
Locatio31_0_30_, this_.ZipCodeId as ZipCodeId0_30_,
this_.PrimarySIC_Code as PrimarySIC33_0_30_, this_.StateId as
StateId0_30_, this_.CountryId as CountryId0_30_, this_.SWOTAnalysisId
as SWOTAna36_0_30_, primaryind2_.CompanyId as CompanyId32_,
primaryind2_.CompanyIndustryId as CompanyI1_32_,
primaryind2_.CompanyIndustryId as CompanyI1_8_0_,
primaryind2_.IsPrimary as IsPrimary8_0_, primaryind2_.CompanyId as
CompanyId8_0_, primaryind2_.HIC as HIC8_0_, industry3_.HIC as HIC6_1_,
industry3_.Name as Name6_1_, industry3_.Level as Level6_1_,
industry3_.ParentHIC as ParentHIC6_1_, itspending4_.CompanyId as
CompanyId33_, itspending4_.ITSpendingId as ITSpendi1_33_,
itspending4_.ITSpendingId as ITSpendi1_14_2_, itspending4_.Amount as
Amount14_2_, itspending4_.CompanyId as CompanyId14_2_,
itspending4_.ITSpendingTypeId as ITSpendi3_14_2_,
itspending5_.ITSpendingTypeID as ITSpendi1_15_3_, itspending5_.Name as
Name15_3_, itspending5_.ParentID as ParentID15_3_,
hardware6_.CompanyId as CompanyId34_, hardware6_.ITSpendingId as
ITSpendi1_34_, hardware6_.ITSpendingId as ITSpendi1_14_4_,
hardware6_.Amount as Amount14_4_, hardware6_.CompanyId as
CompanyId14_4_, hardware6_.ITSpendingTypeId as ITSpendi3_14_4_,
computingp7_.CompanyId as CompanyId35_, computingp7_.ITSpendingId as
ITSpendi1_35_, computingp7_.ITSpendingId as ITSpendi1_14_5_,
computingp7_.Amount as Amount14_5_, computingp7_.CompanyId as
CompanyId14_5_, computingp7_.ITSpendingTypeId as ITSpendi3_14_5_,
networking8_.CompanyId as CompanyId36_, networking8_.ITSpendingId as
ITSpendi1_36_, networking8_.ITSpendingId as ITSpendi1_14_6_,
networking8_.Amount as Amount14_6_, networking8_.CompanyId as
CompanyId14_6_, networking8_.ITSpendingTypeId as ITSpendi3_14_6_,
serversand9_.CompanyId as CompanyId37_, serversand9_.ITSpendingId as
ITSpendi1_37_, serversand9_.ITSpendingId as ITSpendi1_14_7_,
serversand9_.Amount as Amount14_7_, serversand9_.CompanyId as
CompanyId14_7_, serversand9_.ITSpendingTypeId as ITSpendi3_14_7_,
software10_.CompanyId as CompanyId38_, software10_.ITSpendingId as
ITSpendi1_38_, software10_.ITSpendingId as ITSpendi1_14_8_,
software10_.Amount as Amount14_8_, software10_.CompanyId as
CompanyId14_8_, software10_.ITSpendingTypeId as ITSpendi3_14_8_,
softwareli11_.CompanyId as CompanyId39_, softwareli11_.ITSpendingId as
ITSpendi1_39_, softwareli11_.ITSpendingId as ITSpendi1_14_9_,
softwareli11_.Amount as Amount14_9_, softwareli11_.CompanyId as
CompanyId14_9_, softwareli11_.ITSpendingTypeId as ITSpendi3_14_9_,
supportand12_.CompanyId as CompanyId40_, supportand12_.ITSpendingId as
ITSpendi1_40_, supportand12_.ITSpendingId as ITSpendi1_14_10_,
supportand12_.Amount as Amount14_10_, supportand12_.CompanyId as
CompanyId14_10_, supportand12_.ITSpendingTypeId as ITSpendi3_14_10_,
services13_.CompanyId as CompanyId41_, services13_.ITSpendingId as
ITSpendi1_41_, services13_.ITSpendingId as ITSpendi1_14_11_,
services13_.Amount as Amount14_11_, services13_.CompanyId as
CompanyId14_11_, services13_.ITSpendingTypeId as ITSpendi3_14_11_,
applicatio14_.CompanyId as CompanyId42_, applicatio14_.ITSpendingId as
ITSpendi1_42_, applicatio14_.ITSpendingId as ITSpendi1_14_12_,
applicatio14_.Amount as Amount14_12_, applicatio14_.CompanyId as
CompanyId14_12_, applicatio14_.ITSpendingTypeId as ITSpendi3_14_12_,
infrastruc15_.CompanyId as CompanyId43_, infrastruc15_.ITSpendingId as
ITSpendi1_43_, infrastruc15_.ITSpendingId as ITSpendi1_14_13_,
infrastruc15_.Amount as Amount14_13_, infrastruc15_.CompanyId as
CompanyId14_13_, infrastruc15_.ITSpendingTypeId as ITSpendi3_14_13_,
helpdeskan16_.CompanyId as CompanyId44_, helpdeskan16_.ITSpendingId as
ITSpendi1_44_, helpdeskan16_.ITSpendingId as ITSpendi1_14_14_,
helpdeskan16_.Amount as Amount14_14_, helpdeskan16_.CompanyId as
CompanyId14_14_, helpdeskan16_.ITSpendingTypeId as ITSpendi3_14_14_,
communicat17_.CompanyId as CompanyId45_, communicat17_.ITSpendingId as
ITSpendi1_45_, communicat17_.ITSpendingId as ITSpendi1_14_15_,
communicat17_.Amount as Amount14_15_, communicat17_.CompanyId as
CompanyId14_15_, communicat17_.ITSpendingTypeId as ITSpendi3_14_15_,
voiceservi18_.CompanyId as CompanyId46_, voiceservi18_.ITSpendingId as
ITSpendi1_46_, voiceservi18_.ITSpendingId as ITSpendi1_14_16_,
voiceservi18_.Amount as Amount14_16_, voiceservi18_.CompanyId as
CompanyId14_16_, voiceservi18_.ITSpendingTypeId as ITSpendi3_14_16_,
dataservic19_.CompanyId as CompanyId47_, dataservic19_.ITSpendingId as
ITSpendi1_47_, dataservic19_.ITSpendingId as ITSpendi1_14_17_,
dataservic19_.Amount as Amount14_17_, dataservic19_.CompanyId as
CompanyId14_17_, dataservic19_.ITSpendingTypeId as ITSpendi3_14_17_,
convergeds20_.CompanyId as CompanyId48_, convergeds20_.ITSpendingId as
ITSpendi1_48_, convergeds20_.ITSpendingId as ITSpendi1_14_18_,
convergeds20_.Amount as Amount14_18_, convergeds20_.CompanyId as
CompanyId14_18_, convergeds20_.ITSpendingTypeId as ITSpendi3_14_18_,
consulting21_.CompanyId as CompanyId49_, consulting21_.ITSpendingId as
ITSpendi1_49_, consulting21_.ITSpendingId as ITSpendi1_14_19_,
consulting21_.Amount as Amount14_19_, consulting21_.CompanyId as
CompanyId14_19_, consulting21_.ITSpendingTypeId as ITSpendi3_14_19_,
systemspla22_.CompanyId as CompanyId50_, systemspla22_.ITSpendingId as
ITSpendi1_50_, systemspla22_.ITSpendingId as ITSpendi1_14_20_,
systemspla22_.Amount as Amount14_20_, systemspla22_.CompanyId as
CompanyId14_20_, systemspla22_.ITSpendingTypeId as ITSpendi3_14_20_,
disasterre23_.CompanyId as CompanyId51_, disasterre23_.ITSpendingId as
ITSpendi1_51_, disasterre23_.ITSpendingId as ITSpendi1_14_21_,
disasterre23_.Amount as Amount14_21_, disasterre23_.CompanyId as
CompanyId14_21_, disasterre23_.ITSpendingTypeId as ITSpendi3_14_21_,
traininged24_.CompanyId as CompanyId52_, traininged24_.ITSpendingId as
ITSpendi1_52_, traininged24_.ITSpendingId as ITSpendi1_14_22_,
traininged24_.Amount as Amount14_22_, traininged24_.CompanyId as
CompanyId14_22_, traininged24_.ITSpendingTypeId as ITSpendi3_14_22_,
internalit25_.CompanyId as CompanyId53_, internalit25_.ITSpendingId as
ITSpendi1_53_, internalit25_.ITSpendingId as ITSpendi1_14_23_,
internalit25_.Amount as Amount14_23_, internalit25_.CompanyId as
CompanyId14_23_, internalit25_.ITSpendingTypeId as ITSpendi3_14_23_,
telco26_.CompanyId as CompanyId54_, telco26_.ITSpendingId as
ITSpendi1_54_, telco26_.ITSpendingId as ITSpendi1_14_24_,
telco26_.Amount as Amount14_24_, telco26_.CompanyId as
CompanyId14_24_, telco26_.ITSpendingTypeId as ITSpendi3_14_24_,
systemsint27_.CompanyId as CompanyId55_, systemsint27_.ITSpendingId as
ITSpendi1_55_, systemsint27_.ITSpendingId as ITSpendi1_14_25_,
systemsint27_.Amount as Amount14_25_, systemsint27_.CompanyId as
CompanyId14_25_, systemsint27_.ITSpendingTypeId as ITSpendi3_14_25_,
zipcode28_.ZipCodeId as ZipCodeId27_26_, zipcode28_.ZipCode as
ZipCode27_26_, siccode29_.Code as Code20_27_, siccode29_.Name as
Name20_27_, country30_.CountryId as CountryId4_28_, country30_.Name as
Name4_28_, country30_.GeographyId as Geograph3_4_28_,
contractsa31_.CompanyId as CompanyId3_29_,
contractsa31_.ActiveContractValue as ActiveCo2_3_29_,
contractsa31_.ActiveContractVolume as ActiveCo3_3_29_,
contractsa31_.TotalContractValue as TotalCon4_3_29_,
contractsa31_.TotalContractVolume as TotalCon5_3_29_,
contractsa31_.RelationshipVendor as Relation6_3_29_ FROM Company this_
left outer join CompanyIndustry primaryind2_ on
this_.CompanyId=primaryind2_.CompanyId and (primaryind2_.IsPrimary =
1) left outer join HooversIndustry industry3_ on
primaryind2_.HIC=industry3_.HIC left outer join ITSpending
itspending4_ on this_.CompanyId=itspending4_.CompanyId left outer join
ITSpendingType itspending5_ on
itspending4_.ITSpendingTypeId=itspending5_.ITSpendingTypeID left outer
join ITSpending hardware6_ on this_.CompanyId=hardware6_.CompanyId and
(hardware6_.ITSpendingTypeId = 2) left outer join ITSpending
computingp7_ on this_.CompanyId=computingp7_.CompanyId and
(computingp7_.ITSpendingTypeId = 12) left outer join ITSpending
networking8_ on this_.CompanyId=networking8_.CompanyId and
(networking8_.ITSpendingTypeId = 11) left outer join ITSpending
serversand9_ on this_.CompanyId=serversand9_.CompanyId and
(serversand9_.ITSpendingTypeId = 10) left outer join ITSpending
software10_ on this_.CompanyId=software10_.CompanyId and
(software10_.ITSpendingTypeId = 3) left outer join ITSpending
softwareli11_ on this_.CompanyId=softwareli11_.CompanyId and
(softwareli11_.ITSpendingTypeId = 15) left outer join ITSpending
supportand12_ on this_.CompanyId=supportand12_.CompanyId and
(supportand12_.ITSpendingTypeId = 16) left outer join ITSpending
services13_ on this_.CompanyId=services13_.CompanyId and
(services13_.ITSpendingTypeId = 4) left outer join ITSpending
applicatio14_ on this_.CompanyId=applicatio14_.CompanyId and
(applicatio14_.ITSpendingTypeId = 19) left outer join ITSpending
infrastruc15_ on this_.CompanyId=infrastruc15_.CompanyId and
(infrastruc15_.ITSpendingTypeId = 20) left outer join ITSpending
helpdeskan16_ on this_.CompanyId=helpdeskan16_.CompanyId and
(helpdeskan16_.ITSpendingTypeId = 21) left outer join ITSpending
communicat17_ on this_.CompanyId=communicat17_.CompanyId and
(communicat17_.ITSpendingTypeId = 5) left outer join ITSpending
voiceservi18_ on this_.CompanyId=voiceservi18_.CompanyId and
(voiceservi18_.ITSpendingTypeId = 24) left outer join ITSpending
dataservic19_ on this_.CompanyId=dataservic19_.CompanyId and
(dataservic19_.ITSpendingTypeId = 25) left outer join ITSpending
convergeds20_ on this_.CompanyId=convergeds20_.CompanyId and
(convergeds20_.ITSpendingTypeId = 26) left outer join ITSpending
consulting21_ on this_.CompanyId=consulting21_.CompanyId and
(consulting21_.ITSpendingTypeId = 6) left outer join ITSpending
systemspla22_ on this_.CompanyId=systemspla22_.CompanyId and
(systemspla22_.ITSpendingTypeId = 29) left outer join ITSpending
disasterre23_ on this_.CompanyId=disasterre23_.CompanyId and
(disasterre23_.ITSpendingTypeId = 30) left outer join ITSpending
traininged24_ on this_.CompanyId=traininged24_.CompanyId and
(traininged24_.ITSpendingTypeId = 31) left outer join ITSpending
internalit25_ on this_.CompanyId=internalit25_.CompanyId and
(internalit25_.ITSpendingTypeId = 38) left outer join ITSpending
telco26_ on this_.CompanyId=telco26_.CompanyId and
(telco26_.ITSpendingTypeId = 35) left outer join ITSpending
systemsint27_ on this_.CompanyId=systemsint27_.CompanyId and
(systemsint27_.ITSpendingTypeId = 36) left outer join ZipCode
zipcode28_ on this_.ZipCodeId=zipcode28_.ZipCodeId left outer join
SICCode siccode29_ on this_.PrimarySIC_Code=siccode29_.Code left outer
join Country country30_ on this_.CountryId=country30_.CountryId left
outer join CompanyContractAggregation contractsa31_ on
this_.CompanyId=contractsa31_.CompanyId WHERE this_.CompanyId in (#p0)
] Name:cp0 - Value:3506 [SQL: SELECT this_.CompanyId as
CompanyId0_30_, this_.HooversCompanyId as HooversC2_0_30_, this_.DUNS
as DUNS0_30_, this_.CompanyName as CompanyN4_0_30_, this_.AddressLine1
as AddressL5_0_30_, this_.AddressLine2 as AddressL6_0_30_, this_.State
as State0_30_, this_.City as City0_30_, this_.ZipCode as ZipCode0_30_,
this_.CountryTelephoneAccessCode as Country10_0_30_,
this_.TelephoneNumber as Telepho11_0_30_, this_.FaxNumber as
FaxNumber0_30_, this_.Revenue as Revenue0_30_,
this_.FinancialYearEndDate as Financi14_0_30_, this_.EmployeesTotal as
Employe15_0_30_, this_.EmployeesHereTotal as Employe16_0_30_,
this_.LegalStructure as LegalSt17_0_30_, this_.ProfitLoss as
ProfitLoss0_30_, this_.NetWorth as NetWorth0_30_,
this_.HasSubsidariesIndicator as HasSubs20_0_30_,
this_.ITSpendingUpdateDate as ITSpend21_0_30_, this_.ITSpending as
ITSpending0_30_, this_.ITSpendingNextYearPrediction as
ITSpend23_0_30_, this_.ITSpendingGrowth as ITSpend24_0_30_,
this_.AuditCreatedDate as AuditCr25_0_30_, this_.AuditModifiedDate as
AuditMo26_0_30_, this_.Website as Website0_30_, this_.OwnershipTypeId
as Ownersh28_0_30_, this_.CompanyStatusId as Company29_0_30_,
this_.RecordTypeId as RecordT30_0_30_, this_.LocationTypeId as
Locatio31_0_30_, this_.ZipCodeId as ZipCodeId0_30_,
this_.PrimarySIC_Code as PrimarySIC33_0_30_, this_.StateId as
StateId0_30_, this_.CountryId as CountryId0_30_, this_.SWOTAnalysisId
as SWOTAna36_0_30_, primaryind2_.CompanyId as CompanyId32_,
primaryind2_.CompanyIndustryId as CompanyI1_32_,
primaryind2_.CompanyIndustryId as CompanyI1_8_0_,
primaryind2_.IsPrimary as IsPrimary8_0_, primaryind2_.CompanyId as
CompanyId8_0_, primaryind2_.HIC as HIC8_0_, industry3_.HIC as HIC6_1_,
industry3_.Name as Name6_1_, industry3_.Level as Level6_1_,
industry3_.ParentHIC as ParentHIC6_1_, itspending4_.CompanyId as
CompanyId33_, itspending4_.ITSpendingId as ITSpendi1_33_,
itspending4_.ITSpendingId as ITSpendi1_14_2_, itspending4_.Amount as
Amount14_2_, itspending4_.CompanyId as CompanyId14_2_,
itspending4_.ITSpendingTypeId as ITSpendi3_14_2_,
itspending5_.ITSpendingTypeID as ITSpendi1_15_3_, itspending5_.Name as
Name15_3_, itspending5_.ParentID as ParentID15_3_,
hardware6_.CompanyId as CompanyId34_, hardware6_.ITSpendingId as
ITSpendi1_34_, hardware6_.ITSpendingId as ITSpendi1_14_4_,
hardware6_.Amount as Amount14_4_, hardware6_.CompanyId as
CompanyId14_4_, hardware6_.ITSpendingTypeId as ITSpendi3_14_4_,
computingp7_.CompanyId as CompanyId35_, computingp7_.ITSpendingId as
ITSpendi1_35_, computingp7_.ITSpendingId as ITSpendi1_14_5_,
computingp7_.Amount as Amount14_5_, computingp7_.CompanyId as
CompanyId14_5_, computingp7_.ITSpendingTypeId as ITSpendi3_14_5_,
networking8_.CompanyId as CompanyId36_, networking8_.ITSpendingId as
ITSpendi1_36_, networking8_.ITSpendingId as ITSpendi1_14_6_,
networking8_.Amount as Amount14_6_, networking8_.CompanyId as
CompanyId14_6_, networking8_.ITSpendingTypeId as ITSpendi3_14_6_,
serversand9_.CompanyId as CompanyId37_, serversand9_.ITSpendingId as
ITSpendi1_37_, serversand9_.ITSpendingId as ITSpendi1_14_7_,
serversand9_.Amount as Amount14_7_, serversand9_.CompanyId as
CompanyId14_7_, serversand9_.ITSpendingTypeId as ITSpendi3_14_7_,
software10_.CompanyId as CompanyId38_, software10_.ITSpendingId as
ITSpendi1_38_, software10_.ITSpendingId as ITSpendi1_14_8_,
software10_.Amount as Amount14_8_, software10_.CompanyId as
CompanyId14_8_, software10_.ITSpendingTypeId as ITSpendi3_14_8_,
softwareli11_.CompanyId as CompanyId39_, softwareli11_.ITSpendingId as
ITSpendi1_39_, softwareli11_.ITSpendingId as ITSpendi1_14_9_,
softwareli11_.Amount as Amount14_9_, softwareli11_.CompanyId as
CompanyId14_9_, softwareli11_.ITSpendingTypeId as ITSpendi3_14_9_,
supportand12_.CompanyId as CompanyId40_, supportand12_.ITSpendingId as
ITSpendi1_40_, supportand12_.ITSpendingId as ITSpendi1_14_10_,
supportand12_.Amount as Amount14_10_, supportand12_.CompanyId as
CompanyId14_10_, supportand12_.ITSpendingTypeId as ITSpendi3_14_10_,
services13_.CompanyId as CompanyId41_, services13_.ITSpendingId as
ITSpendi1_41_, services13_.ITSpendingId as ITSpendi1_14_11_,
services13_.Amount as Amount14_11_, services13_.CompanyId as
CompanyId14_11_, services13_.ITSpendingTypeId as ITSpendi3_14_11_,
applicatio14_.CompanyId as CompanyId42_, applicatio14_.ITSpendingId as
ITSpendi1_42_, applicatio14_.ITSpendingId as ITSpendi1_14_12_,
applicatio14_.Amount as Amount14_12_, applicatio14_.CompanyId as
CompanyId14_12_, applicatio14_.ITSpendingTypeId as ITSpendi3_14_12_,
infrastruc15_.CompanyId as CompanyId43_, infrastruc15_.ITSpendingId as
ITSpendi1_43_, infrastruc15_.ITSpendingId as ITSpendi1_14_13_,
infrastruc15_.Amount as Amount14_13_, infrastruc15_.CompanyId as
CompanyId14_13_, infrastruc15_.ITSpendingTypeId as ITSpendi3_14_13_,
helpdeskan16_.CompanyId as CompanyId44_, helpdeskan16_.ITSpendingId as
ITSpendi1_44_, helpdeskan16_.ITSpendingId as ITSpendi1_14_14_,
helpdeskan16_.Amount as Amount14_14_, helpdeskan16_.CompanyId as
CompanyId14_14_, helpdeskan16_.ITSpendingTypeId as ITSpendi3_14_14_,
communicat17_.CompanyId as CompanyId45_, communicat17_.ITSpendingId as
ITSpendi1_45_, communicat17_.ITSpendingId as ITSpendi1_14_15_,
communicat17_.Amount as Amount14_15_, communicat17_.CompanyId as
CompanyId14_15_, communicat17_.ITSpendingTypeId as ITSpendi3_14_15_,
voiceservi18_.CompanyId as CompanyId46_, voiceservi18_.ITSpendingId as
ITSpendi1_46_, voiceservi18_.ITSpendingId as ITSpendi1_14_16_,
voiceservi18_.Amount as Amount14_16_, voiceservi18_.CompanyId as
CompanyId14_16_, voiceservi18_.ITSpendingTypeId as ITSpendi3_14_16_,
dataservic19_.CompanyId as CompanyId47_, dataservic19_.ITSpendingId as
ITSpendi1_47_, dataservic19_.ITSpendingId as ITSpendi1_14_17_,
dataservic19_.Amount as Amount14_17_, dataservic19_.CompanyId as
CompanyId14_17_, dataservic19_.ITSpendingTypeId as ITSpendi3_14_17_,
convergeds20_.CompanyId as CompanyId48_, convergeds20_.ITSpendingId as
ITSpendi1_48_, convergeds20_.ITSpendingId as ITSpendi1_14_18_,
convergeds20_.Amount as Amount14_18_, convergeds20_.CompanyId as
CompanyId14_18_, convergeds20_.ITSpendingTypeId as ITSpendi3_14_18_,
consulting21_.CompanyId as CompanyId49_, consulting21_.ITSpendingId as
ITSpendi1_49_, consulting21_.ITSpendingId as ITSpendi1_14_19_,
consulting21_.Amount as Amount14_19_, consulting21_.CompanyId as
CompanyId14_19_, consulting21_.ITSpendingTypeId as ITSpendi3_14_19_,
systemspla22_.CompanyId as CompanyId50_, systemspla22_.ITSpendingId as
ITSpendi1_50_, systemspla22_.ITSpendingId as ITSpendi1_14_20_,
systemspla22_.Amount as Amount14_20_, systemspla22_.CompanyId as
CompanyId14_20_, systemspla22_.ITSpendingTypeId as ITSpendi3_14_20_,
disasterre23_.CompanyId as CompanyId51_, disasterre23_.ITSpendingId as
ITSpendi1_51_, disasterre23_.ITSpendingId as ITSpendi1_14_21_,
disasterre23_.Amount as Amount14_21_, disasterre23_.CompanyId as
CompanyId14_21_, disasterre23_.ITSpendingTypeId as ITSpendi3_14_21_,
traininged24_.CompanyId as CompanyId52_, traininged24_.ITSpendingId as
ITSpendi1_52_, traininged24_.ITSpendingId as ITSpendi1_14_22_,
traininged24_.Amount as Amount14_22_, traininged24_.CompanyId as
CompanyId14_22_, traininged24_.ITSpendingTypeId as ITSpendi3_14_22_,
internalit25_.CompanyId as CompanyId53_, internalit25_.ITSpendingId as
ITSpendi1_53_, internalit25_.ITSpendingId as ITSpendi1_14_23_,
internalit25_.Amount as Amount14_23_, internalit25_.CompanyId as
CompanyId14_23_, internalit25_.ITSpendingTypeId as ITSpendi3_14_23_,
telco26_.CompanyId as CompanyId54_, telco26_.ITSpendingId as
ITSpendi1_54_, telco26_.ITSpendingId as ITSpendi1_14_24_,
telco26_.Amount as Amount14_24_, telco26_.CompanyId as
CompanyId14_24_, telco26_.ITSpendingTypeId as ITSpendi3_14_24_,
systemsint27_.CompanyId as CompanyId55_, systemsint27_.ITSpendingId as
ITSpendi1_55_, systemsint27_.ITSpendingId as ITSpendi1_14_25_,
systemsint27_.Amount as Amount14_25_, systemsint27_.CompanyId as
CompanyId14_25_, systemsint27_.ITSpendingTypeId as ITSpendi3_14_25_,
zipcode28_.ZipCodeId as ZipCodeId27_26_, zipcode28_.ZipCode as
ZipCode27_26_, siccode29_.Code as Code20_27_, siccode29_.Name as
Name20_27_, country30_.CountryId as CountryId4_28_, country30_.Name as
Name4_28_, country30_.GeographyId as Geograph3_4_28_,
contractsa31_.CompanyId as CompanyId3_29_,
contractsa31_.ActiveContractValue as ActiveCo2_3_29_,
contractsa31_.ActiveContractVolume as ActiveCo3_3_29_,
contractsa31_.TotalContractValue as TotalCon4_3_29_,
contractsa31_.TotalContractVolume as TotalCon5_3_29_,
contractsa31_.RelationshipVendor as Relation6_3_29_ FROM Company this_
left outer join CompanyIndustry primaryind2_ on
this_.CompanyId=primaryind2_.CompanyId and (primaryind2_.IsPrimary =
1) left outer join HooversIndustry industry3_ on
primaryind2_.HIC=industry3_.HIC left outer join ITSpending
itspending4_ on this_.CompanyId=itspending4_.CompanyId left outer join
ITSpendingType itspending5_ on
itspending4_.ITSpendingTypeId=itspending5_.ITSpendingTypeID left outer
join ITSpending hardware6_ on this_.CompanyId=hardware6_.CompanyId and
(hardware6_.ITSpendingTypeId = 2) left outer join ITSpending
computingp7_ on this_.CompanyId=computingp7_.CompanyId and
(computingp7_.ITSpendingTypeId = 12) left outer join ITSpending
networking8_ on this_.CompanyId=networking8_.CompanyId and
(networking8_.ITSpendingTypeId = 11) left outer join ITSpending
serversand9_ on this_.CompanyId=serversand9_.CompanyId and
(serversand9_.ITSpendingTypeId = 10) left outer join ITSpending
software10_ on this_.CompanyId=software10_.CompanyId and
(software10_.ITSpendingTypeId = 3) left outer join ITSpending
softwareli11_ on this_.CompanyId=softwareli11_.CompanyId and
(softwareli11_.ITSpendingTypeId = 15) left outer join ITSpending
supportand12_ on this_.CompanyId=supportand12_.CompanyId and
(supportand12_.ITSpendingTypeId = 16) left outer join ITSpending
services13_ on this_.CompanyId=services13_.CompanyId and
(services13_.ITSpendingTypeId = 4) left outer join ITSpending
applicatio14_ on this_.CompanyId=applicatio14_.CompanyId and
(applicatio14_.ITSpendingTypeId = 19) left outer join ITSpending
infrastruc15_ on this_.CompanyId=infrastruc15_.CompanyId and
(infrastruc15_.ITSpendingTypeId = 20) left outer join ITSpending
helpdeskan16_ on this_.CompanyId=helpdeskan16_.CompanyId and
(helpdeskan16_.ITSpendingTypeId = 21) left outer join ITSpending
communicat17_ on this_.CompanyId=communicat17_.CompanyId and
(communicat17_.ITSpendingTypeId = 5) left outer join ITSpending
voiceservi18_ on this_.CompanyId=voiceservi18_.CompanyId and
(voiceservi18_.ITSpendingTypeId = 24) left outer join ITSpending
dataservic19_ on this_.CompanyId=dataservic19_.CompanyId and
(dataservic19_.ITSpendingTypeId = 25) left outer join ITSpending
convergeds20_ on this_.CompanyId=convergeds20_.CompanyId and
(convergeds20_.ITSpendingTypeId = 26) left outer join ITSpending
consulting21_ on this_.CompanyId=consulting21_.CompanyId and
(consulting21_.ITSpendingTypeId = 6) left outer join ITSpending
systemspla22_ on this_.CompanyId=systemspla22_.CompanyId and
(systemspla22_.ITSpendingTypeId = 29) left outer join ITSpending
disasterre23_ on this_.CompanyId=disasterre23_.CompanyId and
(disasterre23_.ITSpendingTypeId = 30) left outer join ITSpending
traininged24_ on this_.CompanyId=traininged24_.CompanyId and
(traininged24_.ITSpendingTypeId = 31) left outer join ITSpending
internalit25_ on this_.CompanyId=internalit25_.CompanyId and
(internalit25_.ITSpendingTypeId = 38) left outer join ITSpending
telco26_ on this_.CompanyId=telco26_.CompanyId and
(telco26_.ITSpendingTypeId = 35) left outer join ITSpending
systemsint27_ on this_.CompanyId=systemsint27_.CompanyId and
(systemsint27_.ITSpendingTypeId = 36) left outer join ZipCode
zipcode28_ on this_.ZipCodeId=zipcode28_.ZipCodeId left outer join
SICCode siccode29_ on this_.PrimarySIC_Code=siccode29_.Code left outer
join Country country30_ on this_.CountryId=country30_.CountryId left
outer join CompanyContractAggregation contractsa31_ on
this_.CompanyId=contractsa31_.CompanyId WHERE this_.CompanyId in
(#p0)]] NHibernate.Loader.Loader.DoList(ISessionImplementor
session, QueryParameters queryParameters) +637
NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor
session, QueryParameters queryParameters) +23
NHibernate.Loader.Criteria.CriteriaLoader.List(ISessionImplementor
session) +60 NHibernate.Impl.SessionImpl.List(CriteriaImpl
criteria, IList results) +1025
NHibernate.Impl.CriteriaImpl.List(IList results) +63
NHibernate.Impl.CriteriaImpl.List() +79
Ovum.Ictop.Dal.CustomRepositories.CompanyRepository.Criteria(CriteriaInstance
criteria, Nullable1 pageIndex, Nullable1 pageSize, Nullable1
maxCount) in
c:\GIT-ICTOP\Ovum_Ictop\Ovum.Ictop.Admin.Dal\CustomRepositories\CompanyRepository.cs:69 Ovum.Ictop.Bl.ServiceWrappers.CompanyService.GetCompanyByCriteria(CriteriaInstance
criteria, Nullable1 pageIndex, Nullable1 pageSize, Nullable1
maxCount) in
c:\GIT-ICTOP\Ovum_Ictop\Ovum.Ictop.Bl\ServiceWrappers\CompanyService.cs:289
Ovum.Ictop.Web.Areas.CA.Controllers.SearchController.GetCompanyByLikeName(String
name, Int32 numberPage) in
c:\GIT-ICTOP\Ovum_Ictop\Ovum.Ictop.Web\Areas\CA\Controllers\SearchController.cs:45
Ovum.Ictop.Web.Areas.CA.Controllers.SearchController.Search(String
companyName) in
c:\GIT-ICTOP\Ovum_Ictop\Ovum.Ictop.Web\Areas\CA\Controllers\SearchController.cs:29
lambda_method(Closure , ControllerBase , Object[] ) +127
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext
controllerContext, IDictionary2 parameters) +264
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext
controllerContext, ActionDescriptor actionDescriptor, IDictionary2
parameters) +39
System.Web.Mvc.<>c_DisplayClass15.<InvokeActionMethodWithFilters>b_12()
+129 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter
filter, ActionExecutingContext preContext, Func1 continuation)
+826106 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext
controllerContext, IList1 filters, ActionDescriptor actionDescriptor,
IDictionary`2 parameters) +314
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext
controllerContext, String actionName) +825328
System.Web.Mvc.Controller.ExecuteCore() +159
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
+335 System.Web.Mvc.<>c_DisplayClassb.<BeginProcessRequest>b_5()
+62 System.Web.Mvc.Async.<>c_DisplayClass1.<MakeVoidDelegate>b_0()
+20 System.Web.Mvc.<>c_DisplayClasse.<EndProcessRequest>b_d()
+54 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
+469 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +375

Related

EF Core 3.1 OrderByDescending curious T-SQL translation

I have the following Linq queries that differs only on the where clause:
Query 1:
var initialList = await
context.AlertDetailsDbSet
// Pouring the TrackedVehicule related graph
.Include(ad => ad.TrackedVehicule.Registration)
.Include(ad => ad.TrackedVehicule.Device)
.Include(ad => ad.TrackedVehicule.GpsBox.Icon)
.Include(ad => ad.TrackedVehicule.GpsBoxTypeNavigation.Icon)
.Include(ad => ad.TrackedVehicule.VehiculeGpsBoxInfo.VehiculeConfigurationNavigation)
// Loading the associated alert
.Include(ad => ad.Alert)
#if NETSTANDARD2_1
// This filter is not supported with EF Core 2.X
// It should be done client side
.Where(ad => ad.Alert.CompanyId == companyId && ad.Alert.AlertRule.Contains("SerializableContextAlert") && ad.AlertDateTime >= startDate && ad.AlertDateTime <= endDate && ad.TrackedVehicule != null && ad.TrackedVehicule.CompanyId == companyId && ad.TrackedVehicule.RowEnabled == true && (ad.TrackedVehicule.GpsBoxType == 29 || ad.TrackedVehicule.GpsBoxType == 12))
#endif
.Skip(0)
// A limit is applied on returned elements
.Take(1000)
.OrderByDescending(ad => ad.AlertDateTime)
// .Skip(skip)
// .Take(take)
// We disable the tracking mechanism because the context will be thrown away as soon as we have the data
.AsNoTracking()
// We want an asynchrounous execution
.ToListAsync(cancellationToken)
Query 2:
var initialList = await
context.AlertDetailsDbSet
// Pouring the TrackedVehicule related graph
.Include(ad => ad.TrackedVehicule.Registration)
.Include(ad => ad.TrackedVehicule.Device)
.Include(ad => ad.TrackedVehicule.GpsBox.Icon)
.Include(ad => ad.TrackedVehicule.GpsBoxTypeNavigation.Icon)
.Include(ad => ad.TrackedVehicule.VehiculeGpsBoxInfo.VehiculeConfigurationNavigation)
// Loading the associated alert
.Include(ad => ad.Alert)
#if NETSTANDARD2_1
// This filter is not supported with EF Core 2.X
// It should be done client side
.Where(ad => ad.Alert.CompanyId == companyId && ad.Alert.AlertRule.Contains("SerializableContextAlert") && ad.TrackedVehicule != null && ad.TrackedVehicule.RowEnabled == true && ad.TrackedVehicule.CompanyId == companyId && (ad.TrackedVehicule.GpsBoxType == 29 || ad.TrackedVehicule.GpsBoxType == 12) && ad.AckTime.HasValue == acknwoledged)
#endif
.OrderByDescending(ad => ad.AlertDateTime)
.Skip(skip)
.Take(take)
// We disable the tracking mechanism because the context will be thrown away as soon as we have the data
.AsNoTracking()
// We want an asynchrounous execution
.ToListAsync(cancellationToken)
Basically, the first query is (among aother things) filtering on a boolean property, while the second one is filtering on a range of dates.
My problem is that those queries are not translated to the same T-TSQL leading to different results.
TSQL 1 :
exec sp_executesql N'SELECT [t].[AlertDetailID], [t].[AckMachineName], [t].[AckPhoneNumber], [t].[AckTime], [t].[AckUserName], [t].[AlertDateTime], [t].[AlertID], [t].[AlertedCorridorId], [t].[AlertedItemId],
[t].[AssociatedVehiculeID], [t].[AssociatedVehiculeUserID], [t].[Context], [t].[CustomID], [t].[CustomInfo], [t].[DbInsertTime], [t].[IsFleetAlert], [t].[MessageStatus], [t].[ReceivedTime], [t].[RowVersion],
[t].[SafeProtectCustomInfo], [t].[TrackedVehiculeID], [t].[TrackedVehiculeUserID], [v0].[VehiculeID], [v0].[Address], [v0].[AddressProtocol], [v0].[BoardID], [v0].[Category], [v0].[CompanyID], [v0].[CustomId], [v0].[DbInsertTime], [v0].[Description], [v0].[GpsBoxSubType], [v0].[GpsBoxTrackingDelay], [v0].[GpsBoxType], [v0].[HardwareID], [v0].[HasGeoWorker], [v0].[IconID], [v0].[Name], [v0].[PhoneNumber], [v0].[RowEnabled], [v0].[RowVersion], [v0].[VehiculeUserID], [r].[VehiculeID], [r].[CompanyId], [r].[LatestRegistrationStatusChangeDate], [r].[RegistrationMailSent], [r].[RegistrationStatusId], [d].[VehiculeID], [d].[AppVersion], [d].[Brand], [d].[Details], [d].[Imei], [d].[Model], [d].[Name], [d].[OsVersion], [d].[RowVersion], [g].[GpsBoxTypeID], [g].[GpsBoxSubTypeID], [g].[IconId], [g].[Name], [i].[IconId], [i].[Category], [i].[FileName], [i].[Icon], [g0].[GpsBoxTypeId], [g0].[Category], [g0].[IconId], [g0].[Name], [i0].[IconId], [i0].[Category], [i0].[FileName], [i0].[Icon], [v1].[VehiculeID], [v1].[BoardConfiguration], [v1].[BoardConnected], [v1].[BoardCurrentAddress], [v1].[BoardLastCommunicationTime], [v1].[Connected], [v1].[CurrentAddress], [v1].[CurrentDelay], [v1].[FuelConsumptionEstimationTime], [v1].[HeartBeatPeriod], [v1].[InsureCoherence], [v1].[KillHedgehog], [v1].[LastCommunicationTime], [v1].[LastConnexionTime], [v1].[LastParkMileage], [v1].[LastParkMileageTime], [v1].[NormalTrackingMode], [v1].[SmartModeDelay], [v1].[SmartModeDistance], [v1].[TimeModeDelay], [v1].[UpdateStartTime], [v1].[UpdateStatus], [v1].[VehiculeConfiguration], [g1].[ConfigurationID], [g1].[ConfigName], [g1].[Firmware], [g1].[GpsBoxType], [g1].[Master], [g1].[MasterName], [g1].[SimContract], [g1].[System], [a1].[AlertID], [a1].[AlertEnabled], [a1].[AlertRule], [a1].[Always], [a1].[CalendarID], [a1].[Category], [a1].[CompanyID], [a1].[Description], [a1].[DisplayName], [a1].[RowEnabled], [a1].[RowVersion], [a1].[Shared], [a1].[UserID]
FROM (
SELECT [a].[AlertDetailID], [a].[AckMachineName], [a].[AckPhoneNumber], [a].[AckTime], [a].[AckUserName], [a].[AlertDateTime], [a].[AlertID], [a].[AlertedCorridorId], [a].[AlertedItemId], [a].[AssociatedVehiculeID], [a].[AssociatedVehiculeUserID], [a].[Context], [a].[CustomID], [a].[CustomInfo], [a].[DbInsertTime], [a].[IsFleetAlert], [a].[MessageStatus], [a].[ReceivedTime], [a].[RowVersion], [a].[SafeProtectCustomInfo], [a].[TrackedVehiculeID], [a].[TrackedVehiculeUserID], [a0].[AlertID] AS [AlertID0]
FROM [AlertDetail] AS [a]
INNER JOIN [Alert] AS [a0] ON [a].[AlertID] = [a0].[AlertID]
LEFT JOIN [Vehicule] AS [v] ON [a].[TrackedVehiculeID] = [v].[VehiculeID]
WHERE (((((([a0].[CompanyID] = #__companyId_0) AND (CHARINDEX(N''SerializableContextAlert'', [a0].[AlertRule]) > 0)) AND [v].[VehiculeID] IS NOT NULL) AND ([v].[RowEnabled] = CAST(1 AS bit))) AND ([v].[CompanyID] = #__companyId_0)) AND (([v].[GpsBoxType] = 29) OR ([v].[GpsBoxType] = 12))) AND (CASE
WHEN [a].[AckTime] IS NOT NULL THEN CAST(1 AS bit)
ELSE CAST(0 AS bit)
END = #__acknwoledged_1)
ORDER BY [a].[AlertDateTime] DESC
OFFSET #__p_2 ROWS FETCH NEXT #__p_3 ROWS ONLY
) AS [t]
LEFT JOIN [Vehicule] AS [v0] ON [t].[TrackedVehiculeID] = [v0].[VehiculeID]
LEFT JOIN [SafeProtect].[Registration] AS [r] ON [v0].[VehiculeID] = [r].[VehiculeID]
LEFT JOIN [SafeProtect].[Device] AS [d] ON [v0].[VehiculeID] = [d].[VehiculeID]
LEFT JOIN [GpsBoxSubType] AS [g] ON ([v0].[GpsBoxType] = [g].[GpsBoxTypeID]) AND ([v0].[GpsBoxSubType] = [g].[GpsBoxSubTypeID])
LEFT JOIN [cst].[Icon] AS [i] ON [g].[IconId] = [i].[IconId]
LEFT JOIN [GpsBoxType] AS [g0] ON [v0].[GpsBoxType] = [g0].[GpsBoxTypeId]
LEFT JOIN [cst].[Icon] AS [i0] ON [g0].[IconId] = [i0].[IconId]
LEFT JOIN [VehiculeGpsBoxInfo] AS [v1] ON [v0].[VehiculeID] = [v1].[VehiculeID]
LEFT JOIN [GpsBoxConfiguration] AS [g1] ON [v1].[VehiculeConfiguration] = [g1].[ConfigurationID]
INNER JOIN [Alert] AS [a1] ON [t].[AlertID] = [a1].[AlertID]
ORDER BY [t].[AlertDateTime] DESC',N'#__companyId_0 int,#__acknwoledged_1 bit,#__p_2 int,#__p_3 int',#__companyId_0=1013,#__acknwoledged_1=0,#__p_2=0,#__p_3=999
TSQL 2:
exec sp_executesql N'SELECT [t].[AlertDetailID], [t].[AckMachineName], [t].[AckPhoneNumber], [t].[AckTime], [t].[AckUserName], [t].[AlertDateTime], [t].[AlertID], [t].[AlertedCorridorId], [t].[AlertedItemId],
[t].[AssociatedVehiculeID], [t].[AssociatedVehiculeUserID], [t].[Context], [t].[CustomID], [t].[CustomInfo], [t].[DbInsertTime], [t].[IsFleetAlert], [t].[MessageStatus], [t].[ReceivedTime], [t].[RowVersion],
[t].[SafeProtectCustomInfo], [t].[TrackedVehiculeID], [t].[TrackedVehiculeUserID], [v0].[VehiculeID], [v0].[Address], [v0].[AddressProtocol], [v0].[BoardID], [v0].[Category], [v0].[CompanyID], [v0].[CustomId], [v0].[DbInsertTime], [v0].[Description], [v0].[GpsBoxSubType], [v0].[GpsBoxTrackingDelay], [v0].[GpsBoxType], [v0].[HardwareID], [v0].[HasGeoWorker], [v0].[IconID], [v0].[Name], [v0].[PhoneNumber], [v0].[RowEnabled], [v0].[RowVersion], [v0].[VehiculeUserID], [r].[VehiculeID], [r].[CompanyId], [r].[LatestRegistrationStatusChangeDate], [r].[RegistrationMailSent], [r].[RegistrationStatusId], [d].[VehiculeID], [d].[AppVersion], [d].[Brand], [d].[Details], [d].[Imei], [d].[Model], [d].[Name], [d].[OsVersion], [d].[RowVersion], [g].[GpsBoxTypeID], [g].[GpsBoxSubTypeID], [g].[IconId], [g].[Name], [i].[IconId], [i].[Category], [i].[FileName], [i].[Icon], [g0].[GpsBoxTypeId], [g0].[Category], [g0].[IconId], [g0].[Name], [i0].[IconId], [i0].[Category], [i0].[FileName], [i0].[Icon], [v1].[VehiculeID], [v1].[BoardConfiguration], [v1].[BoardConnected], [v1].[BoardCurrentAddress], [v1].[BoardLastCommunicationTime], [v1].[Connected], [v1].[CurrentAddress], [v1].[CurrentDelay], [v1].[FuelConsumptionEstimationTime], [v1].[HeartBeatPeriod], [v1].[InsureCoherence], [v1].[KillHedgehog], [v1].[LastCommunicationTime], [v1].[LastConnexionTime], [v1].[LastParkMileage], [v1].[LastParkMileageTime], [v1].[NormalTrackingMode], [v1].[SmartModeDelay], [v1].[SmartModeDistance], [v1].[TimeModeDelay], [v1].[UpdateStartTime], [v1].[UpdateStatus], [v1].[VehiculeConfiguration], [g1].[ConfigurationID], [g1].[ConfigName], [g1].[Firmware], [g1].[GpsBoxType], [g1].[Master], [g1].[MasterName], [g1].[SimContract], [g1].[System], [a1].[AlertID], [a1].[AlertEnabled], [a1].[AlertRule], [a1].[Always], [a1].[CalendarID], [a1].[Category], [a1].[CompanyID], [a1].[Description], [a1].[DisplayName], [a1].[RowEnabled], [a1].[RowVersion], [a1].[Shared], [a1].[UserID]
FROM (
SELECT [a].[AlertDetailID], [a].[AckMachineName], [a].[AckPhoneNumber], [a].[AckTime], [a].[AckUserName], [a].[AlertDateTime], [a].[AlertID], [a].[AlertedCorridorId], [a].[AlertedItemId], [a].[AssociatedVehiculeID], [a].[AssociatedVehiculeUserID], [a].[Context], [a].[CustomID], [a].[CustomInfo], [a].[DbInsertTime], [a].[IsFleetAlert], [a].[MessageStatus], [a].[ReceivedTime], [a].[RowVersion], [a].[SafeProtectCustomInfo], [a].[TrackedVehiculeID], [a].[TrackedVehiculeUserID], [a0].[AlertID] AS [AlertID0]
FROM [AlertDetail] AS [a]
INNER JOIN [Alert] AS [a0] ON [a].[AlertID] = [a0].[AlertID]
LEFT JOIN [Vehicule] AS [v] ON [a].[TrackedVehiculeID] = [v].[VehiculeID]
WHERE ((((((([a0].[CompanyID] = #__companyId_0) AND (CHARINDEX(N''SerializableContextAlert'', [a0].[AlertRule]) > 0)) AND ([a].[AlertDateTime] >= #__startDate_1)) AND ([a].[AlertDateTime] <= #__endDate_2)) AND [v].[VehiculeID] IS NOT NULL) AND ([v].[CompanyID] = #__companyId_0)) AND ([v].[RowEnabled] = CAST(1 AS bit))) AND (([v].[GpsBoxType] = 29) OR ([v].[GpsBoxType] = 12))
ORDER BY (SELECT 1)
OFFSET #__p_3 ROWS FETCH NEXT #__p_4 ROWS ONLY
) AS [t]
LEFT JOIN [Vehicule] AS [v0] ON [t].[TrackedVehiculeID] = [v0].[VehiculeID]
LEFT JOIN [SafeProtect].[Registration] AS [r] ON [v0].[VehiculeID] = [r].[VehiculeID]
LEFT JOIN [SafeProtect].[Device] AS [d] ON [v0].[VehiculeID] = [d].[VehiculeID]
LEFT JOIN [GpsBoxSubType] AS [g] ON ([v0].[GpsBoxType] = [g].[GpsBoxTypeID]) AND ([v0].[GpsBoxSubType] = [g].[GpsBoxSubTypeID])
LEFT JOIN [cst].[Icon] AS [i] ON [g].[IconId] = [i].[IconId]
LEFT JOIN [GpsBoxType] AS [g0] ON [v0].[GpsBoxType] = [g0].[GpsBoxTypeId]
LEFT JOIN [cst].[Icon] AS [i0] ON [g0].[IconId] = [i0].[IconId]
LEFT JOIN [VehiculeGpsBoxInfo] AS [v1] ON [v0].[VehiculeID] = [v1].[VehiculeID]
LEFT JOIN [GpsBoxConfiguration] AS [g1] ON [v1].[VehiculeConfiguration] = [g1].[ConfigurationID]
INNER JOIN [Alert] AS [a1] ON [t].[AlertID] = [a1].[AlertID]
ORDER BY [t].[AlertDateTime] DESC',N'#__companyId_0 int,#__startDate_1 datetime,#__endDate_2 datetime,#__p_3 int,#__p_4 int',#__companyId_0=1013,#__startDate_1='2020-09-07 21:59:59',#__endDate_2='2020-09-08 21:59:59',#__p_3=0,#__p_4=999
It took me some time to spot a tiny by impacting difference between the two queries.
If you look carefully, the first subquery contains an ORDER BY which is what I want:
ORDER BY [a].[AlertDateTime] DESC
The second linq query, despite using the same OderBy is translated to a TSQL subquery that contains
ORDER BY (SELECT 1)
That sounds crasy to me.
I hope that someone could explain what is going on.
It has nothing to do with filtering (Where), but the order of ordering (OrderBy{Descending}) and row limiting (Skip / Take) operators in LINQ query.
The fake ORDER BY (SELECT 1) definitely is generated for the LINQ query having the following fragment
.Skip(0)
.Take(1000)
.OrderByDescending(ad => ad.AlertDateTime)
// .Skip(skip)
// .Take(take)
As you can see, here the row limiting operators are applied before the ordering. Since SqlServer OFFSET FETCH SQL construct requires ORDER BY, EF Core translator inserts fake one (it would have been better to throw exception or let the database do that, but that's another story. Actually before EF Core 3.0 there was CoreEventId.RowLimitingOperationWithoutOrderByWarning which could be configured to throw, but for some unknown reason it has been obsoleted and has no effect in EF Core 3.x).
The correct place of row limiting of course is after ordering, as it is in the commented out code. So simply remove the (temporary I guess) Skip(0).Take(1000) and uncomment the .Skip(skip).Take(take). Problem solved.

Optimize My Sql

I have a query in Epicor that I built using the BAQ Designer (which is just a gui for creating a SQL query). I recently made a change to add more data and now receive the following error.
The query processor ran out of internal resources and could not produce a query plan. This is a rare event and only expected for extremely complex queries or queries that reference a very large number of tables or partitions. Please simplify the query.
I'm sure the monster I created is not optimal and I could probably make some changes to make it better. If any of the experts could take a look and help it would be greatly appreciated as I am not a SQL expert, just good enough to break stuff.The SQL is:
With [Sales_Base] AS
(select
[SalesRep].[SalesRepCode] as [SalesRep_SalesRepCode],
[SalesRep].[Name] as [SalesRep_Name],
[InvcHead].[InvoiceNum] as [InvcHead_InvoiceNum],
[InvcHead].[InvoiceDate] as [InvcHead_InvoiceDate],
[InvcHead].[PONum] as [InvcHead_PONum],
[InvcDtl].[ExtPrice] as [InvcDtl_ExtPrice],
[Customer].[CustID] as [Customer_CustID],
[Customer].[Name] as [Customer_Name],
[SalesCat].[Description] as [SalesCat_Description],
[InvcDtl].[ProdCode] as [InvcDtl_ProdCode],
[SalesRep].[RoleCode] as [SalesRep_RoleCode],
[Customer].[GroupCode] as [Customer_GroupCode],
(Constants.Today) as [Calculated_Today],
(Constants.FirstDayOfMonth) as [Calculated_BOM],
(case
when DatePart(month,Constants.Today) in (1,4,7,10) then Constants.FirstDayOfMonth
when DatePart(month,Constants.Today) in (2,5,8,11) then DateAdd(month,-1,Constants.FirstDayOfMonth)
else DateAdd(month,-2,Constants.FirstDayOfMonth)
end) as [Calculated_BOQ],
(case
when DatePart(month,Constants.Today) = 1 then Constants.FirstDayOfMonth
when DatePart(month,Constants.Today) = 2 then DateAdd(month,-1,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 3 then DateAdd(month,-2,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 4 then DateAdd(month,-3,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 5 then DateAdd(month,-4,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 6 then DateAdd(month,-5,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 7 then DateAdd(month,-6,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 8 then DateAdd(month,-7,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 9 then DateAdd(month,-8,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 10 then DateAdd(month,-9,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 11 then DateAdd(month,-10,Constants.FirstDayOfMonth)
else DateAdd(month,-11,Constants.FirstDayOfMonth)
end) as [Calculated_BOY],
[Customer5].[CustID] as [Customer5_CustID],
[Customer5].[Name] as [Customer5_Name],
[Customer5].[GroupCode] as [Customer5_GroupCode],
[SalesRep].[EMailAddress] as [SalesRep_EMailAddress],
[InvcDtl].[InvoiceLine] as [InvcDtl_InvoiceLine],
(InvcDtl.ExtPrice + IsNull(InvcMisc.MiscAmt,0)) as [Calculated_InvcDtl_ExtPrice]
from Erp.SalesRep as SalesRep
left outer join Erp.InvcHead as InvcHead on
SalesRep.Company = InvcHead.Company
And
SalesRep.SalesRepCode = [Ice].entry(1,SalesRepList,'~')
Or
SalesRep.SalesRepCode = [Ice].entry(2,SalesRepList,'~')
Or
SalesRep.SalesRepCode = [Ice].entry(3,SalesRepList,'~')
Or
SalesRep.SalesRepCode = [Ice].entry(4,SalesRepList,'~')
Or
SalesRep.SalesRepCode = [Ice].entry(5,SalesRepList,'~')
and ( InvcHead.Posted = TRUE and InvcHead.InvoiceType <> 'DEP' )
left outer join Erp.InvcDtl as InvcDtl on
InvcHead.Company = InvcDtl.Company
And
InvcHead.InvoiceNum = InvcDtl.InvoiceNum
left outer join Erp.Customer as Customer on
InvcHead.Company = Customer.Company
And
InvcHead.SoldToCustNum = Customer.CustNum
left outer join Erp.SalesCat as SalesCat on
InvcDtl.Company = SalesCat.Company
And
InvcDtl.SalesCatID = SalesCat.SalesCatID
left outer join Erp.Customer as Customer5 on
InvcDtl.Company = Customer5.Company
And
InvcDtl.BTCustNum = Customer5.CustNum
left outer join Erp.InvcMisc as InvcMisc on
InvcDtl.Company = InvcMisc.Company
And
InvcDtl.InvoiceNum = InvcMisc.InvoiceNum
And
InvcDtl.InvoiceLine = InvcMisc.InvoiceLine
and ( InvcMisc.MiscCode = 'exp' )
where (SalesRep.RoleCode in ('sales', 'salesden', 'salesflx', 'salesmed', 'salespm') and SalesRep.SalesRepCode <> 'default' and SalesRep.SalesRepCode <> 'national' and SalesRep.SalesRepCode <> 'house' and SalesRep.SalesRepCode <> 'flexi' and SalesRep.SalesRepCode <> 'prescale' and SalesRep.InActive = FALSE))
,[Sales_Base_Add_MR] AS
(select
(Sales_Base5.SalesRep_SalesRepCode) as [Calculated_A_SalesRepCode],
(Sales_Base5.SalesRep_Name) as [Calculated_A_SalesRepName],
(Sales_Base5.InvcHead_InvoiceNum) as [Calculated_A_InvoiceNum],
(Sales_Base5.InvcHead_InvoiceDate) as [Calculated_A_InvoiceDate],
(Sales_Base5.InvcHead_PONum) as [Calculated_A_PONum],
(Sales_Base5.Calculated_InvcDtl_ExtPrice) as [Calculated_A_ExtPrice],
(Sales_Base5.Customer_CustID) as [Calculated_A_CustID],
(Sales_Base5.Customer_Name) as [Calculated_A_Cust],
(Sales_Base5.SalesCat_Description) as [Calculated_A_SalesCat],
(Sales_Base5.InvcDtl_ProdCode) as [Calculated_A_ProdCode],
(Sales_Base5.SalesRep_RoleCode) as [Calculated_A_RoleCode],
(Sales_Base5.Customer_GroupCode) as [Calculated_A_GroupCode],
(Sales_Base5.Calculated_Today) as [Calculated_A_Today],
(Sales_Base5.Calculated_BOM) as [Calculated_A_BOM],
(Sales_Base5.Calculated_BOQ) as [Calculated_A_BOQ],
(Sales_Base5.Calculated_BOY) as [Calculated_A_BOY],
(Sales_Base5.Customer5_CustID) as [Calculated_A_BTCustID],
(Sales_Base5.Customer5_Name) as [Calculated_A_BTCust],
(Sales_Base5.Customer5_GroupCode) as [Calculated_A_BTGroupCode],
[SalesRep2].[SalesRepCode] as [SalesRep2_SalesRepCode],
[SalesRep2].[Name] as [SalesRep2_Name],
[SalesRep2].[RoleCode] as [SalesRep2_RoleCode],
(Sales_Base5.SalesRep_EMailAddress) as [Calculated_A_Email],
(Sales_Base5.InvcDtl_InvoiceLine) as [Calculated_A_Line]
from Sales_Base as Sales_Base5
left outer join Erp.InvcHead as InvcHead1 on
Sales_Base5.InvcHead_InvoiceNum = InvcHead1.InvoiceNum
left outer join Erp.SalesRep as SalesRep2 on
InvcHead1.Company = SalesRep2.Company
And
[Ice].entry(1,SalesRepList,'~') = SalesRep2.SalesRepCode
Or
[Ice].entry(2,SalesRepList,'~') = SalesRep2.SalesRepCode
Or
[Ice].entry(3,SalesRepList,'~') = SalesRep2.SalesRepCode
Or
[Ice].entry(4,SalesRepList,'~') = SalesRep2.SalesRepCode
Or
[Ice].entry(5,SalesRepList,'~') = SalesRep2.SalesRepCode
and ( SalesRep2.RoleCode = 'mfgrep' )
where (Sales_Base5.Calculated_InvcDtl_ExtPrice <> 0 and Sales_Base5.InvcDtl_ProdCode <> ''))
,[Total] AS
(select
[Sales_Base_Add_MR3].[Calculated_A_InvoiceNum] as [Calculated_A_InvoiceNum],
(sum( Sales_Base_Add_MR3.Calculated_A_ExtPrice )) as [Calculated_Invoice_Total]
from Sales_Base_Add_MR as Sales_Base_Add_MR3
group by [Sales_Base_Add_MR3].[Calculated_A_InvoiceNum])
,[Key1] AS
(select
(Sales_Base_Add_MR.Calculated_A_SalesRepName) as [Calculated_Key1_SR],
(Sales_Base_Add_MR.Calculated_A_SalesRepName + Sales_Base_Add_MR.SalesRep2_Name) as [Calculated_Key1_Key]
from Sales_Base_Add_MR as Sales_Base_Add_MR
where (Sales_Base_Add_MR.SalesRep2_RoleCode = 'mfgrep')
group by (Sales_Base_Add_MR.Calculated_A_SalesRepName) as [Calculated_Key1_SR],
(Sales_Base_Add_MR.Calculated_A_SalesRepName + Sales_Base_Add_MR.SalesRep2_Name) as [Calculated_Key1_Key])
,[Key2] AS
(select
(Sales_Base_Add_MR2.Calculated_A_SalesRepName) as [Calculated_Key2_SR],
(Sales_Base_Add_MR2.Calculated_A_SalesRepName + Sales_Base_Add_MR2.Calculated_A_BTCustID) as [Calculated_Key2_Key]
from Sales_Base_Add_MR as Sales_Base_Add_MR2
where (Sales_Base_Add_MR2.Calculated_A_BTGroupCode in ('dend', 'flxd', 'medd', 'prsd'))
group by (Sales_Base_Add_MR2.Calculated_A_SalesRepName) as [Calculated_Key2_SR],
(Sales_Base_Add_MR2.Calculated_A_SalesRepName + Sales_Base_Add_MR2.Calculated_A_BTCustID) as [Calculated_Key2_Key])
,[B] AS
(select
(Sales_Base_Add_MR14.Calculated_A_SalesRepCode) as [Calculated_B_SalesRepCode],
(Sales_Base_Add_MR14.Calculated_A_SalesRepName) as [Calculated_B_SalesRepName],
(Sales_Base_Add_MR14.Calculated_A_InvoiceNum) as [Calculated_B_InvoiceNum],
(Sales_Base_Add_MR14.Calculated_A_InvoiceDate) as [Calculated_B_InvoiceDate],
(Sales_Base_Add_MR14.Calculated_A_PONum) as [Calculated_B_PONum],
(Total.Calculated_Invoice_Total) as [Calculated_B_OrderTotal],
(Sales_Base_Add_MR14.Calculated_A_CustID) as [Calculated_B_CustID],
(Sales_Base_Add_MR14.Calculated_A_Cust) as [Calculated_B_Cust],
(Sales_Base_Add_MR14.Calculated_A_RoleCode) as [Calculated_B_RoleCode],
(Sales_Base_Add_MR14.Calculated_A_GroupCode) as [Calculated_B_GroupCode],
(Sales_Base_Add_MR14.Calculated_A_Today) as [Calculated_B_Today],
(Sales_Base_Add_MR14.Calculated_A_BOM) as [Calculated_B_BOM],
(Sales_Base_Add_MR14.Calculated_A_BOQ) as [Calculated_B_BOQ],
(Sales_Base_Add_MR14.Calculated_A_BOY) as [Calculated_B_BOY],
(Sales_Base_Add_MR14.Calculated_A_BTCustID) as [Calculated_B_BTCustID],
(Sales_Base_Add_MR14.Calculated_A_BTCust) as [Calculated_B_BTCust],
(Sales_Base_Add_MR14.Calculated_A_BTGroupCode) as [Calculated_B_BTGroupCode],
(Sales_Base_Add_MR14.SalesRep2_SalesRepCode) as [Calculated_B_MRCode],
(Sales_Base_Add_MR14.SalesRep2_Name) as [Calculated_B_MRName],
(Sales_Base_Add_MR14.SalesRep2_RoleCode) as [Calculated_B_MRRoleCode],
(Sales_Base_Add_MR14.Calculated_A_Email) as [Calculated_B_Email],
(Key1.Calculated_Key1_Key) as [Calculated_B_Key],
(Key2.Calculated_Key2_Key) as [Calculated_B_Key2]
from Sales_Base_Add_MR as Sales_Base_Add_MR14
left outer join Key1 as Key1 on
Sales_Base_Add_MR14.Calculated_A_SalesRepName = Key1.Calculated_Key1_SR
And
Calculated_A_SalesRepName + SalesRep2_Name = Key1.Calculated_Key1_Key
left outer join Key2 as Key2 on
Sales_Base_Add_MR14.Calculated_A_SalesRepName = Key2.Calculated_Key2_SR
And
Calculated_A_SalesRepName + Calculated_A_BTCustID = Key2.Calculated_Key2_Key
left outer join Total as Total on
Sales_Base_Add_MR14.Calculated_A_InvoiceNum = Total.Calculated_A_InvoiceNum
group by (Sales_Base_Add_MR14.Calculated_A_SalesRepCode) as [Calculated_B_SalesRepCode],
(Sales_Base_Add_MR14.Calculated_A_SalesRepName) as [Calculated_B_SalesRepName],
(Sales_Base_Add_MR14.Calculated_A_InvoiceNum) as [Calculated_B_InvoiceNum],
(Sales_Base_Add_MR14.Calculated_A_InvoiceDate) as [Calculated_B_InvoiceDate],
(Sales_Base_Add_MR14.Calculated_A_PONum) as [Calculated_B_PONum],
(Total.Calculated_Invoice_Total) as [Calculated_B_OrderTotal],
(Sales_Base_Add_MR14.Calculated_A_CustID) as [Calculated_B_CustID],
(Sales_Base_Add_MR14.Calculated_A_Cust) as [Calculated_B_Cust],
(Sales_Base_Add_MR14.Calculated_A_RoleCode) as [Calculated_B_RoleCode],
(Sales_Base_Add_MR14.Calculated_A_GroupCode) as [Calculated_B_GroupCode],
(Sales_Base_Add_MR14.Calculated_A_Today) as [Calculated_B_Today],
(Sales_Base_Add_MR14.Calculated_A_BOM) as [Calculated_B_BOM],
(Sales_Base_Add_MR14.Calculated_A_BOQ) as [Calculated_B_BOQ],
(Sales_Base_Add_MR14.Calculated_A_BOY) as [Calculated_B_BOY],
(Sales_Base_Add_MR14.Calculated_A_BTCustID) as [Calculated_B_BTCustID],
(Sales_Base_Add_MR14.Calculated_A_BTCust) as [Calculated_B_BTCust],
(Sales_Base_Add_MR14.Calculated_A_BTGroupCode) as [Calculated_B_BTGroupCode],
(Sales_Base_Add_MR14.SalesRep2_SalesRepCode) as [Calculated_B_MRCode],
(Sales_Base_Add_MR14.SalesRep2_Name) as [Calculated_B_MRName],
(Sales_Base_Add_MR14.SalesRep2_RoleCode) as [Calculated_B_MRRoleCode],
(Sales_Base_Add_MR14.Calculated_A_Email) as [Calculated_B_Email],
(Key1.Calculated_Key1_Key) as [Calculated_B_Key],
(Key2.Calculated_Key2_Key) as [Calculated_B_Key2])
,[Sales_BOM] AS
(select
(B1.Calculated_B_SalesRepName) as [Calculated_Sales_BOM_Rep],
(sum( B1.Calculated_B_OrderTotal )) as [Calculated_Sales_BOM_Sum]
from B as B1
where (B1.Calculated_B_InvoiceDate >= B1.Calculated_B_BOM and B1.Calculated_B_InvoiceDate <= B1.Calculated_B_Today)
group by (B1.Calculated_B_SalesRepName) as [Calculated_Sales_BOM_Rep])
,[Sales_BOQ] AS
(select
(B2.Calculated_B_SalesRepName) as [Calculated_Sales_BOQ_Rep],
(sum( B2.Calculated_B_OrderTotal )) as [Calculated_Sales_BOQ_Sum]
from B as B2
where (B2.Calculated_B_InvoiceDate >= B2.Calculated_B_BOQ and B2.Calculated_B_InvoiceDate <= B2.Calculated_B_Today)
group by (B2.Calculated_B_SalesRepName) as [Calculated_Sales_BOQ_Rep])
,[Sales_BOY] AS
(select
(B3.Calculated_B_SalesRepName) as [Calculated_Sales_BOY_Rep],
(sum( B3.Calculated_B_OrderTotal )) as [Calculated_Sales_BOY_Sum]
from B as B3
where (B3.Calculated_B_InvoiceDate >= B3.Calculated_B_BOY and B3.Calculated_B_InvoiceDate <= B3.Calculated_B_Today)
group by (B3.Calculated_B_SalesRepName) as [Calculated_Sales_BOY_Rep])
,[MR_BOM] AS
(select
(sum( B5.Calculated_B_OrderTotal )) as [Calculated_MR_BOM_Sum],
(B5.Calculated_B_Key) as [Calculated_MR_BOM_Key]
from B as B5
where (B5.Calculated_B_InvoiceDate >= B5.Calculated_B_BOM and B5.Calculated_B_InvoiceDate <= B5.Calculated_B_Today and B5.Calculated_B_MRRoleCode = 'mfgrep')
group by (B5.Calculated_B_Key) as [Calculated_MR_BOM_Key])
,[MR_BOQ] AS
(select
(sum( B6.Calculated_B_OrderTotal )) as [Calculated_MR_BOQ_Sum],
(B6.Calculated_B_Key) as [Calculated_MR_BOQ_Key]
from B as B6
where (B6.Calculated_B_InvoiceDate >= B6.Calculated_B_BOQ and B6.Calculated_B_InvoiceDate <= B6.Calculated_B_Today and B6.Calculated_B_MRRoleCode = 'mfgrep')
group by (B6.Calculated_B_Key) as [Calculated_MR_BOQ_Key])
,[MR_BOY] AS
(select
(sum( B7.Calculated_B_OrderTotal )) as [Calculated_MR_BOY_Sum],
(B7.Calculated_B_Key) as [Calculated_MR_BOY_Key],
(B7.Calculated_B_MRName) as [Calculated_MR_BOY_MR]
from B as B7
where (B7.Calculated_B_InvoiceDate >= B7.Calculated_B_BOY and B7.Calculated_B_InvoiceDate <= B7.Calculated_B_Today and B7.Calculated_B_MRRoleCode = 'mfgrep')
group by (B7.Calculated_B_Key) as [Calculated_MR_BOY_Key],
(B7.Calculated_B_MRName) as [Calculated_MR_BOY_MR])
,[Dist_BOM] AS
(select
(sum( B9.Calculated_B_OrderTotal )) as [Calculated_Dist_BOM_Sum],
(B9.Calculated_B_Key2) as [Calculated_Dist_BOM_Key]
from B as B9
where (B9.Calculated_B_InvoiceDate >= B9.Calculated_B_BOM and B9.Calculated_B_InvoiceDate <= B9.Calculated_B_Today and B9.Calculated_B_BTGroupCode in ('dend', 'flxd', 'medd', 'prsd'))
group by (B9.Calculated_B_Key2) as [Calculated_Dist_BOM_Key])
,[Dist_BOQ] AS
(select
(sum( B10.Calculated_B_OrderTotal )) as [Calculated_Dist_BOQ_Sum],
(B10.Calculated_B_Key2) as [Calculated_Dist_BOQ_Key]
from B as B10
where (B10.Calculated_B_InvoiceDate >= B10.Calculated_B_BOQ and B10.Calculated_B_InvoiceDate <= B10.Calculated_B_Today and B10.Calculated_B_BTGroupCode in ('dend', 'flxd', 'medd', 'prsd'))
group by (B10.Calculated_B_Key2) as [Calculated_Dist_BOQ_Key])
,[Dist_BOY] AS
(select
(sum( B11.Calculated_B_OrderTotal )) as [Calculated_Dist_BOY_Sum],
(B11.Calculated_B_Key2) as [Calculated_Dist_BOY_Key],
(B11.Calculated_B_BTCustID) as [Calculated_Dist_BOY_ID],
(B11.Calculated_B_BTCust) as [Calculated_Dist_BOYDist],
(B11.Calculated_B_BTGroupCode) as [Calculated_Dist_BOY_Group]
from B as B11
where (B11.Calculated_B_InvoiceDate >= B11.Calculated_B_BOY and B11.Calculated_B_InvoiceDate <= B11.Calculated_B_Today and B11.Calculated_B_BTGroupCode in ('dend', 'flxd', 'medd', 'prsd'))
group by (B11.Calculated_B_Key2) as [Calculated_Dist_BOY_Key],
(B11.Calculated_B_BTCustID) as [Calculated_Dist_BOY_ID],
(B11.Calculated_B_BTCust) as [Calculated_Dist_BOYDist],
(B11.Calculated_B_BTGroupCode) as [Calculated_Dist_BOY_Group])
,[Sales_Group] AS
(select
(B12.Calculated_B_SalesRepName) as [Calculated_Sales_Group_SR],
(B12.Calculated_B_Email) as [Calculated_Sales_Group_Email]
from B as B12
group by (B12.Calculated_B_SalesRepName) as [Calculated_Sales_Group_SR],
(B12.Calculated_B_Email) as [Calculated_Sales_Group_Email])
,[MR_Group] AS
(select
(B13.Calculated_B_SalesRepName) as [Calculated_MR_Group_SR],
(B13.Calculated_B_Email) as [Calculated_MR_Group_Email],
(B13.Calculated_B_Key) as [Calculated_MR_Group_Key]
from B as B13
where (B13.Calculated_B_Key is not null)
group by (B13.Calculated_B_SalesRepName) as [Calculated_MR_Group_SR],
(B13.Calculated_B_Email) as [Calculated_MR_Group_Email],
(B13.Calculated_B_Key) as [Calculated_MR_Group_Key])
,[Dist_Group] AS
(select
(B15.Calculated_B_SalesRepName) as [Calculated_Dist_Group_SR],
(B15.Calculated_B_Email) as [Calculated_Dist_Group_Email],
(B15.Calculated_B_Key2) as [Calculated_Dist_Group_Key]
from B as B15
where (B15.Calculated_B_Key2 is not null)
group by (B15.Calculated_B_SalesRepName) as [Calculated_Dist_Group_SR],
(B15.Calculated_B_Email) as [Calculated_Dist_Group_Email],
(B15.Calculated_B_Key2) as [Calculated_Dist_Group_Key])
,[Report] AS
(select
(IsNull(B16.Calculated_B_SalesRepCode,'Sales')) as [Calculated_R_SalesRepCode],
(Sales_Group.Calculated_Sales_Group_SR) as [Calculated_R_SalesRepName],
(IsNull(B16.Calculated_B_InvoiceNum,9999999)) as [Calculated_R_InvoiceNum],
(IsNull(B16.Calculated_B_InvoiceDate, convert(date, '30991231', 112))) as [Calculated_R_InvoiceDate],
(B16.Calculated_B_PONum) as [Calculated_R_PONum],
(B16.Calculated_B_OrderTotal) as [Calculated_R_InvoiceTotal],
(B16.Calculated_B_CustID) as [Calculated_R_CustID],
(B16.Calculated_B_Cust) as [Calculated_R_Cust],
(B16.Calculated_B_RoleCode) as [Calculated_R_RoleCode],
(B16.Calculated_B_GroupCode) as [Calculated_R_GroupCode],
(B16.Calculated_B_BTCustID) as [Calculated_R_BTCustID],
(B16.Calculated_B_BTCust) as [Calculated_R_BTCust],
(B16.Calculated_B_BTGroupCode) as [Calculated_R_BTGroupCode],
(B16.Calculated_B_MRCode) as [Calculated_R_MRCode],
(B16.Calculated_B_MRName) as [Calculated_R_MRName],
(B16.Calculated_B_MRRoleCode) as [Calculated_R_MRRoleCode],
(Sales_Group.Calculated_Sales_Group_Email) as [Calculated_R_Email],
(B16.Calculated_B_Key) as [Calculated_R_Key],
(B16.Calculated_B_Key2) as [Calculated_R_Key2],
(Sales_BOM.Calculated_Sales_BOM_Sum) as [Calculated_R_SalesBOM],
(Sales_BOQ.Calculated_Sales_BOQ_Sum) as [Calculated_R_SalesBOQ],
(Sales_BOY.Calculated_Sales_BOY_Sum) as [Calculated_R_SalesBOY],
(NULL) as [Calculated_R_MRBOM],
(NULL) as [Calculated_R_MRBOQ],
(NULL) as [Calculated_R_MRBOY],
(NULL) as [Calculated_R_DistBOM],
(NULL) as [Calculated_R_DistBOQ],
(NULL) as [Calculated_R_DistBOY]
from Sales_Group as Sales_Group
left outer join B as B16 on
Sales_Group.Calculated_Sales_Group_SR = B16.Calculated_B_SalesRepName
and ( B16.Calculated_B_Key is null and B16.Calculated_B_Key2 is null and B16.Calculated_B_InvoiceDate >= B16.Calculated_B_BOM and B16.Calculated_B_InvoiceDate <= B16.Calculated_B_Today )
left outer join Sales_BOM as Sales_BOM on
Sales_Group.Calculated_Sales_Group_SR = Sales_BOM.Calculated_Sales_BOM_Rep
left outer join Sales_BOQ as Sales_BOQ on
Sales_Group.Calculated_Sales_Group_SR = Sales_BOQ.Calculated_Sales_BOQ_Rep
left outer join Sales_BOY as Sales_BOY on
Sales_Group.Calculated_Sales_Group_SR = Sales_BOY.Calculated_Sales_BOY_Rep
UNION
select
(B17.Calculated_B_SalesRepCode) as [Calculated_C_1],
(MR_Group.Calculated_MR_Group_SR) as [Calculated_C_2],
(B17.Calculated_B_InvoiceNum) as [Calculated_C_3],
(B17.Calculated_B_InvoiceDate) as [Calculated_C_4],
(B17.Calculated_B_PONum) as [Calculated_C_5],
(B17.Calculated_B_OrderTotal) as [Calculated_C_6],
(B17.Calculated_B_CustID) as [Calculated_C_9],
(B17.Calculated_B_Cust) as [Calculated_C_10],
(B17.Calculated_B_RoleCode) as [Calculated_C_13],
(B17.Calculated_B_GroupCode) as [Calculated_C_14],
(B17.Calculated_B_BTCustID) as [Calculated_C_15],
(B17.Calculated_B_BTCust) as [Calculated_C_16],
(B17.Calculated_B_BTGroupCode) as [Calculated_C_17],
(B17.Calculated_B_MRCode) as [Calculated_C_20],
(IsNull(B17.Calculated_B_MRName,MR_BOY.Calculated_MR_BOY_MR)) as [Calculated_C_21],
(B17.Calculated_B_MRRoleCode) as [Calculated_C_22],
(MR_Group.Calculated_MR_Group_Email) as [Calculated_C_23],
(MR_Group.Calculated_MR_Group_Key) as [Calculated_C_24],
(B17.Calculated_B_Key2) as [Calculated_C_25],
(Sales_BOM2.Calculated_Sales_BOM_Sum) as [Calculated_C_27],
(Sales_BOQ2.Calculated_Sales_BOQ_Sum) as [Calculated_C_28],
(Sales_BOY2.Calculated_Sales_BOY_Sum) as [Calculated_C_29],
(MR_BOM.Calculated_MR_BOM_Sum) as [Calculated_C_31],
(MR_BOQ.Calculated_MR_BOQ_Sum) as [Calculated_C_32],
(MR_BOY.Calculated_MR_BOY_Sum) as [Calculated_C_33],
(NULL) as [Calculated_C_35],
(NULL) as [Calculated_C_36],
(NULL) as [Calculated_C_37]
from MR_Group as MR_Group
left outer join B as B17 on
MR_Group.Calculated_MR_Group_Key = B17.Calculated_B_Key
and ( B17.Calculated_B_InvoiceDate >= B17.Calculated_B_BOM and B17.Calculated_B_InvoiceDate <= B17.Calculated_B_Today )
left outer join MR_BOM as MR_BOM on
MR_Group.Calculated_MR_Group_Key = MR_BOM.Calculated_MR_BOM_Key
left outer join MR_BOQ as MR_BOQ on
MR_Group.Calculated_MR_Group_Key = MR_BOQ.Calculated_MR_BOQ_Key
left outer join MR_BOY as MR_BOY on
MR_Group.Calculated_MR_Group_Key = MR_BOY.Calculated_MR_BOY_Key
left outer join Sales_BOM as Sales_BOM2 on
MR_Group.Calculated_MR_Group_SR = Sales_BOM2.Calculated_Sales_BOM_Rep
left outer join Sales_BOQ as Sales_BOQ2 on
MR_Group.Calculated_MR_Group_SR = Sales_BOQ2.Calculated_Sales_BOQ_Rep
left outer join Sales_BOY as Sales_BOY2 on
MR_Group.Calculated_MR_Group_SR = Sales_BOY2.Calculated_Sales_BOY_Rep
UNION
select
(B18.Calculated_B_SalesRepCode) as [Calculated_D_1],
(Dist_Group.Calculated_Dist_Group_SR) as [Calculated_D_2],
(B18.Calculated_B_InvoiceNum) as [Calculated_D_3],
(B18.Calculated_B_InvoiceDate) as [Calculated_D_4],
(B18.Calculated_B_PONum) as [Calculated_D_5],
(B18.Calculated_B_OrderTotal) as [Calculated_D_6],
(B18.Calculated_B_CustID) as [Calculated_D_9],
(B18.Calculated_B_Cust) as [Calculated_D_10],
(B18.Calculated_B_RoleCode) as [Calculated_D_13],
(B18.Calculated_B_GroupCode) as [Calculated_D_14],
(IsNull(B18.Calculated_B_BTCustID,Dist_BOY.Calculated_Dist_BOY_ID)) as [Calculated_D_15],
(IsNull(B18.Calculated_B_BTCust,Dist_BOY.Calculated_Dist_BOYDist)) as [Calculated_D_16],
(IsNull(B18.Calculated_B_BTGroupCode,Dist_BOY.Calculated_Dist_BOY_Group)) as [Calculated_D_17],
(B18.Calculated_B_MRCode) as [Calculated_D_20],
(B18.Calculated_B_MRName) as [Calculated_D_21],
(B18.Calculated_B_MRRoleCode) as [Calculated_D_22],
(Dist_Group.Calculated_Dist_Group_Email) as [Calculated_D_23],
(B18.Calculated_B_Key) as [Calculated_D_24],
(Dist_Group.Calculated_Dist_Group_Key) as [Calculated_D_25],
(Sales_BOM1.Calculated_Sales_BOM_Sum) as [Calculated_D_27],
(Sales_BOQ1.Calculated_Sales_BOQ_Sum) as [Calculated_D_28],
(Sales_BOY1.Calculated_Sales_BOY_Sum) as [Calculated_D_29],
(NULL) as [Calculated_D_31],
(NULL) as [Calculated_D_32],
(NULL) as [Calculated_D_33],
(Dist_BOM.Calculated_Dist_BOM_Sum) as [Calculated_D_35],
(Dist_BOQ.Calculated_Dist_BOQ_Sum) as [Calculated_D_36],
(Dist_BOY.Calculated_Dist_BOY_Sum) as [Calculated_D_37]
from Dist_Group as Dist_Group
left outer join B as B18 on
Dist_Group.Calculated_Dist_Group_Key = B18.Calculated_B_Key2
and ( B18.Calculated_B_InvoiceDate >= B18.Calculated_B_BOM and B18.Calculated_B_InvoiceDate <= B18.Calculated_B_Today )
left outer join Dist_BOM as Dist_BOM on
Dist_Group.Calculated_Dist_Group_Key = Dist_BOM.Calculated_Dist_BOM_Key
left outer join Dist_BOQ as Dist_BOQ on
Dist_Group.Calculated_Dist_Group_Key = Dist_BOQ.Calculated_Dist_BOQ_Key
left outer join Dist_BOY as Dist_BOY on
Dist_Group.Calculated_Dist_Group_Key = Dist_BOY.Calculated_Dist_BOY_Key
left outer join Sales_BOM as Sales_BOM1 on
Dist_Group.Calculated_Dist_Group_SR = Sales_BOM1.Calculated_Sales_BOM_Rep
left outer join Sales_BOQ as Sales_BOQ1 on
Dist_Group.Calculated_Dist_Group_SR = Sales_BOQ1.Calculated_Sales_BOQ_Rep
left outer join Sales_BOY as Sales_BOY1 on
Dist_Group.Calculated_Dist_Group_SR = Sales_BOY1.Calculated_Sales_BOY_Rep)
select
[Report].[Calculated_R_SalesRepCode] as [Calculated_R_SalesRepCode],
[Report].[Calculated_R_SalesRepName] as [Calculated_R_SalesRepName],
[Report].[Calculated_R_InvoiceNum] as [Calculated_R_InvoiceNum],
[Report].[Calculated_R_InvoiceDate] as [Calculated_R_InvoiceDate],
[Report].[Calculated_R_PONum] as [Calculated_R_PONum],
[Report].[Calculated_R_InvoiceTotal] as [Calculated_R_InvoiceTotal],
[Report].[Calculated_R_CustID] as [Calculated_R_CustID],
[Report].[Calculated_R_Cust] as [Calculated_R_Cust],
[Report].[Calculated_R_RoleCode] as [Calculated_R_RoleCode],
[Report].[Calculated_R_GroupCode] as [Calculated_R_GroupCode],
[Report].[Calculated_R_BTCustID] as [Calculated_R_BTCustID],
[Report].[Calculated_R_BTCust] as [Calculated_R_BTCust],
[Report].[Calculated_R_BTGroupCode] as [Calculated_R_BTGroupCode],
[Report].[Calculated_R_MRCode] as [Calculated_R_MRCode],
[Report].[Calculated_R_MRName] as [Calculated_R_MRName],
[Report].[Calculated_R_MRRoleCode] as [Calculated_R_MRRoleCode],
[Report].[Calculated_R_Email] as [Calculated_R_Email],
[Report].[Calculated_R_Key] as [Calculated_R_Key],
[Report].[Calculated_R_Key2] as [Calculated_R_Key2],
[Report].[Calculated_R_SalesBOM] as [Calculated_R_SalesBOM],
[Report].[Calculated_R_SalesBOQ] as [Calculated_R_SalesBOQ],
[Report].[Calculated_R_SalesBOY] as [Calculated_R_SalesBOY],
[Report].[Calculated_R_MRBOM] as [Calculated_R_MRBOM],
[Report].[Calculated_R_MRBOQ] as [Calculated_R_MRBOQ],
[Report].[Calculated_R_MRBOY] as [Calculated_R_MRBOY],
[Report].[Calculated_R_DistBOM] as [Calculated_R_DistBOM],
[Report].[Calculated_R_DistBOQ] as [Calculated_R_DistBOQ],
[Report].[Calculated_R_DistBOY] as [Calculated_R_DistBOY]
from Report as Report
a) Look for simplifying parts of the query. For example, this part can be vastly simplified:
(case
when DatePart(month,Constants.Today) = 1 then Constants.FirstDayOfMonth
when DatePart(month,Constants.Today) = 2 then DateAdd(month,-1,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 3 then DateAdd(month,-2,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 4 then DateAdd(month,-3,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 5 then DateAdd(month,-4,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 6 then DateAdd(month,-5,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 7 then DateAdd(month,-6,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 8 then DateAdd(month,-7,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 9 then DateAdd(month,-8,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 10 then DateAdd(month,-9,Constants.FirstDayOfMonth)
when DatePart(month,Constants.Today) = 11 then DateAdd(month,-10,Constants.FirstDayOfMonth)
else DateAdd(month,-11,Constants.FirstDayOfMonth)
end) as [Calculated_BOY],
Could be instead
(case
when DatePart(month,Constants.Today) = 12 then Constants.FirstDayOfMonth
else DateAdd(month, - ( DatePart(month,Constants.Today) - 1), Constants.FirstDayOfMonth) end ) as [Calculated_BOY]
b) Separate the subselects into temporary tables, particularly those that you can verify produce relatively small resultsets (let's say thousand of rows or less). Providing those temporary tables to the select instead of subselects will help the query processor
I really wonder what is
[Ice].entry(1,SalesRepList,'~')
if it is a function, you can work on it.
But it seems to be creating constant values, not dynamic per row
I see that the query is formed of multiple CTE expressions
The Report CTE is formed of UNIONs can be converted to UNION ALLs if possible
And many GROUP By for calculations
Perhaps those can be converted to new aggregation syntax with SUM() function using PARTITION BY clause

I want to subtract Curr_YTD and Prev_YTD and divide it by Prev_YTD. The stored procedure needs correction

its a stored procedure taking the sum of columns now I want to take subtract the values Curr_YTD and Prev_YTD and divide by Prev_YTD.
The stored procedure needs correction.
Kindly let me know if anybody could help with subtract and divide operator.
SELECT B.TEAMNAME, B.PRIMARY_CODE, B.HIERARCHY_LEVEL, A.THERAPEUTICCLASS_CD,
B.PRODUCT_CODE, B.CHANNEL, B.UNIT,'P' PM_INDICATOR, A.PROCESSINGPERIOD_DT,
A.LOCATION_CODE, A.GEOGRAPHY_NAME,party_id, A.TRX_QTY1, A.TRX_QTY2, A.TRX_QTY3, A.TRX_QTY4,
A.TRX_QTY5,A.TRX_QTY6,A.TRX_QTY7,A.TRX_QTY8,A.TRX_QTY9,A.TRX_QTY10,A.TRX_QTY11,
A.TRX_QTY12,A.TRX_QTY13,A.TRX_QTY14,A.TRX_QTY15,A.TRX_QTY16,A.TRX_QTY17,A.TRX_QTY18,
A.TRX_QTY19,A.TRX_QTY20,A.TRX_QTY21, A.TRX_QTY22,A.TRX_QTY23,A.TRX_QTY24,
A.TRX_QTY1+ A.TRX_QTY2+ A.TRX_QTY3+ A.TRX_QTY4+A.TRX_QTY5+A.TRX_QTY6+A.TRX_QTY7+A.TRX_QTY8 Curr_YTD,
A.TRX_QTY13+A.TRX_QTY14+A.TRX_QTY15+A.TRX_QTY16+A.TRX_QTY17+A.TRX_QTY18+A.TRX_QTY19+A.TRX_QTY20 Prev_YTD,
(Curr_YTD Minus Prev_YTD)/Prev_YTD YTD
FROM (SELECT TTP.LILLY_MARKET_ID THERAPEUTICCLASS_CD,
TO_DATE(XPS.PROCESSINGPERIOD_DT, 'MM / DD / YYYY') PROCESSINGPERIOD_DT,
TTP.PRIMARY_CD PRIMARY_CODE,
TLP.LOCATION_CODE,
RTRIM(TLP.Geography_name) GEOGRAPHY_NAME,
ttp.party_id,
SUM(XPS.TRX_QTY1) TRX_QTY1,SUM(XPS.TRX_QTY2) TRX_QTY2,SUM(XPS.TRX_QTY3) TRX_QTY3,
SUM(XPS.TRX_QTY4) TRX_QTY4,SUM(XPS.TRX_QTY5) TRX_QTY5,SUM(XPS.TRX_QTY6) TRX_QTY6,
SUM(XPS.TRX_QTY7) TRX_QTY7,SUM(XPS.TRX_QTY8) TRX_QTY8,SUM(XPS.TRX_QTY9) TRX_QTY9,
SUM(XPS.TRX_QTY10) TRX_QTY10,SUM(XPS.TRX_QTY11) TRX_QTY11,SUM(XPS.TRX_QTY12) TRX_QTY12,
SUM(XPS.TRX_QTY13) TRX_QTY13,SUM(XPS.TRX_QTY14) TRX_QTY14,SUM(XPS.TRX_QTY15) TRX_QTY15,
SUM(XPS.TRX_QTY16) TRX_QTY16,SUM(XPS.TRX_QTY17) TRX_QTY17,SUM(XPS.TRX_QTY18) TRX_QTY18,
SUM(XPS.TRX_QTY19) TRX_QTY19,SUM(XPS.TRX_QTY20) TRX_QTY20,SUM(XPS.TRX_QTY21) TRX_QTY21,
SUM(XPS.TRX_QTY22) TRX_QTY22,SUM(XPS.TRX_QTY23) TRX_QTY23,SUM(XPS.TRX_QTY24) TRX_QTY24
FROM T_SALECRDTNG_XPS_48861 XPS,
T_SALECRDTNG_TTP_49354 TTP,
T_SALESCRD_LOCTNMAPNG TLP
WHERE TTP.PARTY_ID = XPS.PRFSNL_ID
AND TTP.LILLY_MARKET_ID = XPS.LLY_THERAPEUTICCLASS_CD
AND TLP.MAPPINGID = 48871
and TLP.PRIMARY_CODE = 'USUA'
AND XPS.PRODUCT_ID IN ('10210','10215')
AND XPS.LLY_PRODUCT_ID IN ('203825','208142')
AND TLP.LOCATION_CODE = TRIM(TTP.MASTER_GEO_ID)
AND TTP.PRIMARY_CD = TLP.PRIMARY_CODE
group by TTP.LILLY_MARKET_ID,
TTP.PRIMARY_CD,
TO_DATE(XPS.PROCESSINGPERIOD_DT, 'MM / DD / YYYY'),
TLP.LOCATION_CODE,
ttp.party_id,
TLP.Geography_name) A,
(SELECT DISTINCT UPPER(SC.TEAMNAME) TEAMNAME,
DECODE(SC.HIERARCHY_LEVEL, 'TERRITORY', 3, 'DISTRICT', 2, 'AREA', 1, 'REGION',
6,'DIVISION',0, NULL) HIERARCHY_LEVEL,
SC.PRODUCT_CODE PRODUCT_CODE,
SC.PRIMARY_CODE PRIMARY_CODE,
SC.CHANNEL CHANNEL,
SC.UNIT UNIT
FROM T_SALESCRDNG_CONFIG SC
WHERE SC.CONFIGID = 48994
AND SC.PRIMARY_CODE = 'USUA'
AND SC.PRODUCT_CODE = 'CIALIS TOTAL') B
WHERE A.PRIMARY_CODE = B.PRIMARY_CODE
;
Just add a SELCET and make your existing query a sub-query, something like this
SELECT rs.TEAMNAME, rs.PRIMARY_CODE, rs.HIERARCHY_LEVEL, rs.THERAPEUTICCLASS_CD,
rs.PRODUCT_CODE, rs.CHANNEL, rs.UNIT,rs.PM_INDICATOR, rs.PROCESSINGPERIOD_DT,
rs.LOCATION_CODE, rs.GEOGRAPHY_NAME,rs.party_id, rs.TRX_QTY1, rs.TRX_QTY2, rs.TRX_QTY3, rs.TRX_QTY4,
rs.TRX_QTY5,rs.TRX_QTY6,rs.TRX_QTY7,rs.TRX_QTY8,rs.TRX_QTY9,rs.TRX_QTY10,rs.TRX_QTY11,
rs.TRX_QTY12,rs.TRX_QTY13,rs.TRX_QTY14,rs.TRX_QTY15,rs.TRX_QTY16,rs.TRX_QTY17,rs.TRX_QTY18,
rs.TRX_QTY19,rs.TRX_QTY20,rs.TRX_QTY21, rs.TRX_QTY22,rs.TRX_QTY23,rs.TRX_QTY24,
rs.Curr_YTD, rs.Prev_YTD,
(rs.Curr_YTD - rs.Prev_YTD)/rs.Prev_YTD YTD
FROM
(
SELECT B.TEAMNAME, B.PRIMARY_CODE, B.HIERARCHY_LEVEL, A.THERAPEUTICCLASS_CD,
B.PRODUCT_CODE, B.CHANNEL, B.UNIT,'P' PM_INDICATOR, A.PROCESSINGPERIOD_DT,
A.LOCATION_CODE, A.GEOGRAPHY_NAME,party_id, A.TRX_QTY1, A.TRX_QTY2, A.TRX_QTY3, A.TRX_QTY4,
A.TRX_QTY5,A.TRX_QTY6,A.TRX_QTY7,A.TRX_QTY8,A.TRX_QTY9,A.TRX_QTY10,A.TRX_QTY11,
A.TRX_QTY12,A.TRX_QTY13,A.TRX_QTY14,A.TRX_QTY15,A.TRX_QTY16,A.TRX_QTY17,A.TRX_QTY18,
A.TRX_QTY19,A.TRX_QTY20,A.TRX_QTY21, A.TRX_QTY22,A.TRX_QTY23,A.TRX_QTY24,
A.TRX_QTY1+ A.TRX_QTY2+ A.TRX_QTY3+ A.TRX_QTY4+A.TRX_QTY5+A.TRX_QTY6+A.TRX_QTY7+A.TRX_QTY8 Curr_YTD,
A.TRX_QTY13+A.TRX_QTY14+A.TRX_QTY15+A.TRX_QTY16+A.TRX_QTY17+A.TRX_QTY18+A.TRX_QTY19+A.TRX_QTY20 Prev_YTD
FROM (SELECT TTP.LILLY_MARKET_ID THERAPEUTICCLASS_CD,
TO_DATE(XPS.PROCESSINGPERIOD_DT, 'MM / DD / YYYY') PROCESSINGPERIOD_DT,
TTP.PRIMARY_CD PRIMARY_CODE,
TLP.LOCATION_CODE,
RTRIM(TLP.Geography_name) GEOGRAPHY_NAME,
ttp.party_id,
SUM(XPS.TRX_QTY1) TRX_QTY1,SUM(XPS.TRX_QTY2) TRX_QTY2,SUM(XPS.TRX_QTY3) TRX_QTY3,
SUM(XPS.TRX_QTY4) TRX_QTY4,SUM(XPS.TRX_QTY5) TRX_QTY5,SUM(XPS.TRX_QTY6) TRX_QTY6,
SUM(XPS.TRX_QTY7) TRX_QTY7,SUM(XPS.TRX_QTY8) TRX_QTY8,SUM(XPS.TRX_QTY9) TRX_QTY9,
SUM(XPS.TRX_QTY10) TRX_QTY10,SUM(XPS.TRX_QTY11) TRX_QTY11,SUM(XPS.TRX_QTY12) TRX_QTY12,
SUM(XPS.TRX_QTY13) TRX_QTY13,SUM(XPS.TRX_QTY14) TRX_QTY14,SUM(XPS.TRX_QTY15) TRX_QTY15,
SUM(XPS.TRX_QTY16) TRX_QTY16,SUM(XPS.TRX_QTY17) TRX_QTY17,SUM(XPS.TRX_QTY18) TRX_QTY18,
SUM(XPS.TRX_QTY19) TRX_QTY19,SUM(XPS.TRX_QTY20) TRX_QTY20,SUM(XPS.TRX_QTY21) TRX_QTY21,
SUM(XPS.TRX_QTY22) TRX_QTY22,SUM(XPS.TRX_QTY23) TRX_QTY23,SUM(XPS.TRX_QTY24) TRX_QTY24
FROM T_SALECRDTNG_XPS_48861 XPS,
T_SALECRDTNG_TTP_49354 TTP,
T_SALESCRD_LOCTNMAPNG TLP
WHERE TTP.PARTY_ID = XPS.PRFSNL_ID
AND TTP.LILLY_MARKET_ID = XPS.LLY_THERAPEUTICCLASS_CD
AND TLP.MAPPINGID = 48871
and TLP.PRIMARY_CODE = 'USUA'
AND XPS.PRODUCT_ID IN ('10210','10215')
AND XPS.LLY_PRODUCT_ID IN ('203825','208142')
AND TLP.LOCATION_CODE = TRIM(TTP.MASTER_GEO_ID)
AND TTP.PRIMARY_CD = TLP.PRIMARY_CODE
group by TTP.LILLY_MARKET_ID,
TTP.PRIMARY_CD,
TO_DATE(XPS.PROCESSINGPERIOD_DT, 'MM / DD / YYYY'),
TLP.LOCATION_CODE,
ttp.party_id,
TLP.Geography_name) A,
(SELECT DISTINCT UPPER(SC.TEAMNAME) TEAMNAME,
DECODE(SC.HIERARCHY_LEVEL, 'TERRITORY', 3, 'DISTRICT', 2, 'AREA', 1, 'REGION',
6,'DIVISION',0, NULL) HIERARCHY_LEVEL,
SC.PRODUCT_CODE PRODUCT_CODE,
SC.PRIMARY_CODE PRIMARY_CODE,
SC.CHANNEL CHANNEL,
SC.UNIT UNIT
FROM T_SALESCRDNG_CONFIG SC
WHERE SC.CONFIGID = 48994
AND SC.PRIMARY_CODE = 'USUA'
AND SC.PRODUCT_CODE = 'CIALIS TOTAL') B
WHERE A.PRIMARY_CODE = B.PRIMARY_CODE
) rs
;

SQL Server hangs when running query with multiple joins on empty tables

We have experienced what appears to be an interesting bug in SQL Server that I would like to describe to you. I am hoping to find out if it IS actually a bug, and if so where I can found out more information about it. If it is not a bug, I hope somebody can explain to me why it is not and what we have unwittingly done wrong. I can't seem to find description of a similar issue and I'm not sure if we should report to Microsoft as a bug or what. I will first describe the problem briefly and provide details after.
In short, the problem seems to be that SQL Server chokes on statements containing many joins when some (large?) portion (sorry, can't be more specific here in terms of numbers) of the tables being joined are empty. By choke, I mean that it churns and churns away at the query, never finishing, which causes SQL Server to stop responding entirely. Actually, running the query once causes the CPU to jump to 25% and stay there. Another attempt and CPU jumps to 50%. Sometimes, CPU would go to 75% or 100%, but at the 50% mark, you can't even log in to the database server any more.
The problem occurs in an application that we have that uses Hibernate (via Coldfusion ORM) and has an entity (Document) that is sub-classed into many different types of document. The SQL query in question is generated by Hibernate when we run a query on the base class Document (e.g. "FROM Document WHERE Locked=1"). This results in a very long SQL query that joins all the sub-class tables together (example below) and sometimes kills the server.
A few things of interest that I found while testing:
It ONLY happens when there are a large number of empty joined tables, which is why it was such a bear to track down - never happened when running on our dev or testing databases because they had ample data. So the same query runs instantaneously without issue in one instance and completely kills SQL server in another.
I thought at first that the large number of selected columns in the SQL query might be causing an issue so I tested the SQL query with the same joins but only selected a single column from each table so only a small amout of data was actually being selected - no improvement, still hung.
I ran the query against the DB that had little data and I started removing the joins one by one. I found I could get to a point where the query would run and run quickly. Adding a join back from this point would cause the query to run, but noticeably slower, and repeating this a few more times would eventually cause it to grind to a halt again and kill the server.
So based on the above I've surmised that there is a bug (or a known fact of which I am ignorant) in SQL Server whereby joins resulting in too many nulls cause an issue. I think it might be a bug rather than a known limitation because it does not result in an error message (like, for example, you would get if you attempted a query with more than 2100 items in an IN clause), but rather a hung server. The server goes into la-la land almost like the joins had resulted in a cartesion product, but it's the opposite - instead of huge amounts of data, I have no data.
Has anybody else ever encountered such an issue? Is there some known limit of SQL server that I am rubbing up against? I have had great difficulty googling the issue because it is hard to define.
Should I file a bug with Microsoft for this?
Any insights are greatly appreciated. If any more information is needed from me, please let me know.
Thanks.
Example query follows (I've removed most of the selected columns for brevity):
select
top 1 document0_.ID as ID715_,
document0_.CreatedOn as CreatedOn715_,
document0_.UpdatedOn as UpdatedOn715_,
document0_.DeletedOn as DeletedOn715_,
document0_.Deleted as Deleted715_,
document0_.Active as Active715_,
document0_.ActivatedOn as Activate8_715_,
document0_.DeactivatedOn as Deactiva9_715_,
document0_.Locked as Locked715_,
document0_.CompletedDate as Complet11_715_,
document0_.CreateUserID as CreateU23_715_,
document0_.UpdateUserID as UpdateU24_715_,
document0_1_.CreatedOn as CreatedOn717_,
document0_1_.UpdatedOn as UpdatedOn717_,
document0_1_.DeletedOn as DeletedOn717_,
document0_1_.Deleted as Deleted717_,
document0_1_.Active as Active717_,
document0_1_.ActivatedOn as Activate7_717_,
document0_1_.DeactivatedOn as Deactiva8_717_,
document0_1_.Locked as Locked717_,
document0_1_.DateReceived as DateRec10_717_,
document0_1_.DateIssued as DateIssued717_,
document0_1_.CreateUserID as CreateU12_717_,
document0_1_.UpdateUserID as UpdateU13_717_,
document0_2_.CreatedOn as CreatedOn733_,
document0_2_.UpdatedOn as UpdatedOn733_,
document0_2_.DeletedOn as DeletedOn733_,
document0_2_.Deleted as Deleted733_,
document0_2_.Active as Active733_,
document0_2_.ActivatedOn as Activate7_733_,
document0_2_.DeactivatedOn as Deactiva8_733_,
document0_2_.Locked as Locked733_,
document0_3_.CreatedOn as CreatedOn739_,
document0_3_.UpdatedOn as UpdatedOn739_,
document0_3_.DeletedOn as DeletedOn739_,
document0_3_.Deleted as Deleted739_,
document0_3_.Active as Active739_,
document0_3_.ActivatedOn as Activate7_739_,
document0_3_.DeactivatedOn as Deactiva8_739_,
document0_3_.Locked as Locked739_,
document0_4_.CreatedOn as CreatedOn754_,
document0_4_.UpdatedOn as UpdatedOn754_,
document0_4_.DeletedOn as DeletedOn754_,
document0_4_.Deleted as Deleted754_,
document0_4_.Active as Active754_,
document0_4_.ActivatedOn as Activate7_754_,
document0_4_.DeactivatedOn as Deactiva8_754_,
document0_4_.Locked as Locked754_,
document0_5_.CreatedOn as CreatedOn755_,
document0_5_.UpdatedOn as UpdatedOn755_,
document0_5_.DeletedOn as DeletedOn755_,
document0_5_.Deleted as Deleted755_,
document0_5_.Active as Active755_,
document0_5_.ActivatedOn as Activate7_755_,
document0_5_.DeactivatedOn as Deactiva8_755_,
document0_5_.Locked as Locked755_,
document0_6_.CreatedOn as CreatedOn759_,
document0_6_.UpdatedOn as UpdatedOn759_,
document0_6_.DeletedOn as DeletedOn759_,
document0_6_.Deleted as Deleted759_,
document0_6_.Active as Active759_,
document0_6_.ActivatedOn as Activate7_759_,
document0_6_.DeactivatedOn as Deactiva8_759_,
document0_6_.Locked as Locked759_,
document0_7_.CreatedOn as CreatedOn773_,
document0_7_.UpdatedOn as UpdatedOn773_,
document0_7_.DeletedOn as DeletedOn773_,
document0_7_.Deleted as Deleted773_,
document0_7_.Active as Active773_,
document0_7_.ActivatedOn as Activate7_773_,
document0_7_.DeactivatedOn as Deactiva8_773_,
document0_7_.Locked as Locked773_,
document0_8_.CreatedOn as CreatedOn774_,
document0_8_.UpdatedOn as UpdatedOn774_,
document0_8_.DeletedOn as DeletedOn774_,
document0_8_.Deleted as Deleted774_,
document0_8_.Active as Active774_,
document0_8_.ActivatedOn as Activate7_774_,
document0_8_.DeactivatedOn as Deactiva8_774_,
document0_8_.Locked as Locked774_,
document0_9_.CreatedOn as CreatedOn779_,
document0_9_.UpdatedOn as UpdatedOn779_,
document0_9_.DeletedOn as DeletedOn779_,
document0_9_.Deleted as Deleted779_,
document0_9_.Active as Active779_,
document0_9_.ActivatedOn as Activate7_779_,
document0_9_.DeactivatedOn as Deactiva8_779_,
document0_9_.Locked as Locked779_,
document0_10_.CreatedOn as CreatedOn780_,
document0_10_.UpdatedOn as UpdatedOn780_,
document0_10_.DeletedOn as DeletedOn780_,
document0_10_.Deleted as Deleted780_,
document0_10_.Active as Active780_,
document0_10_.ActivatedOn as Activate7_780_,
document0_10_.DeactivatedOn as Deactiva8_780_,
document0_10_.Locked as Locked780_,
document0_11_.CreatedOn as CreatedOn781_,
document0_11_.UpdatedOn as UpdatedOn781_,
document0_11_.DeletedOn as DeletedOn781_,
document0_11_.Deleted as Deleted781_,
document0_11_.Active as Active781_,
document0_11_.ActivatedOn as Activate7_781_,
document0_11_.DeactivatedOn as Deactiva8_781_,
document0_11_.Locked as Locked781_,
document0_11_.ExpiryDate as ExpiryDate781_,
document0_11_.Number as Number781_,
document0_11_.CreateUserID as CreateU12_781_,
document0_11_.UpdateUserID as UpdateU13_781_,
document0_12_.CreatedOn as CreatedOn784_,
document0_12_.UpdatedOn as UpdatedOn784_,
document0_12_.DeletedOn as DeletedOn784_,
document0_12_.Deleted as Deleted784_,
document0_12_.Active as Active784_,
document0_12_.ActivatedOn as Activate7_784_,
document0_12_.DeactivatedOn as Deactiva8_784_,
document0_12_.Locked as Locked784_,
document0_12_.CreateUserID as CreateU10_784_,
document0_12_.UpdateUserID as UpdateU11_784_,
document0_12_.SurveyID as SurveyID784_,
document0_13_.CreatedOn as CreatedOn789_,
document0_13_.UpdatedOn as UpdatedOn789_,
document0_13_.DeletedOn as DeletedOn789_,
document0_13_.Deleted as Deleted789_,
document0_13_.Active as Active789_,
document0_13_.ActivatedOn as Activate7_789_,
document0_13_.DeactivatedOn as Deactiva8_789_,
document0_13_.Locked as Locked789_,
document0_14_.CreatedOn as CreatedOn790_,
document0_14_.UpdatedOn as UpdatedOn790_,
document0_14_.DeletedOn as DeletedOn790_,
document0_14_.Deleted as Deleted790_,
document0_14_.Active as Active790_,
document0_14_.ActivatedOn as Activate7_790_,
document0_14_.DeactivatedOn as Deactiva8_790_,
document0_14_.Locked as Locked790_,
document0_14_.aboriginal as aboriginal790_,
document0_14_.disability as disability790_,
document0_14_.minority as minority790_,
document0_14_.refuse as refuse790_,
document0_14_.CreateUserID as CreateU14_790_,
document0_14_.UpdateUserID as UpdateU15_790_,
document0_15_.CreatedOn as CreatedOn791_,
document0_15_.UpdatedOn as UpdatedOn791_,
document0_15_.DeletedOn as DeletedOn791_,
document0_15_.Deleted as Deleted791_,
document0_15_.Active as Active791_,
document0_15_.ActivatedOn as Activate7_791_,
document0_15_.DeactivatedOn as Deactiva8_791_,
document0_15_.Locked as Locked791_,
document0_15_.TypeOfTraining as TypeOfT10_791_,
document0_15_.Location as Location791_,
document0_15_.TrainerName as Trainer12_791_,
document0_15_.[Result] as Result13_791_,
document0_15_.TrainingDate as Trainin14_791_,
document0_15_.ExpiryDate as ExpiryDate791_,
document0_15_.CreateUserID as CreateU16_791_,
document0_15_.UpdateUserID as UpdateU17_791_,
document0_16_.CreatedOn as CreatedOn792_,
document0_16_.UpdatedOn as UpdatedOn792_,
document0_16_.DeletedOn as DeletedOn792_,
document0_16_.Deleted as Deleted792_,
document0_16_.Active as Active792_,
document0_16_.ActivatedOn as Activate7_792_,
document0_16_.DeactivatedOn as Deactiva8_792_,
document0_16_.Locked as Locked792_,
document0_16_.CreateUserID as CreateU10_792_,
document0_16_.UpdateUserID as UpdateU11_792_,
document0_17_.CreatedOn as CreatedOn793_,
document0_17_.UpdatedOn as UpdatedOn793_,
document0_17_.DeletedOn as DeletedOn793_,
document0_17_.Deleted as Deleted793_,
document0_17_.Active as Active793_,
document0_17_.ActivatedOn as Activate7_793_,
document0_17_.DeactivatedOn as Deactiva8_793_,
document0_17_.Locked as Locked793_,
document0_17_.Content as Content793_,
document0_17_.CreateUserID as CreateU11_793_,
document0_17_.UpdateUserID as UpdateU12_793_,
document0_18_.CreatedOn as CreatedOn795_,
document0_18_.UpdatedOn as UpdatedOn795_,
document0_18_.DeletedOn as DeletedOn795_,
document0_18_.Deleted as Deleted795_,
document0_18_.Active as Active795_,
document0_18_.ActivatedOn as Activate7_795_,
document0_18_.DeactivatedOn as Deactiva8_795_,
document0_18_.Locked as Locked795_,
document0_19_.CreatedOn as CreatedOn800_,
document0_19_.UpdatedOn as UpdatedOn800_,
document0_19_.DeletedOn as DeletedOn800_,
document0_19_.Deleted as Deleted800_,
document0_19_.Active as Active800_,
document0_19_.ActivatedOn as Activate7_800_,
document0_19_.DeactivatedOn as Deactiva8_800_,
document0_19_.Locked as Locked800_,
document0_20_.CreatedOn as CreatedOn802_,
document0_20_.UpdatedOn as UpdatedOn802_,
document0_20_.DeletedOn as DeletedOn802_,
document0_20_.Deleted as Deleted802_,
document0_20_.Active as Active802_,
document0_20_.ActivatedOn as Activate7_802_,
document0_20_.DeactivatedOn as Deactiva8_802_,
document0_20_.Locked as Locked802_,
document0_20_.StartDate as StartDate802_,
document0_20_.EndDate as EndDate802_,
document0_21_.CreatedOn as CreatedOn807_,
document0_21_.UpdatedOn as UpdatedOn807_,
document0_21_.DeletedOn as DeletedOn807_,
document0_21_.Deleted as Deleted807_,
document0_21_.Active as Active807_,
document0_21_.ActivatedOn as Activate7_807_,
document0_21_.DeactivatedOn as Deactiva8_807_,
document0_21_.Locked as Locked807_,
document0_21_.CreateUserID as CreateU18_807_,
document0_21_.UpdateUserID as UpdateU19_807_,
document0_21_.EducationLevelTypeID as Educati21_807_,
document0_22_.CreatedOn as CreatedOn808_,
document0_22_.UpdatedOn as UpdatedOn808_,
document0_22_.DeletedOn as DeletedOn808_,
document0_22_.Deleted as Deleted808_,
document0_22_.Active as Active808_,
document0_22_.ActivatedOn as Activate7_808_,
document0_22_.DeactivatedOn as Deactiva8_808_,
document0_22_.Locked as Locked808_,
document0_22_.CreateUserID as CreateU10_808_,
document0_22_.UpdateUserID as UpdateU11_808_,
document0_23_.CreatedOn as CreatedOn809_,
document0_23_.UpdatedOn as UpdatedOn809_,
document0_23_.DeletedOn as DeletedOn809_,
document0_23_.Deleted as Deleted809_,
document0_23_.Active as Active809_,
document0_23_.ActivatedOn as Activate7_809_,
document0_23_.DeactivatedOn as Deactiva8_809_,
document0_23_.Locked as Locked809_,
document0_24_.CreatedOn as CreatedOn810_,
document0_24_.UpdatedOn as UpdatedOn810_,
document0_24_.DeletedOn as DeletedOn810_,
document0_24_.Deleted as Deleted810_,
document0_24_.Active as Active810_,
document0_24_.ActivatedOn as Activate7_810_,
document0_24_.DeactivatedOn as Deactiva8_810_,
document0_24_.Locked as Locked810_,
document0_24_.CreateUserID as CreateU10_810_,
document0_24_.UpdateUserID as UpdateU11_810_,
document0_25_.CreatedOn as CreatedOn811_,
document0_25_.UpdatedOn as UpdatedOn811_,
document0_25_.DeletedOn as DeletedOn811_,
document0_25_.Deleted as Deleted811_,
document0_25_.Active as Active811_,
document0_25_.ActivatedOn as Activate7_811_,
document0_25_.DeactivatedOn as Deactiva8_811_,
document0_25_.Locked as Locked811_,
document0_26_.CreatedOn as CreatedOn815_,
document0_26_.UpdatedOn as UpdatedOn815_,
document0_26_.DeletedOn as DeletedOn815_,
document0_26_.Deleted as Deleted815_,
document0_26_.Active as Active815_,
document0_26_.ActivatedOn as Activate7_815_,
document0_26_.DeactivatedOn as Deactiva8_815_,
document0_26_.Locked as Locked815_,
document0_27_.CreatedOn as CreatedOn816_,
document0_27_.UpdatedOn as UpdatedOn816_,
document0_27_.DeletedOn as DeletedOn816_,
document0_27_.Deleted as Deleted816_,
document0_27_.Active as Active816_,
document0_27_.ActivatedOn as Activate7_816_,
document0_27_.DeactivatedOn as Deactiva8_816_,
document0_27_.Locked as Locked816_,
document0_27_.DateReceived as DateRec10_816_,
document0_27_.DateIssued as DateIssued816_,
document0_27_.CreateUserID as CreateU12_816_,
document0_27_.UpdateUserID as UpdateU13_816_,
document0_28_.CreatedOn as CreatedOn819_,
document0_28_.UpdatedOn as UpdatedOn819_,
document0_28_.DeletedOn as DeletedOn819_,
document0_28_.Deleted as Deleted819_,
document0_28_.Active as Active819_,
document0_28_.ActivatedOn as Activate7_819_,
document0_28_.DeactivatedOn as Deactiva8_819_,
document0_28_.Locked as Locked819_,
document0_28_.LicenceDocumentNumber as Licence10_819_,
document0_28_.DemeritPoints as Demerit11_819_,
document0_28_.MeritPoints as MeritPo12_819_,
document0_28_.ExpiryDate as ExpiryDate819_,
document0_28_.CreateUserID as CreateU14_819_,
document0_28_.UpdateUserID as UpdateU15_819_,
document0_28_.LicenceDocumentStatusTypeID as Licence16_819_,
document0_28_.StateProvinceID as StatePr17_819_,
document0_29_.CreatedOn as CreatedOn820_,
document0_29_.UpdatedOn as UpdatedOn820_,
document0_29_.DeletedOn as DeletedOn820_,
document0_29_.Deleted as Deleted820_,
document0_29_.Active as Active820_,
document0_29_.ActivatedOn as Activate7_820_,
document0_29_.DeactivatedOn as Deactiva8_820_,
document0_29_.Locked as Locked820_,
document0_29_.CreateUserID as CreateU10_820_,
document0_29_.UpdateUserID as UpdateU11_820_,
document0_30_.CreatedOn as CreatedOn821_,
document0_30_.UpdatedOn as UpdatedOn821_,
document0_30_.DeletedOn as DeletedOn821_,
document0_30_.Deleted as Deleted821_,
document0_30_.Active as Active821_,
document0_30_.ActivatedOn as Activate7_821_,
document0_30_.DeactivatedOn as Deactiva8_821_,
document0_30_.Locked as Locked821_,
document0_30_.ExpiryDate as ExpiryDate821_,
document0_30_.LicenceNumber as Licence11_821_,
document0_30_.CreateUserID as CreateU12_821_,
document0_30_.UpdateUserID as UpdateU13_821_,
document0_30_.StateProvinceID as StatePr14_821_,
document0_31_.CreatedOn as CreatedOn822_,
document0_31_.UpdatedOn as UpdatedOn822_,
document0_31_.DeletedOn as DeletedOn822_,
document0_31_.Deleted as Deleted822_,
document0_31_.Active as Active822_,
document0_31_.ActivatedOn as Activate7_822_,
document0_31_.DeactivatedOn as Deactiva8_822_,
document0_31_.Locked as Locked822_,
document0_32_.CreatedOn as CreatedOn823_,
document0_32_.UpdatedOn as UpdatedOn823_,
document0_32_.DeletedOn as DeletedOn823_,
document0_32_.Deleted as Deleted823_,
document0_32_.Active as Active823_,
document0_32_.ActivatedOn as Activate7_823_,
document0_32_.DeactivatedOn as Deactiva8_823_,
document0_32_.Locked as Locked823_,
document0_33_.CreatedOn as CreatedOn824_,
document0_33_.UpdatedOn as UpdatedOn824_,
document0_33_.DeletedOn as DeletedOn824_,
document0_33_.Deleted as Deleted824_,
document0_33_.Active as Active824_,
document0_33_.ActivatedOn as Activate7_824_,
document0_33_.DeactivatedOn as Deactiva8_824_,
document0_33_.Locked as Locked824_,
document0_33_.CreateUserID as CreateU10_824_,
document0_33_.UpdateUserID as UpdateU11_824_,
document0_34_.CreatedOn as CreatedOn825_,
document0_34_.UpdatedOn as UpdatedOn825_,
document0_34_.DeletedOn as DeletedOn825_,
document0_34_.Deleted as Deleted825_,
document0_34_.Active as Active825_,
document0_34_.ActivatedOn as Activate7_825_,
document0_34_.DeactivatedOn as Deactiva8_825_,
document0_34_.Locked as Locked825_,
document0_35_.CreatedOn as CreatedOn826_,
document0_35_.UpdatedOn as UpdatedOn826_,
document0_35_.DeletedOn as DeletedOn826_,
document0_35_.Deleted as Deleted826_,
document0_35_.Active as Active826_,
document0_35_.ActivatedOn as Activate7_826_,
document0_35_.DeactivatedOn as Deactiva8_826_,
document0_35_.Locked as Locked826_,
document0_35_.ExpiryDate as ExpiryDate826_,
document0_35_.CreateUserID as CreateU11_826_,
document0_35_.UpdateUserID as UpdateU12_826_,
document0_36_.CreatedOn as CreatedOn827_,
document0_36_.UpdatedOn as UpdatedOn827_,
document0_36_.DeletedOn as DeletedOn827_,
document0_36_.Deleted as Deleted827_,
document0_36_.Active as Active827_,
document0_36_.ActivatedOn as Activate7_827_,
document0_36_.DeactivatedOn as Deactiva8_827_,
document0_36_.Locked as Locked827_,
document0_36_.ExpiryDate as ExpiryDate827_,
document0_36_.CreateUserID as CreateU11_827_,
document0_36_.UpdateUserID as UpdateU12_827_,
document0_37_.CreatedOn as CreatedOn829_,
document0_37_.UpdatedOn as UpdatedOn829_,
document0_37_.DeletedOn as DeletedOn829_,
document0_37_.Deleted as Deleted829_,
document0_37_.Active as Active829_,
document0_37_.ActivatedOn as Activate7_829_,
document0_37_.DeactivatedOn as Deactiva8_829_,
document0_37_.Locked as Locked829_,
document0_37_.CheckDate as CheckDate829_,
document0_37_.CreateUserID as CreateU11_829_,
document0_37_.UpdateUserID as UpdateU12_829_,
document0_38_.CreatedOn as CreatedOn830_,
document0_38_.UpdatedOn as UpdatedOn830_,
document0_38_.DeletedOn as DeletedOn830_,
document0_38_.Deleted as Deleted830_,
document0_38_.Active as Active830_,
document0_38_.ActivatedOn as Activate7_830_,
document0_38_.DeactivatedOn as Deactiva8_830_,
document0_38_.Locked as Locked830_,
document0_39_.CreatedOn as CreatedOn831_,
document0_39_.UpdatedOn as UpdatedOn831_,
document0_39_.DeletedOn as DeletedOn831_,
document0_39_.Deleted as Deleted831_,
document0_39_.Active as Active831_,
document0_39_.ActivatedOn as Activate7_831_,
document0_39_.DeactivatedOn as Deactiva8_831_,
document0_39_.Locked as Locked831_,
document0_40_.CreatedOn as CreatedOn832_,
document0_40_.UpdatedOn as UpdatedOn832_,
document0_40_.DeletedOn as DeletedOn832_,
document0_40_.Deleted as Deleted832_,
document0_40_.Active as Active832_,
document0_40_.ActivatedOn as Activate7_832_,
document0_40_.DeactivatedOn as Deactiva8_832_,
document0_40_.Locked as Locked832_,
document0_41_.CreatedOn as CreatedOn833_,
document0_41_.UpdatedOn as UpdatedOn833_,
document0_41_.DeletedOn as DeletedOn833_,
document0_41_.Deleted as Deleted833_,
document0_41_.Active as Active833_,
document0_41_.ActivatedOn as Activate7_833_,
document0_41_.DeactivatedOn as Deactiva8_833_,
document0_41_.Locked as Locked833_,
document0_41_.CreateUserID as CreateU10_833_,
document0_41_.UpdateUserID as UpdateU11_833_,
document0_.Subtype as Subtype715_
from
Document document0_
left outer join
CustomDocument1 document0_1_
on document0_.ID=document0_1_.DocumentID
left outer join
CustomDocument2 document0_2_
on document0_.ID=document0_2_.DocumentID
left outer join
CustomDocument3 document0_3_
on document0_.ID=document0_3_.DocumentID
left outer join
CustomDocument4 document0_4_
on document0_.ID=document0_4_.DocumentID
left outer join
CustomDocument5 document0_5_
on document0_.ID=document0_5_.DocumentID
left outer join
CustomDocument6 document0_6_
on document0_.ID=document0_6_.DocumentID
left outer join
CustomDocument7 document0_7_
on document0_.ID=document0_7_.DocumentID
left outer join
CustomDocument8 document0_8_
on document0_.ID=document0_8_.DocumentID
left outer join
CustomDocument9 document0_9_
on document0_.ID=document0_9_.DocumentID
left outer join
CustomDocument10 document0_10_
on document0_.ID=document0_10_.DocumentID
left outer join
CustomDocument11 document0_11_
on document0_.ID=document0_11_.DocumentID
left outer join
CustomDocument12 document0_12_
on document0_.ID=document0_12_.DocumentID
left outer join
CustomDocument13 document0_13_
on document0_.ID=document0_13_.DocumentID
left outer join
CustomDocument14 document0_14_
on document0_.ID=document0_14_.DocumentID
left outer join
CustomDocument15 document0_15_
on document0_.ID=document0_15_.DocumentID
left outer join
CustomDocument16 document0_16_
on document0_.ID=document0_16_.DocumentID
left outer join
CustomDocument17 document0_17_
on document0_.ID=document0_17_.DocumentID
left outer join
CustomDocument18 document0_18_
on document0_.ID=document0_18_.DocumentID
left outer join
CustomDocument19 document0_19_
on document0_.ID=document0_19_.DocumentID
left outer join
CustomDocument20 document0_20_
on document0_.ID=document0_20_.DocumentID
left outer join
CustomDocument21 document0_21_
on document0_.ID=document0_21_.DocumentID
left outer join
CustomDocument22 document0_22_
on document0_.ID=document0_22_.DocumentID
left outer join
CustomDocument23 document0_23_
on document0_.ID=document0_23_.DocumentID
left outer join
CustomDocument24 document0_24_
on document0_.ID=document0_24_.DocumentID
left outer join
CustomDocument25 document0_25_
on document0_.ID=document0_25_.DocumentID
left outer join
CustomDocument26 document0_26_
on document0_.ID=document0_26_.DocumentID
left outer join
CustomDocument27 document0_27_
on document0_.ID=document0_27_.DocumentID
left outer join
CustomDocument28 document0_28_
on document0_.ID=document0_28_.DocumentID
left outer join
CustomDocument29 document0_29_
on document0_.ID=document0_29_.DocumentID
left outer join
CustomDocument30 document0_30_
on document0_.ID=document0_30_.DocumentID
left outer join
CustomDocument31 document0_31_
on document0_.ID=document0_31_.DocumentID
left outer join
CustomDocument32 document0_32_
on document0_.ID=document0_32_.DocumentID
left outer join
CustomDocument33 document0_33_
on document0_.ID=document0_33_.DocumentID
left outer join
CustomDocument34 document0_34_
on document0_.ID=document0_34_.DocumentID
left outer join
CustomDocument35 document0_35_
on document0_.ID=document0_35_.DocumentID
left outer join
CustomDocument36 document0_36_
on document0_.ID=document0_36_.DocumentID
left outer join
CustomDocument37 document0_37_
on document0_.ID=document0_37_.DocumentID
left outer join
CustomDocument38 document0_38_
on document0_.ID=document0_38_.DocumentID
left outer join
CustomDocument39 document0_39_
on document0_.ID=document0_39_.DocumentID
left outer join
CustomDocument40 document0_40_
on document0_.ID=document0_40_.DocumentID
left outer join
CustomDocument41 document0_41_
on document0_.ID=document0_41_.DocumentID
where
document0_.DocumentTypeID=?
and document0_.Locked=?
Thanks to #TabAlleman, #Ionic et al. The comments helped me realize that it is the calculation of the execution plan that is sucking up so many resources. This understanding led me to find this SO question asking about execution plans taking a long time to generate in SQL Server 2014.
I should have mentioned that we are using SQL Server 2014 as it turns out this version introduced an aggressive new query optimizer that has trouble with the query in question. If I set the database compatibility to SQL Server 2012, the execution plan is generated immediately.
I may still log a bug in SQL Connect as this query may represent an edge case that needs looking into.
Thanks all.

Select in LINQ with a strange value #p__linq__0

I have this select in LINQ
public List<EquipamentoNoDiscovery> GetEquipamentosNoDiscovery(int imID)
var lista = (from ma in ctx.macaddress
join m in ctx.mac on
ma.address_mac equals m.mac_id into g1
from m in g1.DefaultIfEmpty()
join ml in ctx.mac_link on
m.mac_id equals ml.mac_id into g2
from ml in g2.DefaultIfEmpty()
join im in ctx.immobile on
ml.link_id equals im.immobile_id into g3
from im in g3.DefaultIfEmpty()
join en in ctx.enterprise on
im.enterprise_id equals en.enterprise_id into g4
from en in g4.DefaultIfEmpty()
join pl in ctx.port_link on
ma.address_id equals pl.address_id into g5
from pl in g5.DefaultIfEmpty()
join p in ctx.port on
new { pl.sw_id, pl.port_id } equals new { p.sw_id, p.port_id }
join s in ctx.switch_lan on
pl.sw_id equals s.sw_id into g6
from s in g6.DefaultIfEmpty()
where pl.address_id == imID
select new
{
Regiao = en.enterprise_u_name,
Predio = im.immobile_u_name,
Equipamento = m.host,
TipoPlaca = m.mac_type,
Mac = ma.address_mac,
Ip_ma = ma.address_ip,
Ip_m = m.ip_address,
Comunidade = s.sw_community,
IpSwitch = s.sw_ip,
PortaIndex = p.port_index,
PortaNome = p.port_name
});
ObjectQuery oQuery = (ObjectQuery)lista;
string cmdSQL = oQuery.ToTraceString();
When I use the command oQuery.ToTraceString(), I can see this "where pl.address_id == imID" become this "WHERE [Extent6].[address_id] = #p_linq_0".
Then my select always return empty, if in SQL command I change the value #p_linq_0 to a number, It works fine.
Any suggestions, please?
Thanks!
That's just a query parameter which comes from this where claused.
where pl.address_id == imID
Your log should also show the value which has been passed for that parameter, which should be the value of imID. Check that value - it's possible that it's not what you expected it to be.

Resources