SELECT user, LEFT( host, ( LOCATE( ':', host ) - 1 ) ) as host, count(*) as count
FROM `information_schema`.processlist where Command = 'Sleep'
GROUP BY user, LEFT( host, ( LOCATE( ':', host ) - 1 ) )
ORDER BY count DESC
Explanation of the functions used:
MySQL LOCATE() returns the position of the first occurrence of a string within a string.
MySQL LEFT() returns a specified number of characters from the left of the string. Both the number and the string are supplied as arguments of the function.
Found this query from Ben Nadel:
https://www.bennadel.com/blog/3054-grouping-the-mysql-processlist-by-ip-address-to-view-connection-counts.htm
SELECT tmp.ipAddress, -- Calculate how many connections are being held by this IP address. COUNT( * ) AS ipAddressCount, -- For each connection, the TIME column represent how many SECONDS it has been in -- its current state. Running some aggregates will give us a fuzzy picture of what -- the connections from this IP address is doing. FLOOR( AVG( tmp.time ) ) AS timeAVG, MAX( tmp.time ) AS timeMAX FROM -- Let's create an intermediary table that includes an additional column representing -- the client IP address without the port. ( SELECT -- We don't actually need all of these columns for the demo. But, I'm -- including them here to demonstrate what fields COULD be used in the -- processlist system. pl.id, pl.user, pl.host, pl.db, pl.command, pl.time, pl.state, pl.info, -- The host column is in the format of "IP:PORT". We want to strip off -- the port number so that we can group the results by the IP alone. LEFT( pl.host, ( LOCATE( ':', pl.host ) - 1 ) ) AS ipAddress FROM INFORMATION_SCHEMA.PROCESSLIST pl ) AS tmp GROUP BY tmp.ipAddress ORDER BY ipAddressCount DESC
No comments:
Post a Comment