Best way to program queries 10'000 + times?

I always have problems with PHP when doing large loops. I divided it into very small loops and made them hundreds of times instead, when it comes to large quantities.

This is one of the examples I'm working with now:

$stmt = $GLOBALS['link']->prepare('SELECT * FROM tracking_redirect');
$stmt->execute();
$tracking = new tracking();
while($click = $stmt->fetch()){
    $tracking->checkClickIP($click['blogid'], $click['campaignid'], $click['id'], $click['ipaddress']);
}

tracking_redirect is a table with a lot of information about which user is being redirected to a site. I store data like ipaddress, cookie, date and user id. The table currently has ~ 60,000 rows.

checkClickIP () is a function for checking whether a click / redirect is valid or valid. Example to make sure that the same user does not click too many times.

public function checkClickIP($userid, $campaignid, $clickid, $ipaddress = null) {
        if(!isset($ipaddress)){
            $ipaddress = $_SERVER['REMOTE_ADDR'];
        }

        $datestmt = $GLOBALS['link']->prepare('SELECT crdate FROM tracking_redirect WHERE id=:clickid');
        $datestmt->execute(array('clickid' => $clickid));
        $datestmt = $datestmt->fetch();

        $stmt = $GLOBALS['link']->prepare('SELECT COUNT(*) as total FROM tracking_redirect WHERE ipaddress=:ipaddress AND campaignid=:campaignid AND blogid=:userid AND crdate<:clickdate');
        $stmt->execute(array(
            'ipaddress' => $ipaddress, 
            'campaignid' => $campaignid, 
            'userid' => $userid, 
            'clickdate' => $datestmt['crdate']
        ));
        $totalclicks = $stmt->fetch();

        //Same computer has clicked more than 5 times on the same campaign. ALERT WARNING!
        if($totalclicks['total'] >= 5){
            //Disable the click
            $disable = $GLOBALS['link']->prepare('UPDATE tracking_redirect SET disabled=:disabled WHERE id=:clickid');
            $disable->execute(array(
                'disabled' => 1, 
                'clickid' => $clickid
            ));

            //Send a warning to the user if this person clicked 5 times on the same campaign.
            if($totalclicks['total'] == 5){
                $stmt = $GLOBALS['link']->prepare('SELECT * FROM user_login_history WHERE userid=:userid AND usertype=:usertype AND ipaddress=:ipaddress AND date(visitdate) BETWEEN :startdate AND :currentdate LIMIT 1');
                $stmt->execute(array(
                    'userid' => $userid, 
                    'usertype' => 1, 
                    'ipaddress' => $ipaddress, 
                    'startdate' => date('Y-m-d', strtotime('-3 months')), //If user logged in 3 months ago or closer from this IP
                    'currentdate' => date('Y-m-d')
                ));
                //The computer belongs to the blogger who published the ad. ALERT WARNING! USER CLICKING HIS OWN ADS
                if($loginhistory = $stmt->fetch()){
                    //Send warning to user.
                }
                //The computer does not belong to the blogger, but it suspicious behavior. ALERT WARNING! CHECK POST
                else{
                    //Send warning to user.
                }
            }
        }
    } 

, While. . , while, , ..

, , . , . , ~ 10000 ~ 100 000 ?

+4
1

, . , 20K GO. LINQ, , - , , , .

MySQL

+3

All Articles