Are You Accidentally Crippling Your EF Core Queries?

  Рет қаралды 31,379

Milan Jovanović

Milan Jovanović

Күн бұрын

Пікірлер: 116
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Do you want to simplify your development process? Grab my free Clean Architecture template here: bit.ly/3Andaly Want to master Clean Architecture? Go here: bit.ly/3PupkOJ Want to unlock Modular Monoliths? Go here: bit.ly/3SXlzSt
@sarathkruthiventy4872
@sarathkruthiventy4872 3 ай бұрын
Hi Milan! When ef core did Cartesian product without split query. .did that minimal api end point also returns 68k rows for that emp id similar to SQL results?
@eugene5096
@eugene5096 3 ай бұрын
100K KZbinr , congrats brother !!!
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Thank you 🙌
@dusanknezevic9072
@dusanknezevic9072 3 ай бұрын
Ah, the cartesian explosion. SQL is one of most underrated skills in my opinion. Great SQL skills (and DB skills in general, not just SQL) usually make your software 20x better, wheter you use ORM or not
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Hey Dule :) This is an interesting one, since you'd get a similar problem even with SQL. I did exaggerate the issue by using a very large dataset, but it's still interesting.
@igorpom2
@igorpom2 19 күн бұрын
Большое спасибо за видео! Очень интересно.. Поздравляю с 100 000!
@MilanJovanovicTech
@MilanJovanovicTech 19 күн бұрын
Спасибо! :)
@mohammadjaber3898
@mohammadjaber3898 3 ай бұрын
This is exactly why i allows have the sql profiler open My problem with the new generation of developers whos working EF without having any sql knowledge or how EF is translating I saw over a 20 include in 1 EF core call
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
This is why I always say to learn SQL first 😁
@pilotboba
@pilotboba 3 ай бұрын
You would also have people that don't realize the SQL query that EF writes is bad also. Cartesian explosion was an issue well before people started using ORMs. :)
@gigantedocil
@gigantedocil 3 ай бұрын
Another way to get around this issue without having to use split queries if you're using Postgres is by using the built-in json aggregate functions. Instead of the database returning tons of duplicated rows due to the cartesian explosion problem you explained, it returns the exact JSON that you need. Not sure how this feature works with EF Core, but it is indeed really useful and it would be cool if you could compare both approaches!
@christian.mar.garcia
@christian.mar.garcia 3 ай бұрын
Returning json from sql is also possible in SQL server since the 2016 version. You have to store the result in a varchar(max) variable and return it.
@gigantedocil
@gigantedocil 3 ай бұрын
@@christian.mar.garcia Nice, didn't know. But how do you create the structure of the JSON? Seems like you would be returning a stringified version of the result set?
@christian.mar.garcia
@christian.mar.garcia 3 ай бұрын
@@gigantedocil there are some json functions in t-sql for getting the query results in json format. You have to decide if you want to include an empty array for no data or null for some related data not present.
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Hopefully we get a batching API in EF Core one day
@jordyvu999
@jordyvu999 3 ай бұрын
don’t depend on EF or any orm for complex join queries with perf critical, you don’t know what happened when running, in this case call ef with your raw sql that you can control it.
@antonmartyniuk
@antonmartyniuk 3 ай бұрын
Congratulations on 100K subscribers
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Thank you so much 😀
@juanpaoc1728
@juanpaoc1728 3 ай бұрын
The cross-product in 2:54 is wrong, isn't it? It's not combining the rows correctly right? Correct me if I'm wrong
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Yep, didn't update the yellow table. But seems I fixed it in the 3x3 example
@LucasRosinelli
@LucasRosinelli 3 ай бұрын
I was going to leave a similar comment 😅 thanks @MilanJovanovicTech keep the good work
@victorcomposes
@victorcomposes 3 ай бұрын
Congratulations on 100K.
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Thanks!
@orterves
@orterves 3 ай бұрын
ORMs make this such an easy thing to get wrong by mistake, though even in the raw sql it's not necessarily obvious at a glance. It's a shame that returning complex objects from relational SQL databases never really became a 1st class feature, and we still have to resort to hacks like embedding into json or (ugh) XML, or otherwise fiddle with multi-result sets.
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
I don't think the ORM is to blame here
@FrancoGasperino
@FrancoGasperino 2 ай бұрын
Problem is objects, not SQL. Data is first class.
@orterves
@orterves 2 ай бұрын
@@FrancoGasperino I mean support for sum types as well as product types
@DaminGamerMC
@DaminGamerMC 3 ай бұрын
The thing is sometimes, for more complex use cases, you need joins and left jokns and many queries working together
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
We're not arguing against it 😁
@javidibra6786
@javidibra6786 3 ай бұрын
amazing content man , thanks
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
My pleasure!
@rohanjaykar8652
@rohanjaykar8652 3 ай бұрын
could you please explian projection ? 11:00
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Select(x => { take only the columns you need } )
@satriansyahw1725
@satriansyahw1725 3 ай бұрын
It’s more like in sql , u create temporary table 1 and 2 and then combine them before return to client
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Won't it still have the same problem?
@xopabyteh
@xopabyteh 3 ай бұрын
Amazing video, thanks
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Glad you liked it!
@artemvolsh387
@artemvolsh387 3 ай бұрын
2:05, please add a white screen warning
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
I forgot 😁😁
@shubhammondal9048
@shubhammondal9048 3 ай бұрын
Congratulations for 100k.
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Thanks!
@alexandrosKo
@alexandrosKo 3 ай бұрын
According to EF Core documentation: “Note that cartesian explosion does not occur when the two JOINs aren't at the same level” Are you sure your example applies to cartesian explosion case?
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
I don't know, did you watch the video?
@scottvaughan
@scottvaughan 3 ай бұрын
How did you setup your benchmark project to use the AppDbContext?
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Just hardcoded the connection string
@scottvaughan
@scottvaughan 3 ай бұрын
@@MilanJovanovicTech any chance of the source code. Would like to try this benchmarking out for myself.
@AhmedMohammed23
@AhmedMohammed23 Ай бұрын
would as no tracking with identity help here?
@MilanJovanovicTech
@MilanJovanovicTech Ай бұрын
Not really, no. Different type of issue.
@Kirides
@Kirides 3 ай бұрын
could've mentioned the ginormous difference of data (400KB vs 18MB) on the closing "Employees" benchmark Querysplitting can drastically improve "scalability" (as in reducing shared bandwith usage) and reduce egress costs. especially for common "crud" endpoints where some entities literally join 8 tables, with 4 of them being one-to-many or many-to-many.
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
I was implying that by the # of records retrieved in either example
@gauravkumbhare3628
@gauravkumbhare3628 3 ай бұрын
Better to use linq for complex queries
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Maybe, benchmark it
@PetrVejchoda
@PetrVejchoda 3 ай бұрын
Is there any reason to have the Where method after include? I instinctively write the filter first as if in any other linq. I guess since it is translated into SQL anyway, it doesn't make much difference, but if it were some operations on IEnumerable, you would actually do Include on everything, before discarding majority of it.
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
I prefer having it after Include
@investycoon-app
@investycoon-app 3 ай бұрын
It's a little bit off topic. Which IDE to use for doing .NET on MacOS ? Since few months, Visual Studio is deprecated. Thanks for all your videos !
@savvelev
@savvelev 3 ай бұрын
Rider
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
VS Code?
@sandrorevazishvili1113
@sandrorevazishvili1113 3 ай бұрын
Putting includes before .Where() won't make query slower?
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
It won't matter, since it's all translated into one SQL query
@lambo1707
@lambo1707 3 ай бұрын
how did you load the related data?, did you install a package and configure something in a file?
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
No, just EF includes
@lambo1707
@lambo1707 3 ай бұрын
@@MilanJovanovicTech interesting when we are loading related data it produces error until we configure it in program.cs
@backendsamurai
@backendsamurai 3 ай бұрын
Milan, please submit your implementation of Result pattern to Github if its possible. Thanks for your rich content 👍
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Хвала пуно! 😁
@Maxim.Shiryaev
@Maxim.Shiryaev 3 ай бұрын
As for consistency, a REPEATABLE READ transaction is to the rescue. With its own implications, of course.
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Would detract from performance though
@casperhansen826
@casperhansen826 3 ай бұрын
Now add a text field with Task description
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
What would that do?
@KREKER8331
@KREKER8331 3 ай бұрын
Great video , thank you.
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Thank you too!
@alexkal820
@alexkal820 3 ай бұрын
So if i am correct you are basically saying that if you are using a lot of includes its bad. Then you are suggesting query splitting and then based on benchmarks query splitting is making it slower. Is this correct? If this is correct what is the punchline of the video? Because it is confusing.
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
I don't think you quite got this right. - The problem isn't _just_ the includes. The problem is JOINs on the same level, causing a Cartesian explosion. - Based on the benchmarks, query splitting is 9x faster in the case of a Cartesian explosion. So yes, it's needed here. - The other example, where we just have many JOINs but no Cartesian explosion, there isn't a benefit from split queries.
@TriNguyen-iz9ft
@TriNguyen-iz9ft 3 ай бұрын
How to see the query content in EF ?
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
ToQuery or look at the logs
@onaspnet
@onaspnet 3 ай бұрын
There is a lot of magic (black box + documentation) to read about EF. This is why I like to use my own SQL with Dapper
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
You'd have the same problem in SQL
@krccmsitp2884
@krccmsitp2884 3 ай бұрын
The EF Core docs are very comprehensive and detailed.
@onaspnet
@onaspnet 3 ай бұрын
@MilanJovanovicTech however, inefficient joins are easier to observe and fix.
@onaspnet
@onaspnet 3 ай бұрын
@krccmsitp2884 true and long
@kristiadhy
@kristiadhy 3 ай бұрын
Do you write the SQL statement as part of the code in Dapper or do you use StoreProc? Because in my experience I have made a lot of mistakes caused by typing the wrong SQL statement. That's what I don't like about Dapper. I always prefer using strong type.
@Andrii.S16
@Andrii.S16 3 ай бұрын
Could you suggest a tool for working with SQL Server databases? I find Azure Data Studio a bit lacking.
@rafapioli75
@rafapioli75 3 ай бұрын
DBeaver is a great tool to use with SQL Server!
@xtazyxxx3487
@xtazyxxx3487 3 ай бұрын
SSMS
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
DBeaver is great
@mridulpaul1378
@mridulpaul1378 3 ай бұрын
May I get source code of it?
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
www.patreon.com/posts/source-code-are-111190292
@zahidcakici7968
@zahidcakici7968 3 ай бұрын
Bro why you just didn't filter employeeId (Where(e => e.Id == id)) before the include ?
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
What difference would it make? 😅 This is LINQ -> SQL
@zahidcakici7968
@zahidcakici7968 3 ай бұрын
@@MilanJovanovicTech So you are saying that EfCore optimises query and filter the where query before join or it just doesn’t matter if we filter before join or not?
@omarjamil9449
@omarjamil9449 3 ай бұрын
How can I appear database icon next to entity class? 😅😅
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
ReSharper
@mzuvin
@mzuvin 3 ай бұрын
I know that pagination does not work in split query.
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Probably (but that won't suffer from this issue as much since we're limiting the # of rows we return)
@enricoroselino7557
@enricoroselino7557 3 ай бұрын
wait i might done this 😂 ill check asap
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
And...? 😁
@mzuvin
@mzuvin 3 ай бұрын
splitting the query is not a magic feature, it is proof that ef core is slow.
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Slow is relative
@RayZde
@RayZde 2 ай бұрын
If you want performance, its simple. Dont use EF.
@MilanJovanovicTech
@MilanJovanovicTech 2 ай бұрын
Some people can't be helped
@nonadequateuser7401
@nonadequateuser7401 3 ай бұрын
Am I only one guy who hates EF and don't understand why people still using it?
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Among the few
@ishitvyas
@ishitvyas 3 ай бұрын
Yes you are
@filipnikolic2280
@filipnikolic2280 3 ай бұрын
Probably
@algorithm-artisan
@algorithm-artisan 3 ай бұрын
Yes
@kristiadhy
@kristiadhy 3 ай бұрын
So what is your alternative? I know there is another option like Dapper. But I don't like writing the SQL statement as part of the code because it's prone to errors or typos.
@taiphanvan2403
@taiphanvan2403 3 ай бұрын
Is AsSplitQuery effective when I use the navigation properties in the Select method rather than Include? In the Select method, I only fetch some necessary properties of navigation property rather than fetch all them. Example: var employees = await context .Employees .Select(e => new EmployeeDto() { Salary = e.SalaryPayment... }).ToListAsync();
@xtazyxxx3487
@xtazyxxx3487 3 ай бұрын
Try linqpad to see the sql generated by the linq query. Normally include create joins whereas projection creates subqueries
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Well it's hard to tell from your small example, but you can easily test if split queries work better
Full Text Search in .NET With PostgreSQL and EF Core
24:13
Milan Jovanović
Рет қаралды 10 М.
Exceptions Are Extremely Expensive… Do This Instead
17:15
Milan Jovanović
Рет қаралды 49 М.
It’s all not real
00:15
V.A. show / Магика
Рет қаралды 20 МЛН
Cheerleader Transformation That Left Everyone Speechless! #shorts
00:27
Fabiosa Best Lifehacks
Рет қаралды 16 МЛН
The Correct Way to Run Database Migrations in EF Core
19:51
Nick Chapsas
Рет қаралды 36 М.
Coding Was HARD Until I Learned These 5 Things...
8:34
Elsa Scola
Рет қаралды 823 М.
EF Core Performance Optimization Challenge | 233x FASTER
14:42
Milan Jovanović
Рет қаралды 70 М.
The Right Way To Build Async APIs in ASP.NET Core
20:12
Milan Jovanović
Рет қаралды 23 М.
Learn to code with an unfair advantage.
15:05
Jason Goodison
Рет қаралды 176 М.
Migrating From Docker Compose to .NET Aspire (my experience)
17:01
Milan Jovanović
Рет қаралды 15 М.
Microservices are Technical Debt
31:59
NeetCodeIO
Рет қаралды 700 М.
Make Your HttpClient Resilient - Circuit Breaker, Retry, Timeout
19:23
Milan Jovanović
Рет қаралды 10 М.
What’s the Result Type Everyone Is Using in .NET?
14:47
Nick Chapsas
Рет қаралды 114 М.