Query: Translate IQueryable.Concat/Union/Intersect/Except/etc. to server
EF version:1.0.1.0
var q1 = from m in db.Table1
select new
{
name = m.Name,
};
var q2 = from m in db.Table2
select new
{
name = m.Name,
};
q1 = q1.Concat(q2);
var result = q1.ToArray();
I watch the sql string by ILogger, the sql string is not union two table, the dbconnection will excute two dbcommand for each table like this: command 1:
SELECT "m"."Name"
FROM "Table1" AS "m"
command 2:
SELECT "m"."Name"
FROM "Table2" AS "m"
at EF 6.1 version, it's make a sql string like this:
(SELECT "m"."Name"
FROM "Table1" AS "m") union
(SELECT "m"."Name"
FROM "Table2" AS "m")
@simpleway2016 In general the LINQ implementation of EF Core only attempts to translate specific patterns to equivalent SQL expressions for relational databases.
In the particular case of Queryable.Concat() an appropriate translation could be two SQL queries with UNION ALL in between but instead we concatenate the results in memory.
Changing this to producing a single SQL query on the server isn't currently a top priority because we believe that for many common scenarios (e.g. for the sample queries you posted) there isn't a significant difference.
Could you elaborate on what scenario you think performing the union on the server would be more useful for you?
var q1 = from m in db.Table1
select new
{
name = m.Name,
};
var q2 = from m in db.Table2
select new
{
name = m.Name,
};
q1 = q1.Concat(q2);
var result = q1.OrderBy(m=>m.Name).Skip(1000).Take(10).ToArray();
If i use the ef core like that,it also make two sql string , excute two dbcommand,and concatenate the results in memory, and then excute "OrderBy(m=>m.Name).Skip(1000).Take(10)", I think it's bad performance
I would really like to see this feature too. As @simpleway2016 said above, paging results of a union in the database is much more efficient.
It would have a significant performance difference if there is a large amount of latency between the client and the server (such as being in a different timezone).
Consider a banking application where it needs to display a number of types of transactions, each of which has its own specific properties but also shares some common data with other types, in one page, with paging and sorting. This is a perfect case where translating the concat query into one union all statement would be needed.
Please put this feature on a higher priority ,We really need this feature, it should have much higher priority than Lazy loading, Imagine there are 100,000 rows of data and we only need top 10
@ajcvickers what's the state of this ?
@John0King It's still scheduled for 2.1, but as far as I am aware @smitpatel has not implemented it yet. (It's always possible it won't make 2.1 given we don't know the full schedule yet.)
Prompt to me, where I can read all supported things for EF Core? (e.t. Concat....)
@ajcvickers EF Core 2.1 Missing this feature again ? :disappointed:
I would be keen to see except implemented too. Any updates on the progress?
| Enumerable | T-SQL |
|---|---|
| Concat | UNION ALL |
| Except | EXCEPT |
| Intersect | INTERSECT |
| Union | UNION |
Just a possible workaround for @John0King example
Imagine there are 100,000 rows of data and we only need top 10
You can Select top 10 from the first table, and Select top 10 from the second table, and then union them in memory and show first 10.
It's a side note, we also suffer from inability of EF Core to implement server side Union.
@michael-freidgeim-webjet I already use this for some case, but the really problem is paging .
Waiting for this, to know If I'll have tô change a lot of code in a migration to Core. It would be nice to still can do Union on the SQL Server
Let's wait like "Homunculus loxodontus"
Voting up for the in-server UNION. Imagine you have to union 5 similar queries, each produces 10001 rows: 10000 rows are same for each, and 1 is different. If UNION runs on server - 10005 rows are transferred to a client. If UNION runs locally - 50005 rows are transferred to a client. Makes a difference.
Hearted this feature, seems like basic functionality. I've searched my ass off for this extra SELECT query returning 8000 rows while I instructed a top 100!
context.Table1.Select(...) .Except(context.Table2.Where(...).Select(...)) .OrderBy(...).Skip(...).Take(100).ToList();
Huge performance hit! Makes set operators completely useless for any serious project.
Server side UNION: Linq2Sql - yes Entity Framework - yes EFCore 1.0 - no EFCore 2.0 - no EFCore 2.1 - no? Is there any workarounds or this leaves us in the struggle with outdated technologies?
Unfortunately, the lack of this functionality has made moving to EFCore a non-starter, so still anxiously awaiting it's implementation.