<?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 Emre Köse on Medium]]></title>
        <description><![CDATA[Stories by Emre Köse on Medium]]></description>
        <link>https://medium.com/@emreks?source=rss-a023982638c5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*3JVp92668OOGvgWioR7v2Q.jpeg</url>
            <title>Stories by Emre Köse on Medium</title>
            <link>https://medium.com/@emreks?source=rss-a023982638c5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 18:34:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@emreks/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[Comparing IVFFlat and HNSW in pgvector: Vector Similarity Performance Analysis on Diverse Datasets]]></title>
            <link>https://medium.com/@emreks/comparing-ivfflat-and-hnsw-with-pgvector-performance-analysis-on-diverse-datasets-e1626505bc9a?source=rss-a023982638c5------2</link>
            <guid isPermaLink="false">https://medium.com/p/e1626505bc9a</guid>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[vector-similarity-search]]></category>
            <category><![CDATA[approximatenn]]></category>
            <category><![CDATA[pgvector]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Emre Köse]]></dc:creator>
            <pubDate>Thu, 13 Jun 2024 12:09:57 GMT</pubDate>
            <atom:updated>2024-06-14T08:22:42.132Z</atom:updated>
            <content:encoded><![CDATA[<h4>Exploring the performance of IVFFlat and HNSW algorithms through experiments using PostgreSQL’s pgvector extension</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*IkAxMsIILjoJh6bt" /><figcaption>Photo by <a href="https://unsplash.com/@guerrillabuzz?utm_source=medium&amp;utm_medium=referral">GuerrillaBuzz</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In the rapidly evolving field of approximate nearest neighbor (ANN) search algorithms, selecting the right method for your data can significantly impact performance and efficiency. Vector similarity search, a critical component in various applications such as recommendation systems, image retrieval, and natural language processing, aims to find the closest data points in high-dimensional spaces. This article provides a comparative analysis of two prominent ANN algorithms, IVFFlat and HNSW, using the <a href="https://github.com/pgvector/">pgvector</a> extension in PostgreSQL. By examining their resource usage and performance across various datasets, I aim to highlight the strengths and weaknesses of each approach. Whether you’re a data scientist or a database enthusiast, this study offers valuable insights to help you make informed decisions for optimizing vector similarity searches in your specific applications.</p><p><a href="https://github.com/koseemre/pgvector-benchmark">GitHub - koseemre/pgvector-benchmark: Comparison of IVFFlat and HNSW Algorithms</a></p><h3>IVFFlat</h3><p>IVFFLAT (Inverted File with Flat Compression) is a kind of indexing technique used for approximate nearest neighbor search in large-scale databases. The algorithm divides the vectors in the database into multiple lists by clustering. So, similar vectors are located in the same list, which can be accessed by its inverted index. The inverted index maps each list (or cluster) to its constituent vectors, and each list is associated with a centroid vector that represents the center of that cluster.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ban5_5p38Mos2Nb0XEFdFw.png" /><figcaption>IVFFLAT list structure</figcaption></figure><p>In query time, the search operation is performed only on some of lists, so this works pretty fast. These lists where the query operation will be performed are the lists that have least distances between their centroid and the queried vector.</p><p>P.S. regarding the naming of the inverted file -&gt; Documents are indexed based on the terms they contain in inverted file systems. In other words, each term in the vocabulary is associated with a list of documents that contain that term. In the context of nearest neighbor search, rather than dealing with documents, we’re working with vectors in a high-dimensional space. Each vector is associated with a list of indices pointing to the vectors in the database that are similar to it.</p><h4>IVFFLAT parameters in PGVECTOR</h4><p>There are two main parameters to get an ideal recall value for ivfflat and these are suggested like below in the pgvector repository:</p><p>1- The <strong>number of lists </strong>value is suggested as <em>#rows/1000 </em>up to 1M rows and <em>sqrt(#rows) </em>for over 1M rows</p><p>2- The <strong>number of probes </strong>which represents number of lists to be needed to search during query process suggested as <em>sqrt(number of lists) </em>for starting value.</p><p>Both parameters should be tried by different values to achieve optimal results on specific dataset.</p><h3>HNSW</h3><p>HNSW (Hierarchical Navigable Small World) is another indexing algorithm that operates the approximate nearest neighbor method in high-dimensional space. Hierarchical NSW progressively constructs a multi-layer architecture composed of a hierarchical collection of proximity graphs (layers) for nested subsets of the stored elements. So, it effectively traverses through the data for nearest neighbor queries. <strong>Skip-lists</strong> and <strong>navigable small worlds</strong> are fundamental data structures to understand HNSW.</p><p>Each node in the graph corresponds to a data point (vector). According to the original paper, edges are mainly determined by distance metrics, and the graph is designed to balance search efficiency with accuracy. The upper layers include long-range connections that facilitate rapid navigation through the graph.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/868/1*k7atg2xcPeZPexXO0utTig.png" /><figcaption>Figure from the HNSW paper “Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs” , Yu. A. Malkov, D. A. Yashunin.</figcaption></figure><p>There are fine-grained connections in the bottom layers , typically reflecting the actual distances between points in the space. Search operation performs zoom-in method. The search initiates from the highest layer and progresses to the subsequent lower layer each time the local nearest neighbor is identified through a greedy algorithm among the nodes of that layer. In the end, the nearest neighbor found at the lowest layer provides the answer to the query.</p><p>P.S. The primary aim of this article is not to delve into the details of the algorithms. For those who wish to learn about HNSW in detail, including its technical background, it would be highly beneficial to start by reading the article at <a href="https://towardsdatascience.com/similarity-search-part-4-hierarchical-navigable-small-world-hnsw-2aad4fe87d37">this address.</a></p><h4>HNSW parameters in PGVECTOR</h4><p>m and ef_construction are two parameters of building the index;</p><ul><li>m is the maximum number of connections per layer (16 by default)</li><li>ef_construction is the size of the dynamic candidate list for constructing the graph (64 by default)</li></ul><blockquote>“A higher value of ef_construction provides better recall at the cost of index build time / insert speed.” [pgvector]</blockquote><p>ef_search is the query parameter which specifies the size of the dynamic candidate list for search (40 by default)</p><p>The dynamic candidate list operates based on a priority queue. This queue sorts the candidate vectors according to their distances from the queried vector. Therefore, it provides efficient and adaptive navigation through the graph, leading to effective approximate nearest neighbor search.</p><h3>Comparison of IVFFlat and HNSW in the postgre’s pgvector</h3><h4>Data sets</h4><p>I used four data sets with different dimension and size which are used also in the <a href="https://github.com/erikbern/ann-benchmarks">ann-benchmarks</a> repository. These datasets has been divided into training and testing sets and includes ground truth data for the 100 nearest neighbors.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sbhHuCR5gj32_jfDMoHMnw.png" /><figcaption>Table-1: Datasets</figcaption></figure><h4>Experiments</h4><p>The experiment starts by inserting the dataset into the table. Then, index build operation is started for hnsw and ivfflat. Experiments are processed for each dataset and indexing types separately.</p><p>Index build operation has both time and resource costs, so it is a good advantage not to reindex the table after initial insert operations if possible. But there is also a risk of getting inefficient similarity results if there are many insertions after initial indexing, especially for IVFFLAT because of it’s list based data structure. Hence, there are also experiments to track the behaviours of similarity search for different initial partial insertion rates. For example, if the parameter is 0.1, this means only the 10% of training data of the dataset is inserted to the table and indexed immediately afterwards. Then, remaining 90% of the dataset inserted to the table. The lists of index build experiments as follows:</p><p>Datasets<strong> = </strong>[MNIST, GIST, GLOVE, SIFT]</p><p>Index Types = [IVFFLAT, HNSW]</p><p>Initial Partial Insertion Rates = [0.1, 0.2, 0.3, 0.4, 1]</p><p>An example experiment by index build: (MNIST, HNSW, 0.1)</p><h4>Evaluation Metric</h4><p>The evaluation metric is r<em>ecall</em> which evaluates the proportion of retrieved neighbors that actually belong to the ground truth group. The parameter N represents the number of closest vectors to the queried vector which will be checked. N is 100 for the evaluation, so it will be based on top-100 recall.</p><h4>Experimental Setup</h4><p>PostgreSQL is set up using a Docker image and runs with an 8-core CPU limit and no memory limit. The specific versions used are PostgreSQL 15.4 (Debian 15.4–1.pgdg120+1) on x86_64-pc-linux-gnu and pgvector 0.5.0.</p><h4>Comparisons</h4><p>First two figures, index build duration and index size for each dataset and indexing type are obtained by given build parameters:</p><p><strong>Index Build Parameters</strong></p><p>IVFFLAT: number of lists= sqrt(#rows)</p><p>HNSW: m = 16, ef_construction=64</p><p><strong>Index Build Duration</strong></p><p>The IVFFLAT index builds faster, with times ranging from 12x to 42x, compared to the HNSW index build operation across datasets.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplotly.com%2F%7Eemreks%2F1.embed%3Fautosize%3Dtrue&amp;display_name=Plotly&amp;url=https%3A%2F%2Fplotly.com%2F%7Eemreks%2F1%2F&amp;image=https%3A%2F%2Fplotly.com%2Fstatic%2Fwebapp%2Fimages%2Fplotly-logo.8d56a320dbb8.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=plotly" width="600" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/d8924ce0af19db6cfcc6fdaf01213fb1/href">https://medium.com/media/d8924ce0af19db6cfcc6fdaf01213fb1/href</a></iframe><p><strong>Index Size</strong></p><p>The HNSW index size is 1.3 times larger than IVFFLAT for the GLOVE dataset and 4.5 times larger for the MNIST. This shows that the ratio of the space occupied by the indexes of the algorithms can vary significantly for different datasets.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplotly.com%2F%7Eemreks%2F4.embed%3Fautosize%3Dtrue&amp;display_name=Plotly&amp;url=https%3A%2F%2Fplotly.com%2F%7Eemreks%2F4%2F&amp;image=https%3A%2F%2Fplotly.com%2Fstatic%2Fwebapp%2Fimages%2Fplotly-logo.8d56a320dbb8.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=plotly" width="600" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/cefad98bd9bc2f8c4c8180757500d9f6/href">https://medium.com/media/cefad98bd9bc2f8c4c8180757500d9f6/href</a></iframe><p><strong>Query Parameters</strong></p><p>Query parameters affect the performance of similarity searches for both IVFFLAT and HNSW. Let’s examine how these parameters influence the results.</p><p><strong>Recall Mean w/ IVFFLAT’s probes and HNSW’s ef-search</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplotly.com%2F%7Eemreks%2F6.embed%3Fautosize%3Dtrue&amp;display_name=Plotly&amp;url=https%3A%2F%2Fplotly.com%2F%7Eemreks%2F6%2F&amp;image=https%3A%2F%2Fplotly.com%2Fstatic%2Fwebapp%2Fimages%2Fplotly-logo.8d56a320dbb8.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=plotly" width="600" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/21d92ccfc09117274744060072aee0a5/href">https://medium.com/media/21d92ccfc09117274744060072aee0a5/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplotly.com%2F%7Eemreks%2F8.embed%3Fautosize%3Dtrue&amp;display_name=Plotly&amp;url=https%3A%2F%2Fchart-studio.plotly.com%2F%7Eemreks%2F8%2F&amp;image=https%3A%2F%2Fchart-studio.plotly.com%2Fstatic%2Fwebapp%2Fimages%2Fplotly-logo.8d56a320dbb8.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=plotly" width="600" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/1e5a244282aa90937f958b4b4588d9c8/href">https://medium.com/media/1e5a244282aa90937f958b4b4588d9c8/href</a></iframe><p>As mentioned, the number of probes for IVFFLAT and the ef-search value for HNSW are directly proportional to recall and inversely proportional to query speed. A notable point is that for datasets other than GLOVE, the maximum recall value is already achieved when the ef-search value is 100, and similarly, for IVFFLAT, there is no need to increase probes after 5.</p><p><strong>Query per seconds and recall mean</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplotly.com%2F%7Eemreks%2F10.embed%3Fautosize%3Dtrue&amp;display_name=Plotly&amp;url=https%3A%2F%2Fchart-studio.plotly.com%2F%7Eemreks%2F10%2F&amp;image=https%3A%2F%2Fchart-studio.plotly.com%2Fstatic%2Fwebapp%2Fimages%2Fplotly-logo.8d56a320dbb8.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=plotly" width="600" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/034aa39d1bbe5966bb610c0c95cb4d4e/href">https://medium.com/media/034aa39d1bbe5966bb610c0c95cb4d4e/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplotly.com%2F%7Eemreks%2F12.embed%3Fautosize%3Dtrue&amp;display_name=Plotly&amp;url=https%3A%2F%2Fchart-studio.plotly.com%2F%7Eemreks%2F12%2F&amp;image=https%3A%2F%2Fchart-studio.plotly.com%2Fstatic%2Fwebapp%2Fimages%2Fplotly-logo.8d56a320dbb8.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=plotly" width="600" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/734e15b5039e173bc88504b30925ca88/href">https://medium.com/media/734e15b5039e173bc88504b30925ca88/href</a></iframe><p>Looking at Figures 5 and 6, we can see that the similarity search performance of IVFFLAT is more stable compared to HNSW in datasets other than GLOVE when parameters are changed. At the highest recall values, the highest QPS values show that HNSW is 2.3 and 1.4 times faster for the GIST and MNIST datasets, respectively, while for the other two datasets, their speeds are quite similar. It is notable that the two datasets where HNSW is faster have larger vector lengths compared to the others.</p><p><strong>Inserting Data after Initial Indexing</strong></p><p>There could be additional insertions in real-world cases after index build. So, to simulate this situation, I applied indexing for different initial partial insertions, then inserted the remaining rows. Let’s see how it works:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplotly.com%2F%7Eemreks%2F14.embed%3Fautosize%3Dtrue&amp;display_name=Plotly&amp;url=https%3A%2F%2Fchart-studio.plotly.com%2F%7Eemreks%2F14%2F&amp;image=https%3A%2F%2Fchart-studio.plotly.com%2Fstatic%2Fwebapp%2Fimages%2Fplotly-logo.8d56a320dbb8.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=plotly" width="600" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa68a1473dd7f2bb656b408fceebf877/href">https://medium.com/media/aa68a1473dd7f2bb656b408fceebf877/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplotly.com%2F%7Eemreks%2F16.embed%3Fautosize%3Dtrue&amp;display_name=Plotly&amp;url=http%3A%2F%2Fchart-studio.plotly.com%2F%7Eemreks%2F16%2F&amp;image=http%3A%2F%2Fchart-studio.plotly.com%2Fstatic%2Fwebapp%2Fimages%2Fplotly-logo.8d56a320dbb8.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=plotly" width="600" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/940e97118e6bf350e16ee4e06c8e96dd/href">https://medium.com/media/940e97118e6bf350e16ee4e06c8e96dd/href</a></iframe><p>HNSW does not require training for indexing. Due to this need, IVFFLAT is sensitive to the amount of initial data. As seen in Figure 7, different recall values were obtained for the GLOVE dataset with varying amounts of initial data, despite using the same search parameters. Notably, the highest recall value was achieved after indexing when only 10% of the data was added. No such sensitivity was observed in other datasets. For HNSW, no change was observed in any dataset, including GLOVE.</p><h3>Conclusion</h3><p>Looking at the results, it is difficult to declare a clear winner. Different datasets showcase different performance strengths. However, in general, considering resource consumption and indexing time, IVFFLAT appears to have an advantage. On the other hand, HNSW demonstrates superior speed performance, especially for datasets with relatively longer vector lengths, such as MNIST and GIST.</p><p>When examining the effects of data added after initial indexing, HNSW remains consistently stable across all datasets. In contrast, IVFFLAT could show variability in performance due to its list structure, which is built with initially calculated centroids.</p><p>These results suggest that, especially in real-world cases, it is crucial to evaluate based on the specific dataset to be used. There can be situations where speed and search performance are comparable, and in such cases, avoiding unnecessary resource consumption is possible.</p><h3>Further Experiments</h3><p>The datasets used in the experiments can be considered relatively small in scale. In my next article, I plan to conduct experiments on datasets with much larger sizes (10M and above). Secondly, I intend to explore how search performance is affected in these large tables by using partitioning, and I will share my findings for both algorithms.</p><p>If you notice any points that need correction in my text or have any comments about the article, please share them with me.</p><h3>References</h3><ul><li><a href="https://github.com/pgvector/pgvector">https://github.com/pgvector/pgvector</a></li><li><a href="https://arxiv.org/abs/1603.09320">https://arxiv.org/abs/1603.09320</a> — Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs</li><li><a href="https://github.com/erikbern/ann-benchmarks">https://github.com/erikbern/ann-benchmarks</a></li><li><a href="https://towardsdatascience.com/similarity-search-part-4-hierarchical-navigable-small-world-hnsw-2aad4fe87d37">https://towardsdatascience.com/similarity-search-part-4-hierarchical-navigable-small-world-hnsw-2aad4fe87d37</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e1626505bc9a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>