我有这个 SQL 查询,它每 5 秒查询一次数据库,以确定谁当前正在积极使用该软件。活动用户在过去 10 秒内对服务器进行了 ping 操作。(该表会根据用户活动正确更新,并且我有一个线程在会话超时时驱逐条目,一切正常)。
我正在寻找一种更有效/更快的方法来执行此操作,因为它被频繁调用,大约每 5 秒一次。此外,数据库中最多可能有 500 个用户。该语言是Java,但问题确实与任何语言有关。
List<String> r = new ArrayList<String>();
Calendar c = Calendar.getInstance();
long threshold = c.get(Calendar.SECOND) + c.get(Calendar.MINUTE)*60 + c.get(Calendar.HOUR_OF_DAY)*60*60 - 10;
String tmpSql = "SELECT user_name, EXTRACT(HOUR FROM last_access_ts) as hour, EXTRACT(MINUTE FROM last_access_ts) as minute, EXTRACT(SECOND FROM last_access_ts) as second FROM user_sessions";
DBResult rs = DB.select(tmpSql);
for (int i=0; i<rs.size(); i++)
{
Map<String, Object> result = rs.get(i);
long hour = (Long)result.get("hour");
long minute = (Long)result.get("minute");
long second = (Long)result.get("second");
if (hour*60*60 + minute*60 + second > threshold)
r.add(result.get("user_name").toString());
}
return r;