我做了一个选择 的查询last secods
,例如:如果现在是 10:30:30s 并且我传递了last 20 seconds
它应该只返回和之间的值10:30:10
,10:30:30
所以这里是:
public List<Log> listLastSeconds(Integer id_point, int seconds) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, -seconds);
Date today = new Date();
Date lastSeconds = calendar.getTime();
Query query = em.createQuery("SELECT l FROM Log l WHERE l.point.id =:id AND l.time >= :lastSeconds AND l.time <= :today", Log.class);
query.setParameter("id", id_point);
query.setParameter("lastSeconds", lastSeconds);
query.setParameter("today", today);
return query.getResultList();
}
但是即使对于很小的差异(例如 10 秒),此查询也需要很长时间。
编辑:
CREATE TABLE `log` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_point` int(11) NOT NULL,
`value` varchar(10) NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `fk_point_id` (`id_point`),
CONSTRAINT `fk_point_id` FOREIGN KEY (`id_point`) REFERENCES `point` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=9360244 DEFAULT CHARSET=latin2$$
如何改进此查询以更快?