Home > Backend Development > PHP Tutorial > What should I do if the performance of loop traversal and combination of data retrieved from the database is poor?

What should I do if the performance of loop traversal and combination of data retrieved from the database is poor?

WBOY
Release: 2016-08-04 09:19:02
Original
1063 people have browsed it

Requirement: Statistics of daily data based on start and end time
The current data retrieved from the database has about 10 two-dimensional arrays with the same structure as follows:

<code>[
    '0' => [
        'time' => '2016-8-3',
        'data1'=> 'xxx',
        ...
    ]
]</code>
Copy after login
Copy after login

Because each array has required fields, these 10 two-dimensional arrays need to be combined. The idea is as follows:
Traverse and retrieve data based on date

<code>while (strtotime($start_time) < strtotime($end_time)) {
    
    // 10个foreach
    foreach($arr as $k => $v) {
        if (strtotime($start_time) == strtotime($v['time'])) {
            $data[] = $v[];
            ...
        }
    }
    
    foreach($arr as $k => $v) {
        if (strtotime($start_time) == strtotime($v['time'])) {
            $data[] = $v[];
            ...
        }
    }
    
    foreach($arr as $k => $v) {
        if (strtotime($start_time) == strtotime($v['time'])) {
            $data[] = $v[];
            ...
        }
    }
    ...

    $start_time = strtotime($start_time . ' +1 day');
    
}</code>
Copy after login
Copy after login

The final data structure I want to combine:

<code>[
    '2016-8-3' => [
        'data' => '',
        ...
    ],
    
    '2016-8-4' => [
        'data' => '',
        ...
    ]
    ..
]</code>
Copy after login
Copy after login

But it seems that the performance of that while is very poor, and the data cannot be run out. After waiting for a long time, it directly prompts:
Maximum execution time of 30 seconds exceeded
Do you have any good suggestions?

Reply content:

Requirement: Statistics of daily data based on start and end time
The current data retrieved from the database has about 10 two-dimensional arrays with the same structure as follows:

<code>[
    '0' => [
        'time' => '2016-8-3',
        'data1'=> 'xxx',
        ...
    ]
]</code>
Copy after login
Copy after login

Because each array has required fields, these 10 two-dimensional arrays need to be combined. The idea is as follows:
Traverse and retrieve data based on date

<code>while (strtotime($start_time) < strtotime($end_time)) {
    
    // 10个foreach
    foreach($arr as $k => $v) {
        if (strtotime($start_time) == strtotime($v['time'])) {
            $data[] = $v[];
            ...
        }
    }
    
    foreach($arr as $k => $v) {
        if (strtotime($start_time) == strtotime($v['time'])) {
            $data[] = $v[];
            ...
        }
    }
    
    foreach($arr as $k => $v) {
        if (strtotime($start_time) == strtotime($v['time'])) {
            $data[] = $v[];
            ...
        }
    }
    ...

    $start_time = strtotime($start_time . ' +1 day');
    
}</code>
Copy after login
Copy after login

The final data structure I want to combine:

<code>[
    '2016-8-3' => [
        'data' => '',
        ...
    ],
    
    '2016-8-4' => [
        'data' => '',
        ...
    ]
    ..
]</code>
Copy after login
Copy after login

But it seems that the performance of that while is very poor, and the data cannot be run out. After waiting for a long time, it directly prompts:
Maximum execution time of 30 seconds exceeded
Do you have any good suggestions?

There is something wrong with your loop, causing an endless loop. You can print out the $start_time and $end_time of each cycle. It must be strtotime($start_time) < strtotime($end_time)It will always be true

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template