<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: N3MO</title>
    <description>The latest articles on DEV Community by N3MO (@n3mo-dev).</description>
    <link>https://dev.to/n3mo-dev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3977637%2Fd80db94a-7cb8-46fc-a3ba-84fcd9d42bc3.png</url>
      <title>DEV Community: N3MO</title>
      <link>https://dev.to/n3mo-dev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/n3mo-dev"/>
    <language>en</language>
    <item>
      <title>How I cut Django indexing from 23 minutes to 11 minutes with one SQL change</title>
      <dc:creator>N3MO</dc:creator>
      <pubDate>Wed, 10 Jun 2026 11:56:37 +0000</pubDate>
      <link>https://dev.to/n3mo-dev/how-i-cut-django-indexing-from-23-minutes-to-11-minutes-with-one-sql-change-5of</link>
      <guid>https://dev.to/n3mo-dev/how-i-cut-django-indexing-from-23-minutes-to-11-minutes-with-one-sql-change-5of</guid>
      <description>&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;I'm building N3MO — an open source code intelligence &lt;br&gt;
engine that maps Python codebases into a knowledge &lt;br&gt;
graph stored in PostgreSQL.&lt;/p&gt;

&lt;p&gt;When I tested it on Django (3,021 Python files, &lt;br&gt;
181,000+ function calls), the full index took &lt;br&gt;
23 minutes. Extraction was 8 minutes. Linking &lt;br&gt;
was 15 minutes.&lt;/p&gt;

&lt;p&gt;Linking was the bottleneck.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why linking was slow
&lt;/h2&gt;

&lt;p&gt;N3MO's linking phase resolves which function calls &lt;br&gt;
which. The original query used LIKE:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;call_name&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="s1"&gt;'%%.'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This matches calls like &lt;code&gt;module.function&lt;/code&gt; or &lt;br&gt;
&lt;code&gt;self.function&lt;/code&gt; by checking if the call name &lt;br&gt;
ends with the function name.&lt;/p&gt;

&lt;p&gt;The problem: a leading wildcard LIKE query &lt;br&gt;
cannot use indexes. PostgreSQL has to compare &lt;br&gt;
every call against every symbol.&lt;/p&gt;

&lt;p&gt;For Django: 181,000 calls × 43,000 symbols = &lt;br&gt;
7.8 billion comparisons. That's why it took &lt;br&gt;
15 minutes.&lt;/p&gt;
&lt;h2&gt;
  
  
  The fix
&lt;/h2&gt;

&lt;p&gt;Replace LIKE with SPLIT_PART:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;SPLIT_PART&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;call_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'.'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;SPLIT_PART splits the call name at '.' and takes &lt;br&gt;
the last part. &lt;code&gt;module.function&lt;/code&gt; becomes &lt;code&gt;function&lt;/code&gt;. &lt;br&gt;
Then it's an exact match — which CAN use indexes.&lt;/p&gt;

&lt;h2&gt;
  
  
  The result
&lt;/h2&gt;

&lt;p&gt;Before: 23 minutes&lt;br&gt;
After: 11 minutes&lt;br&gt;
Speedup: 2x faster from one line change&lt;/p&gt;

&lt;h2&gt;
  
  
  What I learned
&lt;/h2&gt;

&lt;p&gt;When you have a performance problem in SQL:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Leading wildcard LIKE = no index possible&lt;/li&gt;
&lt;li&gt;SPLIT_PART + exact match = index friendly&lt;/li&gt;
&lt;li&gt;Always check if your WHERE clause can use indexes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;N3MO is open source: github.com/RajX-dev/N3MO&lt;/p&gt;

</description>
      <category>django</category>
      <category>performance</category>
      <category>postgres</category>
      <category>sql</category>
    </item>
  </channel>
</rss>
