Secret To Optimizing SQL Queries - Understand The SQL Execution Order

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 พ.ค. 2023
  • Get a Free System Design PDF with 158 pages by subscribing to our weekly newsletter: bytebytego.ck.page/subscribe
    Animation tools: Adobe Illustrator and After Effects.
    Checkout our bestselling System Design Interview books:
    Volume 1: amzn.to/3Ou7gkd
    Volume 2: amzn.to/3HqGozy
    The digital version of System Design Interview books: bit.ly/3mlDSk9
    ABOUT US:
    Covering topics and trends in large-scale system design, from the authors of the best-selling System Design Interview series.

ความคิดเห็น • 146

  • @cumbi-mongo
    @cumbi-mongo ปีที่แล้ว +203

    Great video! One addition: The "EXPLAIN" command is an invaluable tool for optimizing SQL queries. It provides a detailed execution plan, allowing the developers to understand how the database engine processes a query. By analyzing the execution plan, you can address the performance bottlenecks with proper optimizations, e.g. proper indexes.

    • @Omar-ic3wc
      @Omar-ic3wc 11 หลายเดือนก่อน +4

      Thanks for sharing this.

    • @luis5d6b
      @luis5d6b 11 หลายเดือนก่อน +4

      Thanks a lot for the addition, really good :)

    • @ksm1847
      @ksm1847 9 หลายเดือนก่อน +4

      @cmertayak - I second you. It's an awesome command I use many times at my work to optimise. My go to command to improve queries execution.

    • @zeelthumar
      @zeelthumar 7 หลายเดือนก่อน +2

      Thanks for sharing

    • @DavisTibbz
      @DavisTibbz 3 หลายเดือนก่อน

      Oh yes, if you run EXPLAIN in some desktop client like Mysql Workbench, shows you detailed chart diagram of your Query, quite useful

  • @ayazahamed8254
    @ayazahamed8254 8 หลายเดือนก่อน +1

    The way you explained with the animations are Awesome. Great Job. Very Well Explained.

  • @lucyk7292
    @lucyk7292 ปีที่แล้ว +8

    Thank you for a fantastic visualization of the SQL queries execution order. That's exactly what I have been missing in the other materials. I really appreciate your style of teaching

  • @uzair004
    @uzair004 11 หลายเดือนก่อน +51

    Opt for indexes with SELECT, WHERE, JOIN clauses.
    Use full column comparison to get data instead of half or computed comparison (i.e startsWith)
    Avoid ORDER_BY on large data retreval
    Use limit of smaller number with pagination for more data.

    • @sampri22
      @sampri22 หลายเดือนก่อน

      Could you explain how? What if i need large data retrieved with order by. How would i use limit and pagination in this case? Thanks

  • @user-yi4cp1uh1f
    @user-yi4cp1uh1f 9 หลายเดือนก่อน +1

    bro this way of teaching is really really make sense. thanks a lot for these visuals.

  • @AliBensoukehal
    @AliBensoukehal ปีที่แล้ว +2

    Simple and to the point explanation. Love it. Thanks 👍

  • @JohnS-er7jh
    @JohnS-er7jh 10 หลายเดือนก่อน +15

    One of the best SQL videos I have come across, just the way it is put together and the infographics. If you are learning SQL, you really should understand the mechanics behind optimizing queries, how databases work. Just adding more hardware or VM resources will not fix the issue if your queries are not optimized properly.

    • @rembautimes8808
      @rembautimes8808 3 หลายเดือนก่อน +1

      Very well presented, thanks for explaining SARGAble concept

  • @user-si5ue8dv7y
    @user-si5ue8dv7y 4 หลายเดือนก่อน

    This is the best explanation I've ever seen. Big thumbs for you!

  • @caseyspaulding
    @caseyspaulding ปีที่แล้ว

    Wow. To the point with knowledge I can use today. Thank you.

  • @twistedace6365
    @twistedace6365 8 หลายเดือนก่อน

    thanks, helped clear up some issues I had.

  • @JeremyChone
    @JeremyChone ปีที่แล้ว +3

    wow, what an awesome introduction to SQL optimization.

  • @CyberMew
    @CyberMew ปีที่แล้ว +92

    Very good intro. Would like a more detailed explanation on more complex queries.

    • @adicide9070
      @adicide9070 ปีที่แล้ว +12

      they don't do detailed explanations. it's basically "use indexes". don't sort lots of data. well, thanks.

    • @davidlee588
      @davidlee588 ปีที่แล้ว +3

      @@jonbaird9718agreed, TH-cam is made for juniors

  • @jagmanderbazzad8425
    @jagmanderbazzad8425 9 หลายเดือนก่อน

    Nice and simple explanation.Thanks

  • @developersharif
    @developersharif ปีที่แล้ว +1

    *Explanation level is so beautiful!*

  • @tallalmoshrif6643
    @tallalmoshrif6643 10 หลายเดือนก่อน

    Great video, very informative and well explained bravo!

  • @martingrof1685
    @martingrof1685 7 หลายเดือนก่อน

    Amazing. Thank you!

  • @avijeethati5323
    @avijeethati5323 10 หลายเดือนก่อน +1

    Excellent video explaining basic concepts in very short time..❤
    Impressive graphic animation, could you please share how the execution plan animation was done

  • @karunakaranr2473
    @karunakaranr2473 หลายเดือนก่อน

    Thank you for your time and effort to explain any of the subjects. Really like it and more over able to register the concept in mind easily. Thanks again,.

  • @abhinav10x
    @abhinav10x หลายเดือนก่อน +1

    Very profound, please share more on SQL like windows and CTE, your explanation is very approachable.

  • @ThinhLe-eh9re
    @ThinhLe-eh9re 9 หลายเดือนก่อน

    Thanks for your sharing Bro's.

  • @MrSuriyam
    @MrSuriyam หลายเดือนก่อน

    Hi Sir thank you 🙏 for taking the time to explain the SQL. Sorry Iam new and very helpful.

  • @99aabbccddeeff
    @99aabbccddeeff ปีที่แล้ว

    Excellent explanation, thanks!

  • @antonboiko788
    @antonboiko788 9 หลายเดือนก่อน

    cool, didn't think it's possible to include all these concepts in 6 min video. One thing, it's great to watch it when you want to summarise already existing knowledge

  • @robbybankston4238
    @robbybankston4238 11 หลายเดือนก่อน

    Love your channel. Your videos are great.

  • @user-it7ur3bc9n
    @user-it7ur3bc9n 6 หลายเดือนก่อน

    Additionally, for the optimizer to "make up" a reasonably good plan (from the various alternatives), it needs to know a bit about the data (value) distribution. This is where STATISTICS / ANALYZE (depends on the DB vendor) come handy. It helps the optimizer do estimates for the various steps (rows, size of data, etc.) of each plan, and figure out which of the different plans is the best candidate to execute. Therefore it is important to collect this information on critical columns (usually join, where clause columns). It is also important to keep this information regularly refreshed so that the optimizer does not make bad decisions based on stale statistics. Very bad things can happen with stale statistics.

  • @nixjavi7220
    @nixjavi7220 2 หลายเดือนก่อน

    these videos are amazing!!!! thanks!!!

  • @vasiliynet3425
    @vasiliynet3425 ปีที่แล้ว

    Awesome as usual! Thanks a lot!

  • @stpaquet
    @stpaquet ปีที่แล้ว +22

    Understanding how the DB engine works with indexes is key. you may assume that a WHERE purchase_date >= 2022 AND purchase > 100 would be the same if you have indexes on purchase_date and purchase, but it might be required to have a composite index... Order in the WHERE clause may also be important as it helps reducing the dataset before applying the second condition.

    • @MiningForPies
      @MiningForPies 7 หลายเดือนก่อน +2

      WHERE order has no effect on most sql systems. The only way you can force SQL to filter data first is to use a derived query.

  • @darrenklein6090
    @darrenklein6090 หลายเดือนก่อน

    Thank you, this was really helpful.

  • @mer2550
    @mer2550 ปีที่แล้ว

    good things to practice for the interview. Thanks

  • @niketu99
    @niketu99 7 หลายเดือนก่อน

    Superb video! Simple explanation on query optimisation.

  • @viktorsaranchuk2932
    @viktorsaranchuk2932 11 หลายเดือนก่อน

    Thanks. Good to know! Useful!

  • @user-yq3si3so5n
    @user-yq3si3so5n 2 หลายเดือนก่อน

    Awesome visualization, I've been loving all the short videos on this channel!
    Clarifying Q. The execution order has SELECT happening after HAVING, so this should mean that the calculated column total_spent doesn't exist at the time the HAVING clause is evaluated?

  • @pieter5466
    @pieter5466 ปีที่แล้ว +1

    As usual, excellent and to the point video!

  • @moneycrab
    @moneycrab ปีที่แล้ว +4

    I heard it called "predicate pushdown" when you move a condition earlier in the plan

  • @helal.ismail
    @helal.ismail ปีที่แล้ว

    Very simple and to the point, love the visualization too

  • @gliderguld
    @gliderguld 7 หลายเดือนก่อน

    Well explained. However I do miss 1) the generation of more query-plans and selection amongs them (cost estimations) and (as an element herein) 2) different table access tactics (sequential scan, index access or index only).

  • @yashwanthbedre8220
    @yashwanthbedre8220 11 หลายเดือนก่อน

    Best explanation ever

  • @bigbrother1211
    @bigbrother1211 ปีที่แล้ว

    Thank you so much!

  • @sengs.4838
    @sengs.4838 10 หลายเดือนก่อน

    So good explanations

  • @Sudharshaunvilla
    @Sudharshaunvilla ปีที่แล้ว

    Fantastic explanation.

  • @antonanton22743
    @antonanton22743 ปีที่แล้ว

    thanks a lot for your content

  • @naveenverma2951
    @naveenverma2951 หลายเดือนก่อน

    oh my goodness, this is too good for non IT background jumping ship to see where AI will land. Thx. You are my 3blue1brown for IT

  • @anirudh7463
    @anirudh7463 ปีที่แล้ว +3

    This stuff is gold. Thank you for making this available for free. Really appreciate it!

  • @mikeshin77
    @mikeshin77 ปีที่แล้ว

    Very good video. It is really helpful.

  • @jamesayento1321
    @jamesayento1321 ปีที่แล้ว

    Thanks for this! Will there be a transcription soon?

  • @christopherli5828
    @christopherli5828 3 หลายเดือนก่อน

    thanks so much!

  • @mohan1958
    @mohan1958 2 หลายเดือนก่อน

    Great. Thanks for sharing..

  • @DerBarde2012
    @DerBarde2012 3 หลายเดือนก่อน

    Your presentation is so pleasant to watch, is it manually key-framed in the video editor or are there tools to do that naturally?

  • @huywarrior
    @huywarrior 10 หลายเดือนก่อน

    Lord Buddha. I'm looking for an active data flow visualization that can shorten data query response times! A great video, it saved me today. Leaving with 1 subscription as a fan! 🔍⚡

  • @zackwong1000
    @zackwong1000 ปีที่แล้ว +7

    You should select from the orders table then join the customers since your where clause is a column in orders table! Your SQL is joining on unnecessary rows from orders & customers!

  • @jerichaux9219
    @jerichaux9219 หลายเดือนก่อน

    You guys are awesome!

  • @gabrielb.962
    @gabrielb.962 ปีที่แล้ว +25

    Index usage tip: When using params in your query (e.g., select .... where year > ?), databases may not utilize an index if it is unbalanced. For instance, if you have approximately 1 million rows with year = 2022 and only 1000 rows with year = 2023, the database cannot predict whether the parameter will be useful for filtering. To resolve this issue, pass the value directly in the query itself, allowing the execution plan to determine if the index is suitable for the intended purpose.

    • @stpaquet
      @stpaquet ปีที่แล้ว +2

      As I wrote in my comment, good understanding on how you db engine works is key. And they are all different. So never assume that a good query on a MySQL will be a good query on Postgres, Oracle or any SQL engine.

    • @maf_aka
      @maf_aka ปีที่แล้ว +8

      this opens the gate for SQL injection, don't do this

    • @maksymbaranovskyi8362
      @maksymbaranovskyi8362 ปีที่แล้ว +1

      @@maf_aka I think the idea was not to use prepared statements *where you don't need them.* E.g. if you already have validation in place that ensures your received value is enum (number, null, etc.) - you can be sure no SQL injection is possible there - so no need to use prepared statements *there.*

    • @lethern2
      @lethern2 10 หลายเดือนก่อน

      Ok, but then you get a different query plan for each (different parameter / set of parameters) query

    • @stpaquet
      @stpaquet 10 หลายเดือนก่อน

      @@lethern2 yep. but that's why you need to understand how your db engine works

  • @resistentemente
    @resistentemente 9 หลายเดือนก่อน

    What program is this used in the presentation?

  • @ashutoshtripathi3817
    @ashutoshtripathi3817 3 หลายเดือนก่อน

    muchas gracias!

  • @AbhishekVaid
    @AbhishekVaid 8 หลายเดือนก่อน

    Something doesn't add well here. If you notice HAVING clause refers to 'total_spent' which is defined in SELECT, so dependency wise HAVING should be after SELECT and not before it.

  • @GabrielGasp
    @GabrielGasp 8 หลายเดือนก่อน

    I always thought that the SELECT happened before HAVING, considering that we can use SELECT aliases in the HAVING filter.

  • @bhooshan25
    @bhooshan25 9 หลายเดือนก่อน

    good explaination

  • @shmuelsigler2918
    @shmuelsigler2918 11 หลายเดือนก่อน

    מדהים!

  • @sriteja2510
    @sriteja2510 10 หลายเดือนก่อน

    Hi The actual plan should be derived from the explain and explain analyze right instead from the query?

  • @NiamorH
    @NiamorH ปีที่แล้ว +4

    Nice bird's-eye view introduction.
    It is not clear how to 'use appropriate indexes' to optimize for sorting, and how to implement pagination. Especially in your example where the sort order is made on an aggregate.

  • @freindimania11
    @freindimania11 ปีที่แล้ว

    order_date is mentioned as indexed - is that implicit or explicitly defined?

  • @seraphfull
    @seraphfull ปีที่แล้ว

    thank you for your video,
    i working on IT with 10 years experience, but I never know the order between JOIN and WHERE,
    utill I watch this video

  • @blackisblack22
    @blackisblack22 11 หลายเดือนก่อน +2

    I have always thought that the Sql structure is poorly designed by not starting from FROM and placing the reference at the end of the statement, for example in a SELECT it should go just before ORDER BY, in an UPDATE the SET after WHERE, etc. Somehow they wanted to remedy the problem by introducing the WITH clause but I'm sure many regret that whoever designed the language should have worked a little harder at the time.

  • @ronenTheBarbarian
    @ronenTheBarbarian ปีที่แล้ว

    Would building a cte table and then running a non-sargable query on it, should also be avoided?

  • @sahandjavid8755
    @sahandjavid8755 4 หลายเดือนก่อน

    Question: at the end of the video you mentioned do not sort the whole data and use pagination for optimizing ORDER BY and LIMIT. Those are the things I use for pagination! What do you mean by that?
    The other thing is from your video LIMIT happens after ORDER BY. How come it can help when ORDER BY has already happened?!
    Btw great videos and content, thank you for these

  • @d4devotion
    @d4devotion ปีที่แล้ว

    This is pretty cool.

  • @Moali11
    @Moali11 7 หลายเดือนก่อน

    Can you make a video explaining the difference between system design and software architecture?

  • @mwaikul
    @mwaikul ปีที่แล้ว +1

    What tool do you use to generate your animations?

  • @crossedpolars
    @crossedpolars 9 หลายเดือนก่อน

    Very good video

  • @zixuanzhao6043
    @zixuanzhao6043 19 วันที่ผ่านมา

    This query actually does not need to join customers table since all the fields are present in the orders table already. (unless there are invalid / dirty customer_id data in the orders table and you want to filter them out)

  • @fishinawaterbottle
    @fishinawaterbottle ปีที่แล้ว +7

    I feel like this is a bit misleading because sometimes where and select influence the first stage. As you said, when there’s a covering index, the database won’t read the entire table. So the select and where influence what is read from the source.
    Order and limit can also come it at the source as well if the index can be used with the order. You refer to this when you talk about “sorting the whole table”.
    CTEs and sub queries are not mentioned but that’s okay i guess.

  • @apexyu164
    @apexyu164 ปีที่แล้ว

    so in the above example, which place we should index ?

  • @MPIPolitics
    @MPIPolitics ปีที่แล้ว +1

    my app didnt reached 40 queries per second yet but i will implement that just in case my app will be next amazon :D

  • @gokulkumarprasoon5158
    @gokulkumarprasoon5158 3 หลายเดือนก่อน

    00:45 Understanding SQL query execution and optimization techniques
    01:30 Understanding SQL execution plans can optimize queries for better performance
    02:15 Optimizing SQL queries through index usage
    03:00 Writing soluble queries is essential for optimizing database performance.
    03:45 Sargable queries improve query performance.
    04:30 Understanding the SQL execution order is crucial for query optimization
    05:15 Optimizing SQL Queries with Indexes
    05:57 Understanding SQL execution order is key
    Crafted by Merlin AI.

  • @MewMewCherry1990
    @MewMewCherry1990 9 หลายเดือนก่อน

    Why are we using HAVING total_spent >_ 1000, but not WHERE total_spent >_ 1000 ? Can you please explain?

  • @sevsxes
    @sevsxes 11 หลายเดือนก่อน

    Will it be even faster if we always order where first and join after?

  • @guocity
    @guocity ปีที่แล้ว

    很不错

  • @theawesomeharris
    @theawesomeharris 10 หลายเดือนก่อน

    hi, can you enable captions/subtitle for this video? thank you!

  • @Mr.zafar_siddiqui
    @Mr.zafar_siddiqui 10 หลายเดือนก่อน

    Can you/anyone please explain execution of case when and window function with group by

  • @yash1152
    @yash1152 10 หลายเดือนก่อน

    0:12 _[JOIN comes before WHERE]_
    is there any way to make the WHERE clause execute first to narrow the rows required to make the JOIN in the first place??
    this is the only reason i still do this using a nested query rather than JOIN

    • @notbalding
      @notbalding 7 หลายเดือนก่อน

      a CTE can be benefitial in your use case.

  • @Matthew8473
    @Matthew8473 3 หลายเดือนก่อน

    This is top-notch in every aspect. I read a book with similar content, and it was top-notch. "Better Sleep Better Life" by William Brook

  • @wjrasmussen666
    @wjrasmussen666 ปีที่แล้ว

    Will this work with MySQL as well?

  • @mahmudulmohtasim7612
    @mahmudulmohtasim7612 11 หลายเดือนก่อน +2

    In this example the 'total_spent' alias is already in use in the HAVING clause without defining. How is that possible?

    • @tianyuhu349
      @tianyuhu349 10 หลายเดือนก่อน

      yes, I have the same question, it doesnt make sense...

  • @MarredNDisenchanted
    @MarredNDisenchanted ปีที่แล้ว +3

    Having uses total_spent from the SELECT, so how come HAVING is executed before the SELECT?

    • @urgorka
      @urgorka 3 หลายเดือนก่อน

      I'd say so too. This is error. First SELECT part is evaluated, then - HAVING part.

  • @amrithpurandhar9882
    @amrithpurandhar9882 7 หลายเดือนก่อน

    Can anyone help me when does the function count or sum will be executed will it be after limit ?

  • @vikrantgaikwad5529
    @vikrantgaikwad5529 ปีที่แล้ว

    I think Order by is evaluated before select as order by might change selected rows...is it correct?

  • @user-ff3gn4re4g
    @user-ff3gn4re4g 2 หลายเดือนก่อน

    is there a way to contact you? I have some specific questions on indexes?

  • @some1rational
    @some1rational ปีที่แล้ว +1

    Is that a typo in the first select clause, total spent should be total_spent?

    • @tianyuhu349
      @tianyuhu349 10 หลายเดือนก่อน +1

      yes, i think so, and I have another question, 'Having' uses total_spent from the SELECT, so how come HAVING is executed before the SELECT? Doesnt make sense...

  • @thuynv
    @thuynv 11 หลายเดือนก่อน

    you should have more subtitles

  • @ghaidam9749
    @ghaidam9749 5 หลายเดือนก่อน

    can someone explain to me what's mutant query plans with a real life example?

  • @KuopassaTv
    @KuopassaTv ปีที่แล้ว +1

    Ambiguous query

  • @MuhammadFahreza
    @MuhammadFahreza 6 หลายเดือนก่อน

    I still don't understand the difference between first point noted on here 3:19 and second point noted on 3:23. Would you mind to re-explain it ? thank you!

  • @andyvoice
    @andyvoice 3 หลายเดือนก่อน

    subtitles not available

  • @AliBensoukehal
    @AliBensoukehal ปีที่แล้ว

    What about mongodb ?

  • @pinusong
    @pinusong 6 หลายเดือนก่อน

    Why are there no subtitles? I need subtitles. Thank you very much!

  • @Yemen-Soft
    @Yemen-Soft 6 หลายเดือนก่อน

    Where is the translation of the CC?

  • @batyr1166
    @batyr1166 ปีที่แล้ว

    Please add subtitles

  • @user-dg9by2ju2y
    @user-dg9by2ju2y ปีที่แล้ว

    👍

  • @haooooo
    @haooooo ปีที่แล้ว

    這集沒字幕..