Database Indexing Explained (with PostgreSQL)

  Рет қаралды 309,366

Hussein Nasser

Hussein Nasser

Күн бұрын

Пікірлер: 241
@hnasr
@hnasr 2 жыл бұрын
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 10 ай бұрын
Already bought it and enjoying it 😀
@vinny142
@vinny142 4 жыл бұрын
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 4 жыл бұрын
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 4 жыл бұрын
@@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 3 жыл бұрын
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
@tmanley1985
@tmanley1985 4 жыл бұрын
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
@sagartyagi2450 3 жыл бұрын
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
@juliusgrisette
@juliusgrisette 4 жыл бұрын
Thank you so much! This was clear, concise and very helpful! More postgres tutorials plz!
@slahomar1497
@slahomar1497 2 жыл бұрын
I have learned in this video more than I learned in a complete university semester الله يحفظك ❣
@sujoykarmaker8467
@sujoykarmaker8467 Жыл бұрын
You are a gift from God for us backend developers.
@jorgejarai
@jorgejarai 3 жыл бұрын
You're orders of magnitude better than the TA for my DB course! Thank you very much for the explanation 😊
@erfanakhavanrad6597
@erfanakhavanrad6597 Ай бұрын
Informative with crystal clear explanation. Thank you.
@fernandoabreu1305
@fernandoabreu1305 3 жыл бұрын
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.
@oscarbarajas3610
@oscarbarajas3610 4 жыл бұрын
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 4 жыл бұрын
Thank you 😊 glad you enjoyed the content !
@samirmishra9946
@samirmishra9946 9 ай бұрын
Love you my guy, most valuable tech content creator in youtube.
@RahulGupta-ss8bq
@RahulGupta-ss8bq 5 ай бұрын
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
@harshagarwal3855
@harshagarwal3855 2 ай бұрын
That's because only InnoDB engine adds the primary key to every index. I think you are not using InnoDB
@abdusamadabdullakhanov518
@abdusamadabdullakhanov518 2 жыл бұрын
in this quick video I jumped into Database Flow world!!! really appreciate your work, bro
@pial2461
@pial2461 4 жыл бұрын
wow, exactly what i am looking for! awesome, Hussein.
@hannahle3533
@hannahle3533 3 жыл бұрын
Love this video. Really fascinating learning about the intuition behind indexes!
@maamounhajnajeeb209
@maamounhajnajeeb209 10 ай бұрын
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
@rajveersingh2056
@rajveersingh2056 Жыл бұрын
Like %za% is slow, however I feel, Like za% would benefit from index
@virendrabhati6685
@virendrabhati6685 3 жыл бұрын
Thanks for indexing make simple for a layman.... It's complicated but this video made it baby job
@sachinmaurya3259
@sachinmaurya3259 3 жыл бұрын
Thank you so much! very helpful :) this channel deserve Million of subscriber
@pedrofernandez9506
@pedrofernandez9506 3 жыл бұрын
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.
@maxi0361
@maxi0361 4 жыл бұрын
Waiting for the next indexing video.
@paulonetto1699
@paulonetto1699 3 жыл бұрын
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!
@mmu200
@mmu200 3 жыл бұрын
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?
@CodeFromSoul
@CodeFromSoul 4 жыл бұрын
perfect explanation! thanks Hussein...
@ajinkya-wasnik
@ajinkya-wasnik 9 ай бұрын
**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
@anthonyfarias321
@anthonyfarias321 4 жыл бұрын
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 4 жыл бұрын
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 4 жыл бұрын
Would you be able to make a video regarding that?
@vinny142
@vinny142 4 жыл бұрын
". 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.
@HarshVerma-k9z
@HarshVerma-k9z 11 ай бұрын
The best channel on youtube!
@xdlovas
@xdlovas 4 ай бұрын
One more important thing, if you change rows a lot or add rows etc…, index is a No go.
@rafatmunshi3572
@rafatmunshi3572 4 жыл бұрын
Thanks for this video. Please also make a video on everything about SQL Query optimisation. I really need it soon, thanks!
@hnasr
@hnasr 4 жыл бұрын
Sure thing!
@md.mohiuddin
@md.mohiuddin Жыл бұрын
Thanks. It is a really amazing lesson I have learnt from this discussion.
@benstemen469
@benstemen469 Жыл бұрын
this was a nice demonstration of indexing, thanks!
@rajataggarwal4780
@rajataggarwal4780 2 жыл бұрын
Please create a video on sql joins time complexity analysis, as you have done for index scan, index only and table scan
@bhuman6465
@bhuman6465 3 жыл бұрын
Clearly explained, your voice resembles Harsha Bhogle in 1.5x speed.
@gauravramrakhyani7593
@gauravramrakhyani7593 3 жыл бұрын
Very helpful. Thank You. Subscribed to the channel.
@tamles937
@tamles937 2 жыл бұрын
Really good video, well explained and just the right degree of details that I was looking for!
@esantix
@esantix 2 жыл бұрын
Thanks for your time! Great intro to indexes
@timbui5556
@timbui5556 3 жыл бұрын
You are an amazing teacher. Thank you!
@krunalshrimali4471
@krunalshrimali4471 Жыл бұрын
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?
@umongsain
@umongsain 4 жыл бұрын
Thanks :)
@megazord5696
@megazord5696 4 жыл бұрын
Thanks Hussein! Amazing content!
@ediarnoldo
@ediarnoldo 2 жыл бұрын
Very nice video! Thanks
@franciscandelaria3535
@franciscandelaria3535 Жыл бұрын
Thank you for this informative video❤. You're worth subscribing❤
@IvanRandomDude
@IvanRandomDude 3 жыл бұрын
Great video and teaching skills. Inspired me to buy your udemy course on databases. Can't wait to learn more.
@IBITZEE
@IBITZEE 2 жыл бұрын
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----
@tstudying5319
@tstudying5319 3 жыл бұрын
thanks!! you helped me so much!
@hnasr
@hnasr 3 жыл бұрын
Glad I could help!
@adnannoaman8431
@adnannoaman8431 2 жыл бұрын
clearly explained thank you man
@mohabkhaled1391
@mohabkhaled1391 3 жыл бұрын
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
@chiubaca
@chiubaca 3 жыл бұрын
So interesting! Could u do one explaining how spatial indexes work on geographic data?
@vanshjagyasi-iiitk4111
@vanshjagyasi-iiitk4111 3 жыл бұрын
That was a really good explanation!
@rossthemusicandguitarteacher
@rossthemusicandguitarteacher 3 жыл бұрын
Excellent video, thanks!
@sany2k8
@sany2k8 4 жыл бұрын
You are a star, really appreciate. Just on query sir- - Why slow: select name from employees where id=5000; - Why fast: select id from employees where id=5000; On both cases primary key id is indexed and query scans index using where clause id. Is that because name is not present on the index? Correct me if I am wrong.
@vinny142
@vinny142 4 жыл бұрын
" Is that because name is not present on the index? " Yes. If you select a field that is not present in the index the database must fetch the value for that field from the table (the heap) and that takes time. BUT:if you just add the name to the index then the index becomes as large as the table, which can also have negative consequences.
@sany2k8
@sany2k8 4 жыл бұрын
@@vinny142 thanks sir for the answer
@AjaySharma-vz9ei
@AjaySharma-vz9ei 2 жыл бұрын
This was simply wow 👏🏻
@BearVodkaAndValenki
@BearVodkaAndValenki 2 жыл бұрын
Thank's man, very useful info)
@chandeeparora.7165
@chandeeparora.7165 3 жыл бұрын
That was a great video ! Can you explain how exactly the b-tree looks like when a index is created? I mean, what a b-tree node contains? (Key, value, pointer to row id etc ? )
@hnasr
@hnasr 3 жыл бұрын
Good idea for a video ☝️
@chandeeparora.7165
@chandeeparora.7165 3 жыл бұрын
@@hnasr I would really appreciate if you can do one. I have been trying to understand it from here: use-the-index-luke.com/sql/anatomy. Will wait for the video!
@johnnygp9397
@johnnygp9397 4 жыл бұрын
Well done! Congrats 👏
@camila8ym
@camila8ym Жыл бұрын
Great content!!! Thanks!
@paathshala7686
@paathshala7686 2 жыл бұрын
Why the "like" command took less time than the actual "select" command with no "like"? Was that also cached? @HusseinNasser Just needed some explanation over that !?
@alohaal7125
@alohaal7125 3 жыл бұрын
Great video! Helped a lot.
@joumaamohamad797
@joumaamohamad797 Жыл бұрын
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.
@carterv.31
@carterv.31 3 жыл бұрын
Very nice video
@chrismenui7344
@chrismenui7344 10 ай бұрын
how to performance tune? explain analyze select ...(performance report) table vs index(two different data structures on the disk) slow vs fast index scan no index scan like
@dhritiayusj2710
@dhritiayusj2710 3 жыл бұрын
7:15 For science... Epic
@Hwaism
@Hwaism 4 жыл бұрын
i was waiting "Wake up, ... "
@abzzz4u
@abzzz4u 4 жыл бұрын
Quite explanatory 👍🏻
@p.z.8355
@p.z.8355 3 жыл бұрын
Super engaging!
@hnasr
@hnasr 3 жыл бұрын
Thank you!
@CallumAtwal
@CallumAtwal 3 жыл бұрын
Hey Hussein, I hope this comment finds you. I had a question related to Fulltext Search (for MSSQL but I understand you have a preference for PostGres). I have an issue where if there is a search involving multiple CONTAINS calls, any subsequent search on one of the columns takes a really long time to execute. Oddly enough, after around 10 mins or so, it then becomes rapid. This is on a table with around 71k rows so not massively huge. I wondered if it was something to do with indexes but it would be great to see a video on Fulltext. I can send you the query exactly how I have it
@mohamedmohamedy3085
@mohamedmohamedy3085 2 жыл бұрын
there are two types of inverted indexes you can use for fulltext search: gin, gist each one has pros and cons. for gin: it's good for lookups but not so with inserts, deletes or updates. for gist: it's the opposite (good for updates not lookups) if you use gin on a table that changes often it would be slow as it takes time to build/rebuild the index. whenever you do query while index is building, it won't use the index and will do a full table scan. but once it finishes building the index it will use it. so that could be a reason for your problem. using gin for a table that changes so often which causes the index to rebuild each time do you can't use it until in finishes building.
@CallumAtwal
@CallumAtwal 2 жыл бұрын
@@mohamedmohamedy3085 interesting, was not aware of gin/gist. Will look into that a bit more. I gave up with that search implementation and decided to stick with the current Azure cog search we had on the project 😂
@user-rp9iis1en6h
@user-rp9iis1en6h 4 жыл бұрын
thanks. very nice one. However, could you make another one on table partitioning?
@hnasr
@hnasr 4 жыл бұрын
Yes! I am planning to. Practical video on partitioning
@vinny142
@vinny142 4 жыл бұрын
@@hnasr That's going to be a long one :-)
@krunalshrimali4471
@krunalshrimali4471 Жыл бұрын
How to make queries containing like conditions faster?
@anosurino3830
@anosurino3830 4 жыл бұрын
Thanks for cool video. i have a question. what is the effect if add too much indexing to a table?
@hnasr
@hnasr 4 жыл бұрын
It depends too much indexes on a table can actually slow down writes because of the need to update all those indexes and structures it becomes an overhead. The trick is to index exactly what we need and put the columns we need to get inline index only scans which are the best. Good question
@AltianoGerung
@AltianoGerung 3 жыл бұрын
it is worth noting that mysql is using index on `LIKE %%` query
@aki3774
@aki3774 2 жыл бұрын
Why did '%Zs%' execute so much faster than 'Zs'? Because of cache again?
@Sawa_i
@Sawa_i 6 ай бұрын
Beautiful.
@parasarora5869
@parasarora5869 4 жыл бұрын
so index don't work all the time 😐 ... sad ...but nice video !! 👍👍
@02Josem02
@02Josem02 3 жыл бұрын
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
@sanjaybhatikar
@sanjaybhatikar 2 жыл бұрын
Love it, thank you :)
@ampzamp
@ampzamp 3 жыл бұрын
thats a bloody good video mate. Whats the ram speed youre running there?
@speedytort7384
@speedytort7384 3 ай бұрын
Video starts at 2:30
@peijunwu7354
@peijunwu7354 3 жыл бұрын
@Hussein Nasser Can you also discuss multi-column index? Thanks!
@luqmansen
@luqmansen 4 жыл бұрын
super cool, thanks for this
@raj_kundalia
@raj_kundalia 10 ай бұрын
thank you!
@rodrigomarcondes2000
@rodrigomarcondes2000 3 жыл бұрын
great video!
@nextgodlevel
@nextgodlevel 2 жыл бұрын
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.
@abdullahalosaimi2570
@abdullahalosaimi2570 4 жыл бұрын
The worst-case scenario is O(n), and it has to scan everything, but I think if the requested record was at the beginning of the table, it should be faster. Is this how it works?
@hnasr
@hnasr 4 жыл бұрын
Correct assuming full table scan and if we are lucky the row was at the beginning. I am not sure however that all databases scan top to bottom though. That is why database sometimes prefer seq scan over index scan id it knows it could find the row faster with a seq sequence or if the index scan is gonna be slower because of scattered nature
@eduerondu9845
@eduerondu9845 3 жыл бұрын
Great video. What would be the best way to deal with more expressive queries like the example you ran into at the end with 'LIKE'. Perhaps you have a index key that encodes multiple data points, and you want to run regex across each index key to quickly get back a set of id's that satisfy your search. Is there a better method for approaching this type of requirement?
@hnasr
@hnasr 3 жыл бұрын
Thanks! You can create an index in postgres with gin extension that allows for expressive queries such as LIKE niallburkley.com/blog/index-columns-for-like-in-postgres/
@eduerondu9845
@eduerondu9845 3 жыл бұрын
@@hnasr Thanks for the info. Great stuff!
@serhiihorun6298
@serhiihorun6298 3 жыл бұрын
Thank you man!
@shadab8798
@shadab8798 2 жыл бұрын
At 7:08 I think it has a use case let's say I want to have some kind of validation before inserting data for an employee I can just select an id and if I get a response that means this id is a valid id and then I can proceed further. Or this approach has some issues and I should select some other column to be sure that this exists?
@criscrix3
@criscrix3 Жыл бұрын
I'm 8 months too late but for anyone else wondering this in the future this a perfectly valid use case. I've seen it in use quite a few times in production at major IT companies though if I remember corectly you can also just "SELECT 1 FROM table WHERE ..." and it will just return 1 for every row found.
@marijatosic217
@marijatosic217 Жыл бұрын
How to create indexing example on 12:20 min.
@eunlimlee582
@eunlimlee582 3 жыл бұрын
Thank you so much !💕
@fintech1378
@fintech1378 Жыл бұрын
awesome
@hazemabdelalim5189
@hazemabdelalim5189 4 жыл бұрын
How does indexing work in Non-Relational databases !?
@codehash1549
@codehash1549 3 жыл бұрын
Is Indexing random sha256 key a good idea for millions of records ?
@tmanley1985
@tmanley1985 4 жыл бұрын
One more thing, you can create indexes for partial search queries in postgres by using trigram indexes. I don't know exactly how it works, but I believe it creates indexes for subsets of partial searches. I learned about that here: niallburkley.com/blog/index-columns-for-like-in-postgres/
@hnasr
@hnasr 4 жыл бұрын
Correct, you can create an index on partial string so that queries that use like will hit the index. while that will speed your query it will increase the size of the index dramatically.
@tmanley1985
@tmanley1985 4 жыл бұрын
@@hnasr Yes, I was actually wondering about the potential downsides of this. This would likely slow down inserts as well correct? I mean any index will likely do that because the index has to be rebuilt but with something as big as a partial search index, I have to think it would be even greater.
@vinny142
@vinny142 4 жыл бұрын
@@tmanley1985 Yes it will slow it down, but then again, insert-speed is only relevant if you have a situation where you have lots of inserts happening all the time. For web applications the ratio of insert to select is about 1:10000 so it really doesn't matter much is an insert takes a few milliseconds longer if the bulk of the workbecomes ten times faster.
@tmanley1985
@tmanley1985 4 жыл бұрын
@@vinny142 Yep, that's the conclusion I came to as well. The benefits far outweigh the detriments if you're doing any kind of partial search queries in reporting or anywhere else. Thanks for the reply!
@rabbyhossain6150
@rabbyhossain6150 3 жыл бұрын
Is there any way to optimize like query?
@xmilad5214
@xmilad5214 3 ай бұрын
great
@DirePantsDim
@DirePantsDim 3 жыл бұрын
Btree as in Binary Tree data structure?
@TigasFMS
@TigasFMS 4 жыл бұрын
Why not add indexes for every column since it makes (or at least helps) every query to be faster?
@hnasr
@hnasr 4 жыл бұрын
I explained that in other comments. its the cost of maintaining the indexes during inserts can get expensive, and queries that are inefficient can scattershot indexes and decrease performance..
@ddonddon2430
@ddonddon2430 3 жыл бұрын
@Hussein Nasser how does multi column index(c1, c2, c3) works. If I have a query like where c1='xyz' and c3='123' and c2='456' will this query use the index which is created. Also I am getting a result known as row identifier scan in the explain query. Am not sure what it is. Can you please throw some light on these?
@hnasr
@hnasr 3 жыл бұрын
Yes an AND query would always use the composite index I talked about it in this video kzbin.info/www/bejne/o3PSn3uKpbaCbac
@tavi7
@tavi7 3 жыл бұрын
Hey, I know this is not Stackoverflow but I have a question and I thought you could help :) my question is, if I have a table with columns a, b, c, d, e, f and I create a primary key on a, b, c, d and e, is there a way I can create a hash index on those 5 columns? So only 1 hash index that only stores 1 hash for each row with all the 5 columns combined and hashed because I'm always gonna do queries on those 5 columns so I don't need to waste memory for storing 5 times more hashes and it would also be faster right?
@hnasr
@hnasr 3 жыл бұрын
Hard to discuss such problems over YT comments but ill give it a shot Question very broad and depends on your use case. I would first ask is the database uses clustered or non-clustered index (you said primary so Im assuming clustered) if so than generally this doesn’t look like a good idea inserts will be extremely slow assuming those columns are not ordered. I would suggest generating a hash of those column and use that as the primary key again you have the same problem with randomness. I suggest using a database where you can control clustering (postgres use all secondary keys) If you absolutely need to keep using clustered primary key then I suggest creating a new primary field that is serial and create a unique composite index on the 5 columns instead... Also make sure that your query uses “AND” and not “ OR” otherwise the index is not as effective.. Hope that helps
@tavi7
@tavi7 3 жыл бұрын
@@hnasr Yeah it really did help! My question was not the best 😅 Thank you so much! Also keep up the KZbin stuff, I've been watching all your videos
@sundaramjha1776
@sundaramjha1776 4 жыл бұрын
How Btree search store data on String column, btree for primary key number work like less than greater than, but how it works for string?
@hnasr
@hnasr 4 жыл бұрын
a string is just a number so it works similarly but the comparison is a little bit different since there are multiple bytes to compare.
Software Engineering is Overwhelming
34:18
Hussein Nasser
Рет қаралды 30 М.
SQL IN clauses are miles faster in Postgres 17
22:22
Hussein Nasser
Рет қаралды 6 М.
Миллионер | 1 - серия
34:31
Million Show
Рет қаралды 1,7 МЛН
Database Indexing for Dumb Developers
15:59
Laith Academy
Рет қаралды 60 М.
When should you shard your database?
21:20
Hussein Nasser
Рет қаралды 78 М.
SQL Indexes Explained in 20 Minutes
19:31
developedbyed
Рет қаралды 10 М.
They Enabled Postgres Partitioning and their Backend fell apart
31:52
Hussein Nasser
Рет қаралды 41 М.
6 SQL Joins you MUST know! (Animated + Practice)
9:47
Anton Putra
Рет қаралды 162 М.
How do indexes make databases read faster?
23:25
Arpit Bhayani
Рет қаралды 66 М.
SQL Indexes - Definition, Examples, and Tips
12:14
Database Star
Рет қаралды 77 М.