问题
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