<?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: Adrian Ferrandis</title>
    <description>The latest articles on DEV Community by Adrian Ferrandis (@ferrandisme).</description>
    <link>https://dev.to/ferrandisme</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%2F1051065%2F3a6e0563-fc29-45d6-ba1c-3c704855bcf0.png</url>
      <title>DEV Community: Adrian Ferrandis</title>
      <link>https://dev.to/ferrandisme</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ferrandisme"/>
    <language>en</language>
    <item>
      <title>OOP: Database optimization</title>
      <dc:creator>Adrian Ferrandis</dc:creator>
      <pubDate>Thu, 23 Mar 2023 23:03:26 +0000</pubDate>
      <link>https://dev.to/ferrandisme/oop-database-optimization-1hej</link>
      <guid>https://dev.to/ferrandisme/oop-database-optimization-1hej</guid>
      <description>&lt;p&gt;This example will check how to optimize a loop with queries inside. If you are experienced/senior, this may be easy or not useful. After this post, you will be able to solve this problem with a production-ready solution. &lt;/p&gt;

&lt;h2&gt;
  
  
  Context
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;This post discusses a real-life scenario where we need to import data for a restaurant and summarize it for the marketing team within a week&lt;/strong&gt;. The data we have is &lt;strong&gt;24,000 receipts&lt;/strong&gt; from the last year, and each receipt contains the date, a list of food IDs, a promotion ID, an employee ID, and satisfaction ratings. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;date&lt;br&gt;
   List -&amp;gt; id of food&lt;br&gt;
   id from promotion&lt;br&gt;
   id from employee&lt;br&gt;
   satisfaction (0-5)&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Analysis
&lt;/h2&gt;

&lt;p&gt;To summarize the data, we need to retrieve more information about the food items, check the promotion details, and obtain the employee's name for each receipt. We can achieve this by looping through each receipt and making database queries to get the required information. In code it will be something like this&lt;/p&gt;

&lt;p&gt;&lt;code&gt;loop(receive from receives){&lt;br&gt;
allFood = getFoodsById(receive.foodIds) // 1*foods database query&lt;br&gt;
    promotion = getPromotionById(receive.promotionId) // 1 database query&lt;br&gt;
    employee = getEmployeeById(receive.employeId)  // 1 database query&lt;br&gt;
    addSumaryInformation(allFood, promotion, employee)  &lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;However, with (2 + foods) * 24,000 database queries. We are going to assume that we have 10 foods for each order. It would take approximately &lt;strong&gt;20 days&lt;/strong&gt; to complete the process, which is longer than the required time frame. This delay could be problematic since this is a critical process that needs to be performed every day.&lt;/p&gt;

&lt;p&gt;Formula:&lt;br&gt;
&lt;code&gt;((12)*24000)*0,1s = 28800 seconds  = 480 h = 20 days&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;The good news is that reducing the processing time is possible and easy by optimizing the code. For instance, we can reduce the number of database queries by fetching all the necessary information in a single query. Additionally, we can use caching to avoid making multiple calls to the database for the same information. By implementing these optimizations, we can significantly reduce the processing time and meet the marketing team's deadline.&lt;/p&gt;

&lt;p&gt;In conclusion, by optimizing the code, we can efficiently import and summarize data for the restaurant within the required timeframe and meet the marketing team's needs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;

&lt;p&gt;Assumption: We had looped the list elements and got all ids in&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Map&amp;lt;id, Food&amp;gt; foods = getAllFoodsByIds(foodIds)&lt;br&gt;
Map&amp;lt;id, Promotion&amp;gt; foods = getAllPromotionsByIds(foodIds)&lt;br&gt;
Map&amp;lt;id, Employee&amp;gt; foods = getAllEmployeesByIds(employeeIds)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;loop(receive from i){&lt;br&gt;
    allFood = ... here will have a loop that gets from the map each key&lt;br&gt;
    promotion = promotions.get(receive.promotionId)&lt;br&gt;
    employee = getEmployeeById(receive.employeeId)  &lt;br&gt;
    addSumaryInformation(allFood, promotion, employee)  &lt;br&gt;
}&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If each big query is 1 second and we have three queries, this will be 3 seconds. We can assume 5 minutes if we need to make more queries (database max IN in SQL) or process the result. This time will be much lower than the original 20 days. &lt;strong&gt;It is a 0,017% of the initial time.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If each big query is 1 second and we have three queries, this will be 3 seconds. We can assume 5 minutes if we need to make more queries (database max IN in SQL) or process the result. This time will be much lower than the original 20 days. It is a 0,017% of the initial time.&lt;/p&gt;

&lt;p&gt;Note: In reality, the time can be much lower. I tested this in Java/Spring/JPA, and the time reduction has been around 90/95%.&lt;br&gt;
Note2: This solution may increase the consumption of other resources.&lt;/p&gt;

&lt;p&gt;Thank you for reading! Check my website to get more information about me.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>tutorial</category>
      <category>database</category>
      <category>oop</category>
    </item>
  </channel>
</rss>
