How can I stop IS NULL Checks from being generated in SQL?
Posted: Fri 24 Feb 2012 14:05
Given a query similar to this:
from cm2 in ProjectDataModel.VW_PROJECT_CATALOG
join cm in ProjectDataModel.VW_PROJECT_CATALOG
on new{ PRODID = cm2.PRODID, PRODUNIT = cm2.PROD_UNIT_ID }
equals new { PRODID = cm.PRODID, PRODUNIT = cm.PROD_UNIT_ID}
where productStatus.Contains(cm.PRODSTATUS)
&& processedStatus.Contains(cm2.PRODSTATS)
select cm);
We end up getting a bunch of null checks in the generated sql
FROM PROJECT.VW_PROJECT_CATALOG t1
INNER JOIN PROJECT_APP.VW_PROJECT_CATALOG t2
ON ((t1.PRODID = t2.PRODID)
OR ((t1.PRODID IS NULL) AND (t2.PRODID IS NULL)))
AND ((t1.PROD_UNIT_ID = t2.PROD_UNIT_ID)
OR ((t1.PROD_UNIT_ID IS NULL) AND (t2.PROD_UNIT_ID IS NULL)))
WHERE (t2.PRODSTATUS IN (:p4,:p5))
AND (t1.PRODSTATUS IN (:p6,:p7,:p8))
We have set the columns in the model to Nullable = false, we are unable to modify the db columns themselves as we don't have access to modify them. Is there any other way to get around having this null check generated in the sql? It's causing terrible performance.
from cm2 in ProjectDataModel.VW_PROJECT_CATALOG
join cm in ProjectDataModel.VW_PROJECT_CATALOG
on new{ PRODID = cm2.PRODID, PRODUNIT = cm2.PROD_UNIT_ID }
equals new { PRODID = cm.PRODID, PRODUNIT = cm.PROD_UNIT_ID}
where productStatus.Contains(cm.PRODSTATUS)
&& processedStatus.Contains(cm2.PRODSTATS)
select cm);
We end up getting a bunch of null checks in the generated sql
FROM PROJECT.VW_PROJECT_CATALOG t1
INNER JOIN PROJECT_APP.VW_PROJECT_CATALOG t2
ON ((t1.PRODID = t2.PRODID)
OR ((t1.PRODID IS NULL) AND (t2.PRODID IS NULL)))
AND ((t1.PROD_UNIT_ID = t2.PROD_UNIT_ID)
OR ((t1.PROD_UNIT_ID IS NULL) AND (t2.PROD_UNIT_ID IS NULL)))
WHERE (t2.PRODSTATUS IN (:p4,:p5))
AND (t1.PRODSTATUS IN (:p6,:p7,:p8))
We have set the columns in the model to Nullable = false, we are unable to modify the db columns themselves as we don't have access to modify them. Is there any other way to get around having this null check generated in the sql? It's causing terrible performance.