php
  • mysql
  • mysqli
  • count
  • rows
  • 2015-05-26 109 views 2 likes 
    2

    我想用SQL_CALC_FOUND_ROWS和MYSQLI來計算我的表上的訪問者,但它很慢。它超過15秒。我能做些什麼使它更快? 我不得不提到我的表是MyISAM,總共有超過200萬行。MYSQLI行計算太慢

    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(); 
        } 
    

    任何幫助表示讚賞.. 提前感謝!

    +0

    @anantkumarsingh SQL_CALC_FOUND_ROWS不是列 – jason88

    +0

    哎呀! MyISAM和2M行很痛苦。你的'visitors_table'列是某種索引嗎? – Machavity

    +0

    @anantkumarsingh這是一個[MySQL函數](https://dev.mysql.com/doc/refman/5.5/en/information-functions.html#function_found-rows) – Machavity

    回答

    0

    確保您使用的列上visitor_affiliate索引:

    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' 
    
    +0

    感謝蒂姆...... – jason88

    +0

    我做了索引...我怎麼才能得到每個用戶的查詢後的數字? – jason88

    +0

    SELECT COUNT(*)AS tot FROM visitors_table ='Name' – Tim3880

    相關問題