<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Navin Mittal on Medium]]></title>
        <description><![CDATA[Stories by Navin Mittal on Medium]]></description>
        <link>https://medium.com/@navinmittal29?source=rss-165c55cc4942------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dmbNkD5D-u45r44go_cf0g.png</url>
            <title>Stories by Navin Mittal on Medium</title>
            <link>https://medium.com/@navinmittal29?source=rss-165c55cc4942------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 09:53:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@navinmittal29/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Indexing strategies in databases]]></title>
            <link>https://medium.com/@navinmittal29/indexing-strategies-in-databases-7e3f27803407?source=rss-165c55cc4942------2</link>
            <guid isPermaLink="false">https://medium.com/p/7e3f27803407</guid>
            <category><![CDATA[b-tree]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[indexing]]></category>
            <category><![CDATA[sstable]]></category>
            <dc:creator><![CDATA[Navin Mittal]]></dc:creator>
            <pubDate>Mon, 21 Mar 2022 02:17:23 GMT</pubDate>
            <atom:updated>2022-03-21T02:39:12.575Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fr7exrKuTotGsd1oxX9ddw.jpeg" /></figure><p>When asked about indexes, I could immediately tell what a clustered and non-clustered index is. But if asked how it’s implemented, I am sure I would embarrass myself. This weekend, I went through “Designing Data-Intensive Applications” and I can promise every line and every page is worth underlining. Since this is my first post, I will focus only on summarizing key learnings. In future, I might add some images and examples. Hopefully this article adds value to anyone learning about databases.</p><p>There are <strong>2 types of users</strong> of a database:<br>1. One who manipulates data a lot and queries only small chunk of data. This user is an application engineer and this is OLTP.<br>2. One who doesn’t manipulate data but runs queries on billions or at least millions of rows to derive insights from data. This user is a data analyst and this is OLAP.</p><p>To get data from a small size database is simple and fast. But to fetch data from large database is very slow process. You can say O(n) time complexity.</p><p>To make fetch faster we need to store an additional data structure in our database called index. Index doesn’t change the contents of database, it helps in making lookup faster.</p><p><strong>Techniques to implement indexes:</strong><br>1. Simple log structured index<br>2. Sorted String Tables (SSTables) and LSM-Tree<br>3. B-Trees</p><p><strong>Let’s see each one bit more in detail</strong><br><em>1</em>. <em>Simple log structured index</em></p><p>Storing data:<br>- Here we append new <em>key-value</em> writes to database at the end of a fixed size file called segment.<br>- To identify the latest value for a given key, maintain a hashmap for every segment in the memory. Hashmap maintains <em>key-diskOffset</em> stored for data retrieval in the current active segment.<br>- If the segment is of particular size, make this file immutable. Now create new segment and a new hashmap.<br>- If the data is meant to be deleted, add a tombstone against the key in the log file.<br>- While a new write is being appended, run another thread to compact and merge old segments to keep only the latest <em>key-value</em> pair thus freeing up disk space and avoiding disk fragmentation. New hashmap is also created. Until this merging is complete, old segments aren’t destroyed.<br>- During compaction and merging process, 2 new hashmap are created that maintains the latest value for a given key. Then new, compacted segment file is generated that stores the latest value for each key.</p><p>Accessing data:<br>- First the search is performed on latest segment’s hashmap and if not found then it goes backwards in old compacted segments.</p><p><em>2. SSTables</em></p><p>Storing data:<br>- In previous strategy we did simple append at the end of the file to store data. But in SSTables we store the records in sorted order.<br>- To maintain sorted order AVL tree or red-black tree is constructed.<br>- As soon as the tree size reaches a certain threshold, it’s written on the disk and new tree is constructed for new incoming writes.<br>- In this strategy as well we build hashmap but now the hashmap is small in size since not all <em>key-diskOffset</em> need to be stored.<br>- Segmentation, compaction and merging are performed on segments to avoid disk fragmentation.<br>- Here compaction and merging are performed using the same strategy as mergesort.<br>- Since new SSTables and segments are created during compaction and merging process, the disk block on SSD storage gets overwritten multiple times and overtime wears out. This is an area of optimization in this strategy.</p><p>Accessing data:<br>- Since the keys are sorted, binary search can be performed on the segment. Lookup the 2 keys between which search key should lie. If the search key exists, it will be found. If not, then perform the same search operation on old segments in reverse.</p><p><em>3. B-Trees</em></p><p>Storing data:<br>- B-Trees is most popular strategy in many databases. But log structured strategy is gaining more acceptance due to its writing speed: O(1).<br>- In B-Trees the key-values are stored in sorted order like we saw in the previous strategy.<br>- Instead of segments (like in SSTables), data is stored in fixed size pages or blocks on disk.<br>- Instead of writing new value to a new segment, value is updated in the same page in B-Trees thus avoiding duplicates.<br>- Just like in binary search trees, some pages are meant to store only references to other pages and only leaf pages are meant to store value for the given key.<br>- But in B-Trees, instead of 2 children, branching factor can be hundreds or even thousands. Suppose page size is 4 KB and branching factor is 500, then across 4 levels of this trees 256 TB of <em>key-value</em> pairs can be stored.<br>- If new key-value are to be inserted, pages are split into 2 full size pages and references are updated in the parent page. This makes B-Trees inefficient with writes. This split also creates lot of empty space in the page thus wasting disk memory.</p><p>Accessing data:<br>- Search time: O(log n). If key exists, it’s found pretty quick. Latency is only due to the disk seek to reach the appropriate block.</p><p>Feel free to ask clarifying or thought provoking questions that can add more value to this article.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7e3f27803407" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>