How To Optimize EF Core Query Performance With Compiled Queries

Поділитися
Вставка
  • Опубліковано 23 січ 2025

КОМЕНТАРІ • 81

  • @MilanJovanovicTech
    @MilanJovanovicTech  2 роки тому +6

    Want to master Clean Architecture? Go here: bit.ly/3PupkOJ
    Want to unlock Modular Monoliths? Go here: bit.ly/3SXlzSt

    • @wisnu7734
      @wisnu7734 Рік тому

      Hi...i get error when returning Task...please help me

  • @mikewiebel
    @mikewiebel 2 роки тому +3

    Wish I could give you 2 thumbs up. One for detailing exactly how to do it, the other for delving into the performance difference so I already know what to expect. Well done sir.

  • @nouchance
    @nouchance Рік тому +1

    Axaxaxa Preview 😮 TOP content bro you are awesome 💯!

  • @joseagustincarrenogallego3704
    @joseagustincarrenogallego3704 Рік тому +1

    Hi, such a nice and easy way to explain all what you know about EF and how it works! Thanks a lot.

  • @kodindoyannick5328
    @kodindoyannick5328 Рік тому

    Awesome content! You're the best Milan.

  • @bloopers2967
    @bloopers2967 2 роки тому +1

    Love your informative videos man. Much appreciated

  • @nove1398
    @nove1398 2 роки тому +1

    Thanks for sharing this bit of optimization

  • @tarunpahilajani
    @tarunpahilajani 2 роки тому +4

    Thanks for this new piece of information it will be really helpful and i read about in your newsletter as well on Sunday. Just a request from a beginner can you make videos for beginners as well like from basic (more kind of a tutorial)

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 роки тому

      I can't promise that I'll make anything truly beginner friendly soon, since I want to talk about more advanced topics

  • @bugadada
    @bugadada 2 роки тому +3

    Very informative, as usual. Can we expect 'EF: zero to hero' series?

  • @gorVerbinsky
    @gorVerbinsky 2 роки тому +1

    Hello, could you describe why you are using context.Set , and not using context.entity?

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 роки тому +1

      I just prefer it over having to write the DbSets as properties

  • @rs4359
    @rs4359 Рік тому +1

    I don't get it work with table-joins (Linq: include() an List-Property in DbSet, which contains items from 2nd table).

  • @greatpowerplay
    @greatpowerplay 2 роки тому +2

    Is DbContext creation inside a benchmark so insignificant that it is ok have it there? Not sure how much of internal logic is reused with new operator in contrast with pooling contexts.

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 роки тому +1

      DbContext isn't thread safe, so my concern was not running into threading issue during benchmark.

  • @КонстантинОгородов-ш3л

    Why are you create DbContext inside benchmark method?
    In that case you also measure DbContext creation and connection opening instead of measuring just difference between compiled and usual version.

  • @pr0_secrurity876
    @pr0_secrurity876 2 роки тому +4

    What about EF queries vs calling stored procedures

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 роки тому +2

      You're welcome to make that benchmark 😁

    • @Termiux
      @Termiux 9 місяців тому

      Might as well just go back to writing sql

  • @Tamer_Ali
    @Tamer_Ali Рік тому +1

    Is it better to collect related compiled queries in a repository related to their entity then inject the repository when needed or place them in DbContext?

    • @MilanJovanovicTech
      @MilanJovanovicTech  Рік тому

      Placing them where they're used is probably the best practice

    • @Tamer_Ali
      @Tamer_Ali Рік тому

      @@MilanJovanovicTech what if they are a common use compiled queries by some commands or queries?

    • @MilanJovanovicTech
      @MilanJovanovicTech  Рік тому

      @@Tamer_Ali Extension methods, and since the class is static you can place them there 🤔

  • @rayday1672
    @rayday1672 Рік тому +1

    How can implement order by with EF.CompileAsyncQuery

    • @MilanJovanovicTech
      @MilanJovanovicTech  Рік тому

      It should work just fine. Does it not?

    • @rayday1672
      @rayday1672 Рік тому

      @@MilanJovanovicTech it works. On result we need to add order by.

  • @sandordebnar
    @sandordebnar Рік тому +1

    My question would be :
    -What happens if the dataset changes in the background? Is there some security mechanism that recompiles my query if this happens?

    • @MilanJovanovicTech
      @MilanJovanovicTech  Рік тому

      Nothing you can do about it, honestly. I wouldn't even give it a thought

    • @sandordebnar
      @sandordebnar Рік тому

      @@MilanJovanovicTech Is that means: the real usecase cenario is if i use on datasets ,they not frequently changes..isnt it ?

  • @karolg631
    @karolg631 Рік тому +1

    What is better in terms of performance for simple select queries with few filters (2 props lets say). Compiled Async Queries ? Or using ef methods like SqlQuery, FromSql ?

    • @MilanJovanovicTech
      @MilanJovanovicTech  Рік тому

      Do a benchmark? 😅
      My guess is that SQL will be slightly faster - but your needs will dictate what you want to use

  • @Tamer_Ali
    @Tamer_Ali Рік тому +1

    thanks a lot Milan for that awesome video 👍.
    how to create compiled queries in Clean Architecture and I can only access IApplicationDbContext in the Application layer and not ApplicationDbContext which in the Infrastructure layer?

    • @MilanJovanovicTech
      @MilanJovanovicTech  Рік тому

      Hide it inside a repository or the DbContext

    • @Tamer_Ali
      @Tamer_Ali Рік тому

      @@MilanJovanovicTech if I hide it inside DbContext I won't be able to access and call it in a command or a query when I need.

  • @jvianneyjr
    @jvianneyjr 8 місяців тому

    How can I do a compiled async query with dotnet framework 4.5.2 ?

    • @MilanJovanovicTech
      @MilanJovanovicTech  8 місяців тому +1

      Don't know, haven't used .NET Framework in years

    • @jvianneyjr
      @jvianneyjr 8 місяців тому

      @@MilanJovanovicTech ok, I'm using only .net core until now. Kkk thank you !

  • @Nismifor
    @Nismifor 2 роки тому +1

    You’ve talked briefly about the compiled query caching, but if I’m right, that means if I ran the “NotCompiled” version of the query more than once in my application lifetime, the difference in performance would be significantly less, correct ?

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 роки тому +1

      No, the difference in performance is what you see in the benchmark. Since the benchmark is effectively the application lifetime.

  • @I-PixALbI4-I
    @I-PixALbI4-I 9 місяців тому

    Is it possible to use CompileAsyncQuery for Add and Update? Or only for Get ?

    • @MilanJovanovicTech
      @MilanJovanovicTech  9 місяців тому

      I don't think so - at least it doesn't make sense to me. But I didn't try it.

  • @codingbloke
    @codingbloke 2 роки тому +1

    How much does the construction of the DbContext cost. That is a common cost in both tests, if that is significant then the performance improvement is better than what you have stated. OTH if its not significant it might have been worth mentioning to keep that clear
    Other considerations are application start time. Compiling a single simple query may not be that significant but what about a system with a large number of complex queries. Using some lazy approach that compiles and caches the query on demand IMO wouldn't' be hard though.

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 роки тому

      I think creation of the DbContext is insignificant enough to not affect the querying, so I just left it as is.

  • @renzors
    @renzors 11 місяців тому +1

    Nice video. What about an IAsyncEnumerable?

    • @MilanJovanovicTech
      @MilanJovanovicTech  11 місяців тому +1

      Async = more throughput, not necessarily faster queries

  • @kawantrindade2459
    @kawantrindade2459 11 місяців тому

    Thanks!!

  • @mshahidkhanafridi
    @mshahidkhanafridi 7 місяців тому

    Very informative

  • @jonbellamy
    @jonbellamy Рік тому +1

    I enjoyed this video, thank you. As someone who moved away from EF back when it was EF5 to ADO (for performance reasons), I'm now exploring moving back to EF Core as the team has done a great job with performance over the years. ADO is faster, but it's less of a different and EF has lots of benfefits. I have one question though, and wondered if I'm missing something obvious. When you wrote the Async Compiled Query, why did you use FirstOrDefault rather than FirstOrDefaultAsync?

    • @MilanJovanovicTech
      @MilanJovanovicTech  Рік тому +1

      Because there's nothing _magically different_ between FirstOrDefault and FirstOrDefaultAsync in terms of the generated SQL, when you think about it. All the Async version does is to asynchronously await the task completion, while the other one blocks. But they generate the same SQL.
      So in the compiled query, you write the LINQ in terms of the expression you want to run with EF - and the compilation takes care of generating a state-machine to be able to run it asynchronously.
      At least this is my understanding of how it all works. 😅

    • @jonbellamy
      @jonbellamy Рік тому

      @@MilanJovanovicTech Of course! Makes total sense, I think I was missing the obvious. Thanks for taking the time to respond! 👍

  • @onedev7316
    @onedev7316 2 роки тому +2

    another an excellent insight. Thank you. if you could do some thing related to User Impersonation via Jwt token.

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 роки тому +1

      I really hate user impersonation 🤣

    • @onedev7316
      @onedev7316 2 роки тому

      ​@@MilanJovanovicTech understanding the concept with an example will be good :) with in gatherly app :)

    • @onedev7316
      @onedev7316 2 роки тому

      its a no than we wont be seeing anything about user impersonation :) :).

  • @markokraljevic1590
    @markokraljevic1590 2 роки тому +1

    koju web kameru i mikrofon koristis?

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 роки тому

      Kamera: Poco X3 Pro (telefon)
      Mikrofon: Redragon GM300

    • @markokraljevic1590
      @markokraljevic1590 2 роки тому +1

      @@MilanJovanovicTech ne strimujes sa telefona na komp preko app (Iriun npr) nego sve snimis na telefon pa posle prebacis i editujes?

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 роки тому +1

      @@markokraljevic1590 Tako je. Neuporedivo je bolji kvalitet sa telefona. Iriun koristim samo da podesim kameru na pocetku, doduse.

    • @markokraljevic1590
      @markokraljevic1590 2 роки тому

      @@MilanJovanovicTech nisi radio live streamove do sad, niti planiras? jel bi i za to koristio telefon ili nesto drugo?

  • @gasparasp
    @gasparasp 2 роки тому +1

    Can I use conditional inside the CompileQuery? Like build the query based on the input for exemple
    IQueryable query = _context.Set().OrderByDescending(x=> x.Date);
    if (id.HasValue)
    query = query.Where(x => x.Id == id.Value);
    return query;
    Or it needs to be a more objective query, like the examples given

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 роки тому

      Anything that's a valid LINQ expression can be compiled - so no, you can't do something like you proposed there.

    • @gasparasp
      @gasparasp 2 роки тому

      @@MilanJovanovicTech thank you Very much for the anwser!
      I suspected that was the case, still can be useaful in hot paths of static queries

  • @davood7497
    @davood7497 Рік тому +1

    💯💯💯

  • @techpc5453
    @techpc5453 Рік тому +1