Iterating over records in one table and adding them to another with calculations

时间:2019-01-09 22:08:11

标签: mysql

I'm currently using PHP and MySQL to retrieve a set of 100,000 records in a table, then iterate over each of those records to do some calculations and then insert the result into another table. I'm wondering if I'd be able to do this in pure SQL and make the query run faster.

Here's what I"m currently using:

$stmt= $pdo->query("
SELECT Well_Permit_Num
     , Gas_Quantity
     , Gas_Production_Days
  FROM DEP_OG_Production_Import
 ORDER 
    BY id ASC
");

foreach ($stmt as $row) {
     $data = array('well_id' => $row['Well_Permit_Num'],
                   'gas_quantity' => $row['Gas_Quantity'],
                   'gas_days' => $row['Gas_Production_Days'],
                   'gas_average' => ($row['Gas_Production_Days']);


    $updateTot = $pdo->prepare("INSERT INTO DEP_OG_TOTALS 
                                      (Well_Permit_Num,
                                      Total_Gas,
                                      Total_Gas_Days,
                                      Total_Gas_Avg)
                                VALUES (:well_id,
                                        :gas_quantity,
                                        :gas_days,
                                        :gas_average)
                                ON DUPLICATE KEY UPDATE
                                   Total_Gas = Total_Gas + VALUES(Total_Gas),
                                   Total_Gas_Days = Total_Gas_Days + VALUES(Total_Gas_Days),
                                   Total_Gas_Avg =(Total_Gas + VALUES(Total_Gas)) / (Total_Gas_Days + VALUES(Total_Gas_Days))");



}

I'd like to see if this can be done in pure MySQL instead of having to use PHP just for the fact of using it to hold the variables.

My Result should be 1 record that is a running total for each Well. The source table may house 60-70 records for the same well, but over a few thousand different Wells.

It's a constant import process that has to be run, so it's not like there is a final table which you can just do SUM(Gas_Quantity)... etc.. on

1 个答案:

答案 0 :(得分:0)

As commented by Uueerdo, you seem to need an INSERT ... SELECT query. The role of such query is to INSERT insert the resultset returned by an inner SELECT. The inner select is an aggregate query that computes the total sum of gas and days for each well.

INSERT INTO DEP_OG_TOTALS (Well_Permit_Num, Total_Gas, Total_Gas_Days, Total_Gas_Avg)
SELECT
    t.Well_Permit_Num, 
    SUM(t.Gas_Quantity) Total_Gas,
    SUM(t.Gas_Production_Days) Total_Gas_Days
FROM DEP_OG_Production_Import t
GROUP BY t.Well_Permit_Num
ON DUPLICATE KEY UPDATE
    Total_Gas = Total_Gas + t.Total_Gas,
    Total_Gas_Days = Total_Gas_Days + t.Total_Gas_Days,
    Total_Gas_Avg =(Total_Gas + t.Total_Gas) / (Total_Gas_Days + t.Total_Gas_Days)