[英]How can I simplify and reduce execution time of my sql query with specific date format?
I am using jdbcTemplate with following query and code 我正在使用jdbcTemplate以及以下查询和代码
String sql = select count(*) as count ,trunc(fin.fin_date) as dateAndTime from (select * from rec where rec_status='RECEIVED' and fin_date>sysdate-8) fin group by trunc(fin.fin_date)
try {
return jdbcTemplate.query(sql, new RowMapper<Chrt>() {
@Override
public Chrt mapRow(ResultSet rs, int rowNum) throws SQLException {
Chrt receive = new Chrt();
java.util.Date date =(rs.getDate("dateAndTime"));
receive.setDate(date.toString());
receive.setCount(rs.getInt("count"));
return receive;
}
});
} catch (DataAccessException e) {
return null;
}
}
It returns me date in a format like 2016-05-19 but I need the date like 19-MAY-16. 它以2016-05-19的格式返回日期,但我需要像19-MAY-16这样的日期。 Is it possible to get this format without increasing execution time?
是否可以在不增加执行时间的情况下获得此格式?
How can I do this? 我怎样才能做到这一点?
Instead of the trunc
function, you can use the TO_CHAR
function. 您可以使用
TO_CHAR
函数代替trunc
函数。 It will give you the date in the specified format: 它将以指定的格式为您提供日期:
String sql = select count(*) as count ,TO_CHAR(fin.fin_date, 'DD-MON-YY') as dateAndTime from (select * from rec where rec_status='RECEIVED' and fin_date>sysdate-8) fin group by TO_CHAR(fin.fin_date, 'DD-MON-YY');
This will not impact performances significantly. 这不会对性能产生重大影响。
To increase performances, I suggest you to look at the query plan for the query. 为了提高性能,我建议您查看查询的查询计划。 To see this plan, just execute :
要查看此计划,只需执行:
EXPLAIN PLAN FOR
select count(*) as count ,TO_CHAR(fin.fin_date, 'DD-MON-YY') as dateAndTime from (select * from rec where rec_status='RECEIVED' and fin_date>sysdate-8) fin group by TO_CHAR(fin.fin_date, 'DD-MON-YY')
in any oracle sql client and read the doc to understand why it is too slow and how to improve performances. 在任何oracle sql客户端和阅读文档 ,了解为什么它太慢,如何提高性能。 (a blind guess : put an index on fin_date)
(一个盲目的猜测:在fin_date上放一个索引)
Why not use one query instead of two ? 为什么不使用一个查询而不是两个?
select count(*) as count ,TO_CHAR(fin.fin_date, 'DD-MON-YY') as dateAndTime
from rec
where rec_status='RECEIVED' and fin_date>sysdate-8
group by TO_CHAR(fin.fin_date, 'DD-MON-YY');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.