I am rewriting part of my old NHibernate code to be a database agnostic and use NHibernate queries rather than hard-coded SELECTdatabase statements or representations. I am stuck with what is incredibly slow after being transcribed. The SQL query is as follows:
SELECT
r.recipeingredientid AS id,
r.ingredientid,
r.recipeid,
r.qty,
r.unit,
i.conversiontype,
i.unitweight,
f.unittype,
f.formamount,
f.formunit
FROM recipeingredients r
INNER JOIN shoppingingredients i USING (ingredientid)
LEFT JOIN ingredientforms f USING (ingredientformid)
So, this is a fairly simple query with a JOINs password that selects multiple columns from each table. This query returns about 400,000 rows and takes about 5 seconds to complete. My first attempt to express this as an NHibernate request was this:
var timer = new System.Diagnostics.Stopwatch();
timer.Start();
var recIngs = session.QueryOver<Models.RecipeIngredients>()
.Fetch(prop => prop.Ingredient).Eager()
.Fetch(prop => prop.IngredientForm).Eager()
.List();
timer.Stop();
SQL, 120,264ms. recIngs List<T>, . , - NHibernate ! , . , , , , , .
, , :
IngredientForms joinForm = null;
Ingredients joinIng = null;
var recIngs = session.QueryOver<Models.RecipeIngredients>()
.JoinAlias(r => r.IngredientForm, () => joinForm)
.JoinAlias(r => r.Ingredient, () => joinIng)
.Select(r => joinForm.FormDisplayName)
.List<String>();
JOIN. SQL , FormDisplayName select. 2498 . , -!
, , , . , . - :
.Select(r => new { DisplayName = joinForm.FormDisplayName, IngName = joinIng.DisplayName })
DisplayName IngName. NHibernate:
.
, .List() RecipeIngredients, . .List<Object>() . . , :
.Select(r => new TestType(r))
TestType RecipeIngredients . , , NHibernate :
"NHibernate.MappingException" NHibernate.dll
: : KitchenPC.Modeler.TestType
, NHibernate , RecipeIngredients.
, ? , .Select() . ?
, , , , .
, SQL VIEW ? , . !