Database Indexing Explained (with PostgreSQL)

Ғылым және технология

Get my Fundamentals of Database Engineering udemy course to learn more , link redirects to udemy with coupon applied database.husseinnasser.com
This is a practical video on Database Indexing where I explain what is an index, why do we need it and how it can improve the performance of the queries. Also how (if used incorrectly) it can slow down your queries
intro 0:00
What is an index 0:30
Describe Table 2:15
SELECT [ID] WHERE ID 4:00
SELECT [NAME] WHERE ID 6:30
SELECT [ID] WHERE [NAME] (No index) 9:20
SELECT [ID] WHERE NAME LIKE 11:12
CREATE INDEX ON [NAME] 12:00
SELECT [ID] WHERE NAME (Indexed) 12:50
SELECT [1D] WHERE NAME LIKE (Indexed) 14:30
Summary 16:00
Support my work on PayPal
bit.ly/33ENps4
Become a Member on KZread
/ @hnasr
🧑‍🏫 Courses I Teach
husseinnasser.com/courses
🏭 Backend Engineering Videos in Order
backend.husseinnasser.com
💾 Database Engineering Videos
• Database Engineering
🏰 Load Balancing and Proxies Videos
• Proxies
🏛️ Software Archtiecture Videos
• Software Architecture
📩 Messaging Systems
• Message Queues & PubSu...
Become a Member
/ @hnasr
Support me on PayPal
bit.ly/33ENps4
Stay Awesome,
Hussein

