<?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: Ritesh Rungta</title>
    <description>The latest articles on DEV Community by Ritesh Rungta (@ritesh_rungta_e849b30e0b6).</description>
    <link>https://dev.to/ritesh_rungta_e849b30e0b6</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%2F1995897%2F2b12c1bd-2309-4f76-81ee-7501b291d7c0.png</url>
      <title>DEV Community: Ritesh Rungta</title>
      <link>https://dev.to/ritesh_rungta_e849b30e0b6</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ritesh_rungta_e849b30e0b6"/>
    <language>en</language>
    <item>
      <title>Slow SQL Queries? Boost Your App's Performance with This Technique</title>
      <dc:creator>Ritesh Rungta</dc:creator>
      <pubDate>Tue, 24 Sep 2024 18:33:25 +0000</pubDate>
      <link>https://dev.to/ritesh_rungta_e849b30e0b6/slow-sql-queries-boost-your-apps-performance-with-this-technique-3gnp</link>
      <guid>https://dev.to/ritesh_rungta_e849b30e0b6/slow-sql-queries-boost-your-apps-performance-with-this-technique-3gnp</guid>
      <description>&lt;h2&gt;
  
  
  The Challenge
&lt;/h2&gt;

&lt;p&gt;In my app (React + Spring Boot + Oracle), dealing with large datasets led to frustratingly slow processing time. I needed a solution to accelerate performance without compromising accuracy or completeness.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution: NTILE + Parallel Processing
&lt;/h2&gt;

&lt;p&gt;NTILE is a powerful SQL window function designed to partition a result set into a specified number of roughly equal-sized chunks, known as "tiles." Each row is assigned a partition number based on its position in the ordered set.&lt;/p&gt;

&lt;p&gt;By using NTILE, I split the query results into manageable chunks and processed these partitions in parallel. This approach allowed me to fetch and handle data simultaneously, significantly reducing wait times.&lt;/p&gt;

&lt;p&gt;Here’s a practical example of how to implement this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WITH PartitionedSales AS (
    SELECT 
        sales_id,
        sales_amount,
        sales_date,
        NTILE(2) OVER (ORDER BY sales_id) AS partition_number -- Assigns a partition number (1 or 2) to each row
    FROM 
        sales
    WHERE 
        sales_date BETWEEN '2023-01-01' AND '2023-12-31'
)
SELECT * 
FROM PartitionedSales
WHERE partition_number = :partitionNumber -- Replace :partitionNumber with the actual partition number (1 or 2)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above SQL snippet:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;NTILE(2) divides the data into two equal chunks which will be sorted based on sales_id.&lt;/li&gt;
&lt;li&gt;Replace :partitionNumber with 1 or 2 to fetch data from the corresponding partition.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On the frontend, you can use parallel processing to fetch each partition efficiently:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function fetchPartition(partitionNumber) {
    const response = await fetch('/api/sales?partition=' + partitionNumber});
    return response.json();
}

async function fetchData() {
    try {
        const [partition1, partition2] = await Promise.all([
            fetchPartition(1), // Fetch the first partition
            fetchPartition(2)  // Fetch the second partition
        ]);

        // Combine and process results
        const combinedResults = [...partition1, ...partition2];
        processResults(combinedResults);
    } catch (error) {
        console.error('Error fetching data:', error);
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;fetchPartition retrieves data for a specific partition.&lt;/li&gt;
&lt;li&gt;fetchData runs both fetch operations in parallel and processes the combined results.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How You Can Do It Too
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Identify the Heavy Queries: Find the queries that are slowing down your app.&lt;/li&gt;
&lt;li&gt;Apply NTILE: Use the NTILE function to divide the query results into smaller parts.&lt;/li&gt;
&lt;li&gt;Parallel Processing: Execute these smaller queries in parallel, leveraging your app’s ability to handle concurrent tasks.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you’re looking to boost performance in your data-heavy applications, give this method a try. It’s a smart, effective way to make your queries work harder, not longer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Important Consideration
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;When handling concurrent requests, the demand on database connections can become significant. This heavy utilization of connections may strain your database, potentially leading to performance bottlenecks. It's essential to monitor and manage the number of concurrent requests to ensure that your database remains responsive and performs efficiently.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>database</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
