Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MYSQLI行计算太慢_Php_Mysql_Mysqli_Count_Rows - Fatal编程技术网

Php MYSQLI行计算太慢

Php MYSQLI行计算太慢,php,mysql,mysqli,count,rows,Php,Mysql,Mysqli,Count,Rows,我正在用MYSQLI的SQL\u CALC\u FOUND\u行计算表中的访问者,但速度很慢。超过15秒。我该怎么做才能让它更快? 我必须提到,我的表是MyISAM,总共有200多万行 if ($result = $mysqli->query("SELECT SQL_CALC_FOUND_ROWS * FROM visitors_table WHERE visitor_affiliate= 'first user'")) { /* determine number of rows

我正在用MYSQLI的SQL\u CALC\u FOUND\u行计算表中的访问者,但速度很慢。超过15秒。我该怎么做才能让它更快? 我必须提到,我的表是MyISAM,总共有200多万行

if ($result = $mysqli->query("SELECT SQL_CALC_FOUND_ROWS *  FROM visitors_table WHERE visitor_affiliate= 'first user'")) {
    /* determine number of rows result set */

     $row_cnt = $result->num_rows;

    printf("Result set has %d rows.\n", $row_cnt);

    /* close result set */
    $result->close();
}


 if ($result2 = $mysqli->query("SELECT SQL_CALC_FOUND_ROWS *  FROM visitors_table WHERE visitor_affiliate= 'seconduser'")) {
        /* determine number of rows result set */

         $row_cnt2 = $result2->num_rows;

        printf("Result set has %d rows.\n", $row_cnt2);

        /* close result set */
        $result2->close();
    }

 if ($result3 = $mysqli->query("SELECT SQL_CALC_FOUND_ROWS *  FROM visitors_table WHERE visitor_affiliate= 'thirduser'")) {
        /* determine number of rows result set */

         $row_cnt3 = $result3->num_rows;

        printf("Result set has %d rows.\n", $row_cnt3);

        /* close result set */
        $result2->close();
    }
谢谢你的帮助。。
提前谢谢

确保在“访问者”列上有索引:

CREATE INDEX aff_index ON visitors_table(visitor_affiliate) ;
然后,您需要一个查询来获得三个结果:

SELECT visitor_affiliate, COUNT(*) AS tot FROM visitors_table 
 WHERE visitor_affiliate IN ( 'firstuser',  'seconduser',  'thirduser')
 GROUP BY visitor_affiliate 
它通常比三个查询更快

如果仍然需要单独的查询:

SELECT COUNT(*) AS tot FROM visitors_table 
WHERE visitor_affiliate IN = 'firstuser'

@anantkumarsingh SQL\u CALC\u FOUND\u ROWS不是一个columnYikes!MyISAM和200万行是痛苦的。您的
visitors\u表
列是某种索引吗?@anantkumarsingh这是一个@Machavity我有一个自动递增的列ID…我还选中了SELECT ID。但是也太慢了…对不起。那次我突然想起了。谢谢试着像
SELECT COUNT(*)
和check我做了索引…如何在查询后为每个用户获取数字?从访问者中选择COUNT(*)AS tot\u table='Name'Hi Tim。。我不明白..能给我一个上面已经有的例子中的php例子吗?谢谢。。很抱歉问了这么多问题