I spent almost a day trying to fetch bottom 10 rows from a table that has few hundred thousand rows in multiple ways. But timestamp in result set is always messed up by 7 hours (that is the time difference between UTC and my local)
Schema
CREATE TABLE IF NOT EXISTS xyz(
id timestamp NOT NULL,
name varchar(40) NOT NULL,
PRIMARY KEY (id,name )
);
Bottom 10 entries in db after running select * from xyz order by id desc limit 10;
2020-05-12 12:00:00+00
2020-05-12 12:00:00+00
2020-05-12 11:59:00+00
2020-05-12 11:58:00+00
2020-05-12 11:58:00+00
2020-05-12 11:58:00+00
2020-05-12 11:57:00+00
2020-05-12 11:56:00+00
2020-05-12 11:56:00+00
2020-05-12 11:55:00+00
I tried following 3 ways of selecting bottom 10 rows from DB. Also I always get null list if I select within a range.
List<xyz> findTop10ByOrderByIdDesc();
List<xyz> findTop10DistinctByIdBetweenOrderByIdDesc(LocalDateTime now,LocalDateTime after);
List<xyz> findTop10DistinctByIdBetweenOrderByIdDesc(Date now,Date after);
Here is POJO for holding the result set of above query
public class xyz{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private java.util.Date id;
//private LocalDateTime id
private String name;
// ...
}
but id
field is always off by 7 hours (for example: 1st element I get is 2020-05-12 05:00:00
) . a)How can I fix my code to get right timestamp in POJO (or result set). b) How can I retrieve all the entries within a range (I get null list now). I think both of them are related