Being able to use temp table with ef core linq query
Hi there,
It would be beneficial to allow developers to create temporary tables in their queries, enabling them to use variables in the SELECT projection. For example:
var query = dbContext.tableName.Where(...).Join(...).GroupBy(...)
.Select(x=> new someDto
{
var tempTable = (a complex query here: dbContext.tableName....),
prop1 = x.prop1,
prop2 = x.prop2,
prop3 = tempTable.where(t => t.type == x.type).avg(t=>t.prop1)
}
).ToList();
The syntax was just an example. The main idea is to enable breaking down complex queries using temporary tables. This allows us to execute a single query against the database, with all operations running on the DB server, without needing to defer the execution into the application memory side, I hope I conveyed my idea clearly.
Thanks to the great EFCore team.
The main idea is to enable breaking down complex queries using temporary tables. This allows us to execute a single query against the database, with all operations running on the DB server, without needing to defer the execution into the application memory side [...]
You seem to be looking for temp tables as a way to avoid client evaluation of EF's LINQ querying - while I'm definitely aware of various possible usages of temp tables, this specific one is not one of them. So can you please provide a concrete scenario where you're currently forced into client evluation, and where some sort of temp tsble support in EF would help?
The main idea is to enable breaking down complex queries using temporary tables. This allows us to execute a single query against the database, with all operations running on the DB server, without needing to defer the execution into the application memory side [...]
You seem to be looking for temp tables as a way to avoid client evaluation of EF's LINQ querying - while I'm definitely aware of various possible usages of temp tables, this specific one is not one of them. So can you please provide a concrete scenario where you're currently forced into client evluation, and where some sort of temp tsble support in EF would help?
In my query, I'm encountering a SQL Server limitation: "Cannot perform an aggregate function on an expression containing an aggregate or a subquery."
To work around this, I currently use AsEnumerable() to switch to in-memory processing before performing my final Select() projection, which contains the aggregation. I was considering an alternative approach: using a temporary table to hold some parts of the calculation, then performing the aggregation select on that table. This might be more efficient and beneficial for my case.
It's very unlikely that your query would actually run efficiently: EF would need to create an ad-hoc temp table for that query, run one query to populate it, another to make use of it, and then drop it. You're welcome to do an actual benchmark, but it's unlikely this will perform well.
Instead, we should understand exactly which LINQ query you're trying to execute which results in invalid SQL, and how we can improve EF to produce compatible SQL - can you please open a separate issue for that, with a full runnable, minimal code sample?
It's very unlikely that your query would actually run efficiently: EF would need to create an ad-hoc temp table for that query, run one query to populate it, another to make use of it, and then drop it. You're welcome to do an actual benchmark, but it's unlikely this will perform well.
Instead, we should understand exactly which LINQ query you're trying to execute which results in invalid SQL, and how we can improve EF to produce compatible SQL - can you please open a separate issue for that, with a full runnable, minimal code sample?
Sure, I'll bring the query that caused the SQL error during aggregation to a new issue.