efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Query: Translate IQueryable.Concat/Union/Intersect/Except/etc. to server

Open simpleway2016 opened this issue 10 years ago • 49 comments

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 avatar Oct 19 '16 01:10 simpleway2016

@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?

divega avatar Oct 19 '16 21:10 divega

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

simpleway2016 avatar Oct 20 '16 01:10 simpleway2016

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.

negatedx avatar May 30 '17 22:05 negatedx

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).

kingmotley avatar Jun 02 '17 20:06 kingmotley

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.

nicholasyin avatar Jul 19 '17 03:07 nicholasyin

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

John0King avatar Aug 31 '17 09:08 John0King

@ajcvickers what's the state of this ?

John0King avatar Oct 25 '17 07:10 John0King

@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.)

ajcvickers avatar Oct 25 '17 16:10 ajcvickers

Prompt to me, where I can read all supported things for EF Core? (e.t. Concat....)

djuuuuus avatar Oct 30 '17 17:10 djuuuuus

@ajcvickers EF Core 2.1 Missing this feature again ? :disappointed:

John0King avatar Feb 08 '18 05:02 John0King

I would be keen to see except implemented too. Any updates on the progress?

Allann avatar Feb 20 '18 23:02 Allann

Enumerable T-SQL
Concat UNION ALL
Except EXCEPT
Intersect INTERSECT
Union UNION

smitpatel avatar Feb 23 '18 21:02 smitpatel

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 .

John0King avatar Mar 01 '18 11:03 John0King

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

felipejunges avatar Mar 26 '18 20:03 felipejunges

Let's wait like "Homunculus loxodontus"

marikvntu avatar Mar 29 '18 13:03 marikvntu

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.

greendimka avatar Apr 01 '18 13:04 greendimka

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.

SanderRossel avatar Apr 02 '18 14:04 SanderRossel

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?

vsopko avatar Apr 06 '18 16:04 vsopko

Unfortunately, the lack of this functionality has made moving to EFCore a non-starter, so still anxiously awaiting it's implementation.

kingmotley avatar Apr 06 '18 23:04 kingmotley