I'm using Entity framework with the latest connector for .net (6.4.4) in C#
I'm trying a simple groupby query on a very large table (100,000,000 records) in linq
var imprs = from axi in db.aximpressions
where axi.Axd_SiteID == 2
group axi by axi.Axd_BannerID
into g
select new
{
g.Key,
Sum = g.Sum(i => i.Axi_Impressions)
};
The query takes a few minutes to execute
Here is the weird .ToTraceString()
SELECT `GroupBy1`.`K1` AS `Axd_BannerID`,
`GroupBy1`.`A1` AS `C1`
FROM
(
SELECT `Extent1`.`Axd_BannerID` AS `K1`,
SUM(`Extent1`.`Axi_Impressions`) AS `A1`
FROM
(
SELECT
`aximpression`.`Axi_ID`,
[...]
`aximpression`.`Axi_InViewTime`
FROM `aximpression` AS `aximpression`) AS `Extent1`
WHERE 2 = ` Extent1`.`Axd_SiteID`
GROUP BY `Extent1`.`Axd_BannerID`) AS `GroupBy1`"
The innermost query select ALL records
Then another query filter on the Axd_SiteID field
Finally the group by is done on the outermost query
Now I understand why it takes forever
But why the generated query isn't something simple like this ?
select axd_bannerid,
sum(axi_impression)
from AxImpression
where axd_siteid = 2
group by axd_bannerid
This execute in a few seconds
Thanks!
Michel
I'm trying a simple groupby query on a very large table (100,000,000 records) in linq
var imprs = from axi in db.aximpressions
where axi.Axd_SiteID == 2
group axi by axi.Axd_BannerID
into g
select new
{
g.Key,
Sum = g.Sum(i => i.Axi_Impressions)
};
The query takes a few minutes to execute
Here is the weird .ToTraceString()
SELECT `GroupBy1`.`K1` AS `Axd_BannerID`,
`GroupBy1`.`A1` AS `C1`
FROM
(
SELECT `Extent1`.`Axd_BannerID` AS `K1`,
SUM(`Extent1`.`Axi_Impressions`) AS `A1`
FROM
(
SELECT
`aximpression`.`Axi_ID`,
[...]
`aximpression`.`Axi_InViewTime`
FROM `aximpression` AS `aximpression`) AS `Extent1`
WHERE 2 = ` Extent1`.`Axd_SiteID`
GROUP BY `Extent1`.`Axd_BannerID`) AS `GroupBy1`"
The innermost query select ALL records
Then another query filter on the Axd_SiteID field
Finally the group by is done on the outermost query
Now I understand why it takes forever
But why the generated query isn't something simple like this ?
select axd_bannerid,
sum(axi_impression)
from AxImpression
where axd_siteid = 2
group by axd_bannerid
This execute in a few seconds
Thanks!
Michel