0

I've got a function which calculates some data from a table, a put's it into another, it takes a while for the whole process, about 30 mins or something like that. The function calculates statistics for each day for a couple of months period, but after like 10 days, it starts another thread while continuing another one, and then another one, and another, so i get a table full of random crap in the end.

Why does it start another instance of the function while the other one isn't finished?

Here's the function:

function build_fill_array() {

$sql = db_fetch_object(db_query("SELECT min(timestamp) as min FROM {accesslog}"));
$nids = db_query("SELECT nid FROM {node} WHERE type='raamat'");
while($nid_array = db_fetch_array($nids)) {
    $nid[] = $nid_array['nid'];
}
$start_ts = $sql->min;
$end_ts = mktime(0,0,0, date('m'), date('d'), date('Y'));
$start_ts = mktime(0,0,0, date('m', $start_ts), date('d', $start_ts), date('Y', $start_ts));

while($start_ts != $end_ts) {
    $day = date('d.m.Y', $start_ts);
    $range_from = mktime(0,0,0, date('m', $start_ts), date('d', $start_ts), date('Y', $start_ts));
    $range_to = mktime(23,59,59, date('m', $start_ts), date('d', $start_ts), date('Y', $start_ts));
    foreach($nid as $n) {
    $results = db_fetch_object(db_query("SELECT count(hostname) as total, count(distinct hostname) as visits FROM {accesslog} WHERE timestamp BETWEEN %d AND %d AND path = 'node/%d'", $range_from, $range_to, $n));
    $sql_insert = "INSERT INTO {statistics_view} (`id`, `nid`, `total`, `unique`, `timestamp`, `date`) VALUES (NULL, $n, $results->total, $results->visits, ".mktime().", '$day');";
    db_query($sql_insert); 
  }


    $start_ts = mktime(0,0,0, date('m', $start_ts), date('d', $start_ts)+1, date('Y', $start_ts));
}

}

Edit: don't mind the {} around table names, it's a drupal thing.

4
  • 1
    Why does it start another instance of the function while the other one isn't finished Commented Dec 11, 2009 at 10:30
  • 1
    i don't know what "starting another instance of the function" means. Please elaborate. Commented Dec 11, 2009 at 10:34
  • 1
    I think you can do it in MySQL alone, with a stored procedure. Overhead for retrieving large dataset to PHP and sending it back can be quite notable. Commented Dec 11, 2009 at 10:35
  • 2 johannes: means that it starts to collect all the data and insert it from the beginning, so on day 10.09.2009, it starts again by inserting 1.09.2009, 2.09.2009 and so on, while the previous thread goes further by 11.09.2009, 12.09.2009. In the end you get a mix of 2.09.2009 and 12.09.2009 at the same time. Commented Dec 11, 2009 at 10:38

2 Answers 2

1

Why does it start another instance of the function while the other one isn't finished?

You should look at where the function is called, not the function itself.

Sign up to request clarification or add additional context in comments.

Comments

0

Replace

while($start_ts != $end_ts) {

with:

while($start_ts <= $end_ts) {

1 Comment

didn't help, now have to wait an hour for it to finish :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.