Correlated Subqueries vs Derived Tables

Sdílet
Vložit
  • čas přidán 21. 07. 2024
  • One option for improving query performance is to rewrite the query in order to obtain a completely different execution plan. In this episode we look at how it's possible to rewrite queries with correlated subqueries into derived table syntax and examine what causes the difference in performance.
    Subscribe and turn on notifications to never miss a weekly video: czcams.com/users/DataWithBer...
    Related blog post with example code:
    bertwagner.com/2019/04/23/cor...
    Follow me on Twitter:
    / bertwagner
  • Věda a technologie

Komentáře • 12

  • @h2oautospa998
    @h2oautospa998 Před 5 lety +1

    Thank you for such a well done video. I subscribed because I just finished my first mySQL Database Management class. I have to re-do the class and I think your videos will be a big help.

  • @robertkorth605
    @robertkorth605 Před 2 lety

    I have knowed already it but brilliant presented. Thx

  • @brendafosmire6519
    @brendafosmire6519 Před 4 lety

    Super helpful videos.

  • @stefanotorelli3688
    @stefanotorelli3688 Před 5 lety +1

    Depending on the original query window function could the trick too... but .. as always... it depends...

  • @coding3438
    @coding3438 Před 2 lety +1

    Hey Bert thanks for the amazing tip. I would really appreciate if you can answer this, is there any problem that absolutely requires correlated sub queries that cannot be solved by joins or derived tables.? I don’t like using them because they confuse me. Thanks.

    • @ru2979
      @ru2979 Před rokem

      True , They are nasty 🥺

  • @ulysses1904
    @ulysses1904 Před 5 lety

    Good stuff as always. BTW I have never seen the syntax like (FirstBadgeDate = ) in a select statement. Can you take a moment to explain that, or give it a name so I can research it? I have seen something like that where it was referred to as a quota query but it was in the WHERE clause, like WHERE 2 = (select COUNT(*) from employee EE where E.employeeID = EE.employeeID). I think it was Tony Andrews doing a talk on optimizing DB2 queries where he called it a quota query.

    • @ulysses1904
      @ulysses1904 Před 5 lety

      @@DataWithBert Thanks Bert. Looks like I was overthinking it and I didn't recognize it as a column alias.

  • @zanonilabuschagne7628
    @zanonilabuschagne7628 Před 5 lety

    Thanks Bert. I tried your and got different results. I do want to throw another query out, and get your take on it... In the past, I have achieved great performance gains with CROSS APPLY statements with a TOP in it, but when I tested it in this scenario, the results were atrocious. CPU time and reads went through the roof...
    The query is
    select DISTINCT
    o.UserID
    ,S.FirstBadgeDate
    from Badges o
    CROSS APPLY
    (
    select top 1 [Date] as FirstQuoteDate
    from Badges b
    WHERE b.UserID = i.UserID
    Order by b.Date desc
    ) S
    Any idea why it went so wrong?

    • @MiningForPies
      @MiningForPies Před 3 lety +1

      You’re also selecting the last date, not the first 🤔

  • @KirillBezzubkine
    @KirillBezzubkine Před 4 lety

    nice vid. useful. thx

  • @josephanglada4785
    @josephanglada4785 Před 4 lety +2

    The Final Solution