Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP and MySQL stats system

What is the best database model to store user visits and count unique users using the IP in a big database with 1.000.000 rows for example?

SELECT COUNT(DISTINCT ip) FROM visits

But with 1.000.000 diferent ip's it can be a slow query. Caching will not return the real number.

How big stats systems counts uniques visits?

like image 639
Wiliam Avatar asked Sep 11 '26 13:09

Wiliam


1 Answers

Have another MyISAM table with only IP column and UNIQUE index on it. You'll get the proper count in no time (MyISAM caches number of rows in table)

[added after comments]

If you also need to count visits from each IP, add one more column visitCount and use

INSERT INTO 
  visitCounter (IP,visitCount) 
VALUES 
  (INET_ATON($ip),1) 
ON DUPLICATE KEY UPDATE 
  SET visitCount = visitCount+1
like image 137
Mchl Avatar answered Sep 13 '26 03:09

Mchl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!