I need some help with the optimization of a query with the F# SqlDataConnection Type Provider.
There is a table Items
with the relations to 4 other tables:
Type - n : 1
SubItem1 - m : n
SubItem2 - m : n
SubItem3 - 1 : n
This is the Type Provider query code:
query {
for x in db.Items do
join t in db.ItemType on (x.Typy = t.Name)
select (x, t, x.ItemSubItem1, x.ItemSubItem2, x.SubItem3)
}
|> Seq.map toItem
It produces the following SQL statement:
SELECT [t0].[Id], [t0.Name], [t1].[Name], [t2].[ItemId], [t2].[SubItem1Id], (
SELECT COUNT(*)
FROM [dbo].[ItemSubItem1] AS [t3]
WHERE [t3].[ItemId] = [t0].[Id]
) AS [value]
FROM [dbo].[Item] AS [t0]
INNER JOIN [dbo].[ItemType] AS [t1] ON [t0].[Typ] = [t1].[Name]
LEFT OUTER JOIN [dbo].[ItemSubItem1] AS [t2] ON [t2].[ItemId] = [t0].[Id]
ORDER BY [t0].[Id], [t1].[Name], [t2].[SubItem1Id]
The problem is that only the Type
and SubItems1
are joined. So when toItem
is called for each entry in Items
there will be 2 extra SQL queries generated to get SubItem2
and SubItem3
. This is very inefficient.
Thanks for help!