Please concurrency heroes give me a solution...

WBOY
Release: 2016-08-18 09:15:53
Original
919 people have browsed it

Because there is a piece of code that uses while loop code, it will fail once the amount of data is large. Please give me a solution....

while($rows = $query->fetch_array()){

<code>if($rows['mingxi_1']=='1'){
    $ds = Ssc_Ds($rs['ball_1']);
    $dx = Ssc_Dx($rs['ball_1']);
    if($rows['mingxi_2']==$rs['ball_1'] || $rows['mingxi_2']==$ds || $rows['mingxi_2']==$dx){
     
        $msql="update c_bet set js=1 where id='".$rows['id']."'";
        $mysqli->query($msql) or die ("修改订单状态失败!!!".$rows['id']);
        //给会员账户增加奖金
        $msql="update k_user set money=money+".$rows['win']." where uid=".$rows['uid']."";
        $mysqli->query($msql) or die ("修改失败!!!".$rows['id']);
    }else{
        //注单未中奖,修改注单内容
        $msql="update c_bet set win=0,js=1 where id=".$rows['id']."";
        $mysqli->query($msql) or die ("修改失败!!!".$rows['id']);
    }
}</code>
Copy after login
Copy after login

Reply content:

Because there is a piece of code that uses while loop code, it will fail once the amount of data is large. Please give me a solution....

while($rows = $query->fetch_array()){

<code>if($rows['mingxi_1']=='1'){
    $ds = Ssc_Ds($rs['ball_1']);
    $dx = Ssc_Dx($rs['ball_1']);
    if($rows['mingxi_2']==$rs['ball_1'] || $rows['mingxi_2']==$ds || $rows['mingxi_2']==$dx){
     
        $msql="update c_bet set js=1 where id='".$rows['id']."'";
        $mysqli->query($msql) or die ("修改订单状态失败!!!".$rows['id']);
        //给会员账户增加奖金
        $msql="update k_user set money=money+".$rows['win']." where uid=".$rows['uid']."";
        $mysqli->query($msql) or die ("修改失败!!!".$rows['id']);
    }else{
        //注单未中奖,修改注单内容
        $msql="update c_bet set win=0,js=1 where id=".$rows['id']."";
        $mysqli->query($msql) or die ("修改失败!!!".$rows['id']);
    }
}</code>
Copy after login
Copy after login

1. Can you upload the full version of the code? Are you reluctant to take out SQL?
2. Visual inspection is that there are too many SQL result sets, the memory may overflow, and the operation may be time-consuming
3. Solution: Use redis queue or MySQL queue to solve the problem

The loop operation must not be written like this in the program. . . You can create a new SQL table as a queue, or use redis directly as a queue. . Then open a new service or script to handle this task

It is estimated that the processing speed is too slow, causing the server to be unable to respond to new requests, resulting in failure. Judging from the code, there is a problem with your processing model. It is best not to use a loop to process orders one by one. If possible, use a database to solve the problem. For example, stored procedures are faster.

Your $rs should be a large array, and you are using mysql query in the loop, which will be very inefficient.
It is recommended to run a cli mode PHP for asynchronous processing

I don’t want to use queues to give you a very simple and violent multi-process idea

The largest query result set is split into multiple processes based on the ID modulus. The idea is similar to the following pseudo code

<code>        for ($i=1;$i<=100;$i++){

            if($i%5==0){
                echo 'mod 1 : '.$i.'<br>';
            }
            if($i%5==1){
                echo 'mod 2 : '.$i.'<br>';
            }
            if($i%5==2){
                echo 'mod 3 : '.$i.'<br>';
            }
            if($i%5==3){
                echo 'mod 4 : '.$i.'<br>';
            }
            if($i%5==4){
                echo 'mod 5 : '.$i.'<br>';
            }

        }</code>
Copy after login

Carry out modulo splitting of all IDs based on the amount of data. As for whether you take out all the IDs and split them, or directly open 5 PHP processes and split them when querying SQL statements, you need to make a specific analysis based on your business logic. .

The SQL statement split into 5 processes is similar to the following:

<code>SELECT * FROM member where member_id%5=0;
SELECT * FROM member where member_id%5=1;
SELECT * FROM member where member_id%5=2;
SELECT * FROM member where member_id%5=3;
SELECT * FROM member where member_id%5=4;</code>
Copy after login

You can use while(1){
to take out part of the data for processing at one time, and then break out of the loop after the processing is completed
}

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!