title: Getting Total Sales data in mysql across tables. query timing out
body: <p>Getting Total Sales data. </p>
  
  <p>I have a database on mysql hosted on godaddy with the following tables.</p>
  
  <ol>
  <li>Customer_data - custcode, name, etc. (about 450 rows)</li>
  <li>delivery_data - date, invoice no, quantity, etc. (about 800 rows per month)</li>
  </ol>
  
  <p>I need to get a total sales data for all customers in a month. (ideally for different month ranges which will be input by user to analyse trends).</p>
  
  <pre><code>+---------------+--------+--------+--------+--------+--------+--------+
  | Customer Name | Jan-18 | Feb-18 | Mar-18 | Apr-18 | May-18 | Jun-18 |
  +---------------+--------+--------+--------+--------+--------+--------+
  | ABC           |   30   |   50   |   30   |   80   |   70   |  140   |
  +---------------+--------+--------+--------+--------+--------+--------+
  | DEF           |   40   |   50   |   130  |  180   |   70   |  140   |
  +---------------+--------+--------+--------+--------+--------+--------+
  .....
  </code></pre>
  
  <p>I have tried doing this -</p>
  
  <ol>
  <li>Php with looped json - The query times out as there is just too much data to process</li>
  <li>Excel VBA - It takes a few hours to run the query on the server and all other operations come to a standstill.</li>
  </ol>
  
  <p>What could be the best way to get this data in a more efficient way?</p>
  
  <p>Edit 1 - </p>
  
  <p>The SQL QUERY</p>
  
  <pre><code>while ($year&lt;2017){
      $months=0;
      while ($months&lt;12){
          $cust_code=mysql_result($result2,$no,"code");
          $sqlquery3="select ifnull(sum(a.quantity),0) as total_quantity
                  from delivery_line a, delivery_main b 
              where 
                  a.item REGEXP 'INDOX|MEDOX|INDNIT|INDCO2|ARGON'
                  and a.del_no=b.no 
                  and a.series=b.series
                  and b.cust_code='$cust_code'
                  and month(b.date_added)=($months+1)
                  and year(b.date_added) = ($year)";
          $result3=mysql_query($sqlquery3);
          $count3=mysql_num_rows($result3);
          $quantity=mysql_result($result3,0,"total_quantity");
          echo "&lt;td&gt;".$quantity."&lt;/td&gt;";
      $months++;
      }
      $year++;
  }
  </code></pre>