Пікірлер: 235

  • @hnasr
    @hnasr2 жыл бұрын

    Get my Fundamentals of Database Engineering udemy course to learn more , link redirects to udemy with coupon applied database.husseinnasser.com

  • @abhishekyaduvanshi4569

    @abhishekyaduvanshi4569

    Жыл бұрын

    hey can you tell us how sql indexes works on long text , lets we have text indexes in no-sql(mongo) which works good in long text and statements but how long text and statements can be efficiently searched with sql indexes.

  • @shivthedev

    @shivthedev

    7 ай бұрын

    Already bought it and enjoying it 😀

  • @vinny142
    @vinny1423 жыл бұрын

    9:08 "5080" A good example of how difficult it is to get good benchmarks. ID=5000 was fetched, but why was fetching 5080 so fast while fetching 7080 was slow again? Because PostgreSQL stores rows in pages, which are 8KB blocks of diskspace. Hussain made rows of 2 integer and three(?) characters so one 8KB block can hold about 1000 rows. When the database fetched the page that 5000 was in, that page was cached by the operating system (not the database) had inadvertently instantly cached 1000 rows around id=5000. 14:04 "The primary key is usually stored with every single index" I have never heard of that behavior. Primary keys are always indexed but as far as I am aware they are never automatically added to every index you create. ht index contains tuple and row information to enable a lookup but not the PK. I get the feeling you're seeing the ID rather quicky because the pages were already in cache from the previous queries. And about the LIKe not using an index; that's a good topic for a separate video: Trigram indexes.

  • @hnasr

    @hnasr

    3 жыл бұрын

    Thanks Vinny this is very valuable! and yes me pulling id 5000 and then 5080 came also quick because the OS cached the page.. Neat how databases work Yeah InnoDB I believe works this way stores the primary key along side every index you create on other columns

  • @vinny142

    @vinny142

    3 жыл бұрын

    @@hnasr It seems you are correct about InnoDB adding the PK to the index... wow...weird design choice. But then my opinion of MySQL has never been very high :-)

  • @rafatbiin

    @rafatbiin

    3 жыл бұрын

    I was about to comment on the same thing explaining why the query with id = 5080 was faster than the one with id = 7080.

  • @rafatbiin

    @rafatbiin

    3 жыл бұрын

    also about the page size, I think it's not fixed in every system. for example I just checked on my mac($getconf PAGESIZE), it's 4KB.

  • @neotodsoltani5902

    @neotodsoltani5902

    2 жыл бұрын

    What do you mean by page here? also, if the primary key (or more general, a reference to the record) is not stored with the indexed row, for exmaple name column here (which I assume that is stored in another database in a b-tree structure), how do database find the actuall record when I say "Select * from table where name='name';" ? tnx

  • @oscarbarajas3610
    @oscarbarajas36103 жыл бұрын

    I was just wasting time in youtube and suddenly your video pop up to my screen. Good information summarized. Thanks man! Take my like and keep uploading videos!!!

  • @hnasr

    @hnasr

    3 жыл бұрын

    Thank you 😊 glad you enjoyed the content !

  • @daveschuster1890
    @daveschuster18903 жыл бұрын

    Man, I absolutely love your attitude and style of teaching. Deeply grateful for your content... thank you sir!

  • @juliusgrisette
    @juliusgrisette3 жыл бұрын

    Thank you so much! This was clear, concise and very helpful! More postgres tutorials plz!

  • @hnasr
    @hnasr3 жыл бұрын

    Checkout my Introduction to Database Engineering Udemy course database.husseinnasser.com Chapters intro 0:00 What is an index 0:30 Describe Table 2:15 SELECT [ID] WHERE ID 4:00 SELECT [NAME] WHERE ID 6:30 SELECT [ID] WHERE [NAME] (No index) 9:20 SELECT [ID] WHERE NAME LIKE 11:12 CREATE INDEX ON [NAME] 12:00 SELECT [ID] WHERE NAME (Indexed) 12:50 SELECT [1D] WHERE NAME LIKE (Indexed) 14:30 Summary 16:00

  • @KangJangkrik

    @KangJangkrik

    3 жыл бұрын

    Wow... how did you put that character to comment section?

  • @hnasr

    @hnasr

    3 жыл бұрын

    Athaariq Ardiansyah thats custom emoji that I built, members of the channel can use it 😍

  • @KangJangkrik

    @KangJangkrik

    3 жыл бұрын

    @@hnasr We can program our own emoji? That blows my mind :o

  • @jorgejarai
    @jorgejarai3 жыл бұрын

    You're orders of magnitude better than the TA for my DB course! Thank you very much for the explanation 😊

  • @slahomar1497
    @slahomar14972 жыл бұрын

    I have learned in this video more than I learned in a complete university semester الله يحفظك ❣

  • @tmanley1985
    @tmanley19853 жыл бұрын

    Me: Man I'm really interested in (insert subject here). I wonder if there's a video on this. *Hussein has entered the chat*

  • @sagartyagi2450
    @sagartyagi24503 жыл бұрын

    Started today, My 15th video in a row. Thanks a lot man, getting all this knowledge for free is a blessing for us.

  • @hnasr

    @hnasr

    3 жыл бұрын

    nice! thanks for commenting and take some rest and pick up some other time :) all the best

  • @hannahle3533
    @hannahle35333 жыл бұрын

    Love this video. Really fascinating learning about the intuition behind indexes!

  • @fernandoabreu1305
    @fernandoabreu13053 жыл бұрын

    Great video! You talked about using a multicolumn index as a way to save time to not have to go to disk. It would be interesting to have a video showing the tradeoffs of this approach. It seems Postgres do not recommend using a multicolumn index, only when really necessary.

  • @SiimKoger
    @SiimKoger9 ай бұрын

    Best introduction video on indexing I've seen. I feel like I finally understood the concept. Thabks. The "like" expression was a great addition.

  • @mala3b56
    @mala3b5628 күн бұрын

    man your videos are great. simple to exactly to the point.

  • @atakancolak8411
    @atakancolak84113 жыл бұрын

    just finished my first proper postgresql view that takes about 10 seconds and i see hussain nasser upload this video, coincidence? i think not...

  • @pial2461
    @pial24613 жыл бұрын

    wow, exactly what i am looking for! awesome, Hussein.

  • @sujoykarmaker8467
    @sujoykarmaker8467 Жыл бұрын

    You are a gift from God for us backend developers.

  • @owaistnt
    @owaistnt3 жыл бұрын

    You explaining skills are just excellent.

  • @diogoemon
    @diogoemon4 ай бұрын

    thank you for this! I love these videos that actually show how theory works in a concrete example!

  • @awksedgreep
    @awksedgreep3 жыл бұрын

    Good stuff. I'm a DBA of about 20 years. I remember using PostgreSQL before the SQL interface was added. :D Anywho, you mentioned the primary key being stored with the data. It's actually the opposite. The data is stored or "clustered" with the primary key. It's the only index that exists with the data. All others are index lookups that reference the location of the data. Great explanation.

  • @hnasr

    @hnasr

    3 жыл бұрын

    didn't know postgres primary index is clustered! thanks

  • @abdusamadabdullakhanov518
    @abdusamadabdullakhanov518 Жыл бұрын

    in this quick video I jumped into Database Flow world!!! really appreciate your work, bro

  • @TheNubaHS
    @TheNubaHS2 жыл бұрын

    This was SO GOOD, congratulations, highest quality class

  • @samirmishra9946
    @samirmishra99466 ай бұрын

    Love you my guy, most valuable tech content creator in youtube.

  • @tamles937
    @tamles9372 жыл бұрын

    Really good video, well explained and just the right degree of details that I was looking for!

  • @burakuren5188
    @burakuren51882 жыл бұрын

    Actually normally I would never say something about the accent that anyone has, but wow bro your accent is perfect! Thank you for creating so much valuable content on youtube like this and please keep doing it!

  • @pedrofernandez9506
    @pedrofernandez95063 жыл бұрын

    Thank you for the video it was really helpful to watch. I am working with databases and since you already spoke about acid, indexing and pooling another topic I'd be very interested in is views. How and when they are computed their benefits over regular queries and also materialized views which I think is a great Postgre feature.

  • @sanjaykumar007
    @sanjaykumar0073 жыл бұрын

    perfect explanation! thanks Hussein...

  • @timbui5556
    @timbui55563 жыл бұрын

    You are an amazing teacher. Thank you!

  • @smoothbeak
    @smoothbeak3 жыл бұрын

    This was extremely helpful. One of those topics that is never really explained in detail!

  • @ashutoshmishra2328
    @ashutoshmishra23283 жыл бұрын

    Thank you Hussein for this video, was waiting for this one.

  • @hnasr

    @hnasr

    3 жыл бұрын

    My pleasure

  • @sachinmaurya3259
    @sachinmaurya32592 жыл бұрын

    Thank you so much! very helpful :) this channel deserve Million of subscriber

  • @lailabouziani7704
    @lailabouziani77042 жыл бұрын

    Amazing video, thanx Hussein

  • @esantix
    @esantix Жыл бұрын

    Thanks for your time! Great intro to indexes

  • @megazord5696
    @megazord56963 жыл бұрын

    Thanks Hussein! Amazing content!

  • @nausheenkhan5896
    @nausheenkhan58962 жыл бұрын

    Amazing video, glad to watch this and understand at first glance. I am surely gonna watch more videos to enhance my technical skill.

  • @sokhomsovichea2329
    @sokhomsovichea2329 Жыл бұрын

    Thanks you so much for such a details explanation

  • @md.mohiuddin
    @md.mohiuddin Жыл бұрын

    Thanks. It is a really amazing lesson I have learnt from this discussion.

  • @souravpurohit3348
    @souravpurohit3348 Жыл бұрын

    Nasser, Your videos are really informative and it helps me picture the topic . More power to you , god bless

  • @hnasr

    @hnasr

    Жыл бұрын

    appreciate you dear, thanks for your comment!

  • @user-ce7vu3ct3y
    @user-ce7vu3ct3y8 ай бұрын

    The best channel on youtube!

  • @paulonetto1699
    @paulonetto16993 жыл бұрын

    Thank you so much, Hussein! This explanation is so incredible! After all, i'm asking you if you can explain where index is bad to database (sparse tables, how much it can cost to database size, etc), this will be good at all, and i will be gratefull!

  • @benstemen469
    @benstemen469 Жыл бұрын

    this was a nice demonstration of indexing, thanks!

  • @IvanRandomDude
    @IvanRandomDude3 жыл бұрын

    Great video and teaching skills. Inspired me to buy your udemy course on databases. Can't wait to learn more.

  • @germanreynaga7256
    @germanreynaga72562 жыл бұрын

    Really great job bro, thanks for all this information.

  • @rossthemusicandguitarteacher
    @rossthemusicandguitarteacher3 жыл бұрын

    Excellent video, thanks!

  • @vanshjagyasi-iiitk4111
    @vanshjagyasi-iiitk41113 жыл бұрын

    That was a really good explanation!

  • @geoafrikana
    @geoafrikana10 ай бұрын

    Great explanation. Thanks

  • @codeq9252
    @codeq92525 ай бұрын

    It really helped, Thank you.

  • @virendrabhati6685
    @virendrabhati66853 жыл бұрын

    Thanks for indexing make simple for a layman.... It's complicated but this video made it baby job

  • @chiubaca
    @chiubaca3 жыл бұрын

    So interesting! Could u do one explaining how spatial indexes work on geographic data?

  • @gauravramrakhyani7593
    @gauravramrakhyani75933 жыл бұрын

    Very helpful. Thank You. Subscribed to the channel.

  • @johnnygp9397
    @johnnygp93973 жыл бұрын

    Well done! Congrats 👏

  • @user-ky2fw9kw7m
    @user-ky2fw9kw7m2 жыл бұрын

    Just perfect! Thank ya!

  • @alohaal7125
    @alohaal71253 жыл бұрын

    Great video! Helped a lot.

  • @ediarnoldo
    @ediarnoldo2 жыл бұрын

    Very nice video! Thanks

  • @franciscandelaria3535
    @franciscandelaria35359 ай бұрын

    Thank you for this informative video❤. You're worth subscribing❤

  • @maxi0361
    @maxi03613 жыл бұрын

    Waiting for the next indexing video.

  • @teodorhfb
    @teodorhfb Жыл бұрын

    Awesome explanation

  • @jonathanhill7829
    @jonathanhill78293 жыл бұрын

    Really good video, excellent explanation

  • @adnannoaman8431
    @adnannoaman84312 жыл бұрын

    clearly explained thank you man

  • @sihatafnan5450
    @sihatafnan54503 жыл бұрын

    Thanks a lot Hussein. Great explanation

  • @hnasr

    @hnasr

    3 жыл бұрын

    Thanks Sihat!

  • @02Josem02
    @02Josem023 жыл бұрын

    It's a great and very helpful video on indexing in general. Please may I get a link if you have uploaded a video on multicolumn indexing? How is done specifically in PostgreSQL? Thank you

  • @RahulGupta-ss8bq
    @RahulGupta-ss8bqАй бұрын

    Hi, As far as I know, PostgreSQL includes the primary key along with the secondary index. Now, I have a table - tbl_questions that has: 1. id - primary key 2. question_set_id - secondary index I am using the query: EXPLAIN ANALYZE SELECT * FROM tbl_questions WHERE question_set_id = 3 AND id > 50 LIMIT 10; This query is doing an Index scan on question_set_id and then filtering out records where id > 50 Here's the output: Limit (cost=0.14..7.89 rows=1 width=582) (actual time=0.009..0.009 rows=0 loops=1) -> Index Scan using tbl_questions_question_set_id_idx on tbl_questions (cost=0.14..7.89 rows=1 width=582) (actual time=0.008..0.008 rows=0 loops=1) Index Cond: (question_set_id = 3) Filter: (id > 50) Planning Time: 0.073 ms Execution Time: 0.021 ms (6 rows) My question is, if the id is stored along with question_set_id, then why is the condition not like Index Cond: (question_set_id = 3) AND (id > 50) I have tried switching the position for id and question_set_id in the query but still the same result. However, when I created a composite index like below, it was working as expected: 1. id - primary key 2. question_set_id, id- secondary index Here's the query and output: EXPLAIN ANALYZE SELECT * FROM tbl_questions WHERE question_set_id = 5 AND id > 10 LIMIT 10; Index Scan using tbl_questions_question_set_id_idx on tbl_questions (cost=0.14..8.01 rows=1 width=582) (actual time=0.009..0.009 rows=0 loops=1) Index Cond: ((question_set_id = 5) AND (id > 10)) Planning Time: 0.074 ms Execution Time: 0.021 ms (5 rows) It will be very helpful if you can clear this out or let me know if I am doing anything wrong. Thanks

  • @AjaySharma-vz9ei
    @AjaySharma-vz9ei Жыл бұрын

    This was simply wow 👏🏻

  • @BearVodkaAndValenki
    @BearVodkaAndValenki2 жыл бұрын

    Thank's man, very useful info)

  • @camila8ym
    @camila8ym11 ай бұрын

    Great content!!! Thanks!

  • @tstudying5319
    @tstudying53193 жыл бұрын

    thanks!! you helped me so much!

  • @hnasr

    @hnasr

    3 жыл бұрын

    Glad I could help!

  • @rafatmunshi3572
    @rafatmunshi35723 жыл бұрын

    Thanks for this video. Please also make a video on everything about SQL Query optimisation. I really need it soon, thanks!

  • @hnasr

    @hnasr

    3 жыл бұрын

    Sure thing!

  • @wallychampblog
    @wallychampblog2 жыл бұрын

    Really great video. Wasnt on nonclustered vs clustered but really great explanation nevertheless

  • @mmu200
    @mmu2003 жыл бұрын

    When you have multiple colums in where clause and sometime index will not hit. Looking forwad for a 2nd part of this video explaining best practices when there are multiple columns in where clause then what kind of and order of index should be made. Separate index on each column or combined index and how this will impact on write time? Also if we write like query as 'Zs%' will index hit?

  • @abzzz4u
    @abzzz4u3 жыл бұрын

    Quite explanatory 👍🏻

  • @joumaamohamad797
    @joumaamohamad7979 ай бұрын

    Thank you for clarifying concept database indexing. I appreciate your insights, but I'm still a bit puzzled about a few things. While I understand that traditional relational databases have robust implementations of indexing, I'm curious to know why one might opt for search engines, like Elasticsearch, over them. Specifically, how does Elasticsearch indexing differ from that of relational databases? Moreover, are there specific challenges or limitations associated with relational database indexing that Elasticsearch indexing can address more effectively? I'd greatly appreciate any further insights you can provide on this topic. Thank you in advance for your time and assistance.

  • @krunalshrimali4471
    @krunalshrimali447111 ай бұрын

    Which one is better, having different indexes for different columns or having one index containing multiple columns and can you give examples of in which case which option to go for?

  • @paragtyagi3713
    @paragtyagi37132 жыл бұрын

    Made my day !

  • @sanskarkaazi3830
    @sanskarkaazi3830 Жыл бұрын

    What is the best way to use instead of Like query if some similar query needs to be implemented. Use fullTextSearch or elasticSearch or any other things or is there a way?

  • @rajataggarwal4780
    @rajataggarwal47802 жыл бұрын

    Please create a video on sql joins time complexity analysis, as you have done for index scan, index only and table scan

  • @anthonyfarias321
    @anthonyfarias3213 жыл бұрын

    Hello Hussein! Great video as always. This left me with some questions. How are we supposed to implement a search functionality if "like" is not a good idea? Should we create as much indexes as possible? or should we create indexes on most used fields? Thanks again.

  • @hnasr

    @hnasr

    3 жыл бұрын

    Great question! This is something I didn’t touch upon on the video you can actually create an index based on the LIKE predict. Some databases also support full text search capabilities in an efficient manner. And finally there are databases specialized in text based search

  • @TsunaSawada26

    @TsunaSawada26

    3 жыл бұрын

    Would you be able to make a video regarding that?

  • @vinny142

    @vinny142

    3 жыл бұрын

    ". How are we supposed to implement a search functionality if "like" is not a good idea?" There is nothing inherently wrong with LIKE. Hussain's exaplne uses a BTREE index and that index type cannot search for wildcards at the beginning. Other index types such as Trigram indexes can do that. Fulltext mostly won't help if you are really looking for substrins because they generally don't implement that.Searching is a whole different subject,but genereally speaking PostgreSQL's fulltext with Trigram indexes and a little bit for manual labour is more than sufficient. No need to jump to Lucene and the like unless you are doing very spcific work or at a large scale.

  • @arielpapuga8908
    @arielpapuga89082 жыл бұрын

    At 12:58 you quickly run through the result of this 'explain' query but since we just created index on name column then what is this 'Bitmap heap scan' and 'Bitmap index scan'? Why isn't it 'Index only scan'? Could you please elaborate on this?

  • @luqmansen
    @luqmansen3 жыл бұрын

    super cool, thanks for this

  • @sanjaybhatikar
    @sanjaybhatikar2 жыл бұрын

    Love it, thank you :)

  • @gitlit5489
    @gitlit54892 жыл бұрын

    so how can we make matching expressions fast ?, and will a document-oriented database face the same problem and search through all documents in a collection to match maybe a name or will it be faster ? (i mean do we have an option to optimize queries like this ?)

  • @iamsatyajit963
    @iamsatyajit9633 жыл бұрын

    This is excellent...

  • @giovanni8939
    @giovanni893911 ай бұрын

    Would be nice to know if the index helps in case the expression is "like 'Za%'". Intuitively it should be able to the rows starting with Za and take advantage of the index, what do you think?

  • @ampzamp
    @ampzamp2 жыл бұрын

    thats a bloody good video mate. Whats the ram speed youre running there?

  • @davidlin8589
    @davidlin85893 жыл бұрын

    Does the execution time include the CPU time and the release memory time?

  • @maamounhajnajeeb209
    @maamounhajnajeeb2097 ай бұрын

    I'd watched this video 6 months ago and understand nothing, now I watch this video and I am working on some kind of project that needs indexing Finally I Understand Thank Hussien But, wait a little bit, how do you design your youtube thumbnail

  • @carterv.31
    @carterv.313 жыл бұрын

    Very nice video

  • @mohabkhaled1391
    @mohabkhaled13913 жыл бұрын

    Thanks, that was really clear intro, can I have a suggestion, I know this is not suppose to be a formal education channel and you are reflecting on different topics of backending it's really dull but can you have serieses of same topics in one place, even if you just touched the topic, it will be more beneficial if we could know the different aspects related to a topic.. Again thanks for the intro

  • @peijunwu7354
    @peijunwu73542 жыл бұрын

    @Hussein Nasser Can you also discuss multi-column index? Thanks!

  • @Sawa_i
    @Sawa_i2 ай бұрын

    Beautiful.

  • @mustafa60x
    @mustafa60x9 ай бұрын

    great video

  • @bhuman6465
    @bhuman64653 жыл бұрын

    Clearly explained, your voice resembles Harsha Bhogle in 1.5x speed.

  • @eunlimlee582
    @eunlimlee5823 жыл бұрын

    Thank you so much !💕

  • @serhiihorun6298
    @serhiihorun62983 жыл бұрын

    Thank you man!

  • @raj_kundalia
    @raj_kundalia7 ай бұрын

    thank you!

  • @umongsain738
    @umongsain7383 жыл бұрын

    Thanks :)

  • @opelfahrer91
    @opelfahrer913 жыл бұрын

    Great video, thank you! 12:00 wouldn't it be much slower if you searched for something else like '%wr%' because 'zs' results have been cached as a result from the query you ran before the LIKE-query? I mean, the "= 'zs'" query took about 3.2 seconds, the LIKE + wildcard query only 1.x seconds?

  • @michaelchung8102

    @michaelchung8102

    2 жыл бұрын

    Probably because of caching. EXPLAIN ANALYZE will run the SELECT query and tell what is going on.

  • @ajinkya-wasnik
    @ajinkya-wasnik6 ай бұрын

    **Highlights**: + [00:00:00] **Introduction to database indexing** * What is an index and why it is useful * How indexes are built and stored * Examples of index types: B-tree and LSM tree + [00:03:00] **Querying with and without indexes** * How to use explain analyze to measure query performance * How to compare the execution time and cost of different queries * How to avoid full table scans and use index scans instead + [00:12:08] **Creating an index on a column** * How to create a B-tree index on a name column * How to use the index to speed up queries on the name column * How to avoid going to the heap and use inline queries + [00:16:30] **Querying with expressions and wildcards** * How expressions and wildcards prevent the use of indexes * How to avoid using like with percentage signs * How to use hints to force the use of indexes

  • @rodrigomarcondes2000
    @rodrigomarcondes20003 жыл бұрын

    great video!

  • @IBITZEE
    @IBITZEE2 жыл бұрын

    good info... thanks... very interested in how: - a dattabase decide in what page save a record - how variable size rows are stored (a page can have 1 row... or 10 rows) - what if a record is bigger than the page size? keep up----

  • @nextgodlevel4056
    @nextgodlevel40562 жыл бұрын

    can I get the 11 million rows database link? From where I can find sample database for learning because I don't want to waste my time for adding huge amount of data manually . I know I can use script but still I want some sample databases for testing, exploring and learning.

Келесі