There's got to be a faster way to update
March 27, 2009 1:02 PM
Subscribe
Please help me simplify a complicated mysql update
I have a giant database with data nested in 3 levels. Year (00-07), time (1-15) and location (1-125000)
Let's say I have a field called z. I am trying to add a field called cumulative z (by year) with each record.
My current approach to inserting cumulative z values is as follows:
For the sake of tractability, I have split the data into separate tables for each year.
Using PHP, I have 2 nested loops (time and location).
Within the inner loop, the following happens:
If time is 1, then cumulative z= z for that location.
if time>1
then cumulative z= z+cumulative z for time t-1. This step is the bottleneck. I have to write another query statement to find out what cum z was for the same location at t-1.
The query takes very long for each location. I estimate that it will take several days to do this for a single year. There must be a faster way. Can any SQL gurus offer suggestion to make this update faster?
posted by special-k to computers & internet (19 comments total)
1 user marked this as a favorite
2. Define your table structures? Are they indexed properly? Scratch that ... are they indexed?
3. Include your existing code and query?
posted by devbrain at 1:20 PM on March 27, 2009