Jump to content

Using count(*) slows down the server


allex01

Recommended Posts

Check attached screenshot. The users table contains around 100 colums and 14,000 rows. I'm using the following function to find users. Count is what causing the issue. When the function excutes it keeps running for over 12 minutes to find the users. this is putting a huge load on the server.

 

public function Total_referrals($opt,$user_id)
         {
            
            switch($opt)
            {
                                    case 1:
                                           $where=" where ";
                                           break; 
                                    case 2: 
                                           $doj_start = sprintf("%04d-%02d-%02d", date('Y'),date('m'),date('d'));
                       $dojStart  = $doj_start;
                                            $where=' where doj>= \''.addslashes($dojStart).'\''.' and';
                                            break; 
                                    case 3: $where=" where trans_id=0 and ow_id!=0 and ";
                                            break;        
               
                                    case 4: $where=" where trans_id!=0 and ow_id!=0 and ";
                                            break;        
               
            }
            $where.=' ow_id ='.$user_id;//$this->CFG['user']['user_id'];   
            $sql = 'SELECT count(*) as cnt FROM '.$this->CFG['db']['tbl']['users'];
            $sql .=$where; 
            $stmt = $this->dbObj->Prepare($sql);
            $rs = $this->dbObj->Execute($stmt);
            if (!$rs)
               trigger_error($this->dbObj->ErrorNo().' '.$this->dbObj->ErrorMsg(), E_USER_ERROR);
            if ($rs->PO_RecordCount())
            {
                if($row = $rs->FetchRow())
               {
                          $cnt=$row['cnt'];
                    } // while
            }
            return $cnt;
         }

 

mysql.gif.

Query: SELECT user_id, user_name, sex, first_name, allow_article, last_name, email, age, total_videos, total_musics, total_photos, total_friends, 1 AS total_groups, DATE_FORMAT(doj, '%D %b %y') AS doj, DATE_FORMAT(last_logged, '%D %b %y') AS last_logged, profile_tags, 1 AS tag_match, icon_id, icon_type, usr_status, DATE_FORMAT(last_logged, '%D %b %y') AS last_logged, city, postal_code, country, featured, is_affiliate_type FROM users WHERE usr_status!='Deleted' AND user_id in (select ow_id from users) ORDER BY user_id ASC LIMIT 0,16

Query: SELECT COUNT(*) AS count FROM users WHERE usr_status!='Deleted' AND user_id in (select ow_id from users)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.