Calculate variation of IP addresses column using MySQL

纵然是瞬间 提交于 2019-12-06 12:55:31

问题


I'm trying to detect people using proxies to abuse my website.

Often they will change proxies and so forth. But there is definitely a pattern of them using one proxy address many times. Much more than is normal for legitimate visitors.

Usually most accessing of my website is by unique ip addresses that have only visited once or a few times. Not repeatedly.

Let's say I have these ip addresses in a column:

89.46.74.56
89.46.74.56
89.46.74.56
91.14.37.249
104.233.103.6

That would mean there are 3 uniques out of 5. Giving a "uniqueness score" of 60%.

How would I calculate this efficiently using MySQL?


回答1:


plan

  • get count grouping by ip
  • divide by ( cross-joining ) the total rowcount
  • take maximum repeat ratio from above

setup

create table example
(
  id integer primary key auto_increment not null,
  ip varchar(13) not null
);

insert into example
( ip )
values
( '89.46.74.56'   ),
( '89.46.74.56'   ),
( '89.46.74.56'   ),
( '91.14.37.249'  ),
( '104.233.103.6' )
;

query

select max(repeat_factor)
from
(
select ip, count(*) / rc.row_count as repeat_factor
from example
cross join ( select count(*) as row_count from example ) rc
group by ip
) q
;

output

+--------------------+
| max(repeat_factor) |
+--------------------+
| 0.6                |
+--------------------+

sqlfiddle



来源:https://stackoverflow.com/questions/34545944/calculate-variation-of-ip-addresses-column-using-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!