简体   繁体   中英

MySQL query to identify concurrent access/usage

I am trying get a rough idea of a server's past load; I would like to utilize a log table to estimate concurrent access but I'm getting stuck with building the query. The table is as follows:

FYI: I didn't design/create this table, took over an ugly database :(

CREATE TABLE `user_access_log` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `oem_id` varchar(50) NOT NULL,
  `add_date` datetime NOT NULL,
  `username` varchar(36) NOT NULL,
  `site_id` int(5) NOT NULL,
  `card_id` int(6) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1$$

Table data:

---------------------------------------------------------------------------
| id        | add_date            | username          | site_id | card_id |
---------------------------------------------------------------------------
| 185818901 | 2012-12-25 03:01:04 | 1944E9745A9CE91   | 4       | 27273   |
| 185818900 | 2012-12-25 03:01:04 | EA5902C8115C9FF   | 1       | 27238   |
---------------------------------------------------------------------------

This may be far fetched, or perhaps illogical... but, I am trying to count unique usernames for each second from add_date (keeping in mind the data spans over months).

Any thoughts?

Thanks, Kate

Sounds like you just need to use COUNT and DATEFORMAT :

select DATE_FORMAT(add_date, '%Y-%m-%d %H:%i:%s'), 
    count(distinct username) usercount
from player_user_video
group by DATE_FORMAT(add_date, '%Y-%m-%d %H:%i:%s')

SQL Fiddle Demo

Depending on how your data is stored, you may not need to use DATEFORMAT . But this will return you a count of distinct usernames grouped by the date, hour, minute and second. I assume that's what you're trying to accomplish. If you are trying to figure out which second or hour has the most traffic, you can use SECOND(add_date) for example. Depends on your exact needs (the Fiddle includes both examples).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM