简体   繁体   English

如何使用特定的日期格式简化和减少我的SQL查询的执行时间?

[英]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.

相关问题 如何减少此代码的执行时间 - How can i reduce Execution Time for this code 如何减少for循环中需要花费大量时间执行的迭代? - How can I reduce iterations in for loop that takes to much time for execution? 如何减少斯坦福解析器的执行时间? - How can I reduce Stanford parser time of execution? 如何减少此循环的执行时间(java) - How can I reduce the execution time of this loop (java) 如何使用以字符串格式存储的日期和时间在 Rooms 数据库中按查询排序? - How can I ORDER BY a query in Rooms database using date and time which is stored in String format? 如何减少代码中的 Lambda 冷启动时间? - How can I reduce the Lambda Cold Start time in my code? 在春季和休眠期从数据库获取数据时,如何以正确的格式获取日期时间? - How can I get my date time in correct format while getting from database in spring and hibernate? 如何创建具有特定格式的 Date 对象 - How can I create a Date object with a specific format 如何获得默认的日期和时间格式模式 - How can I get default Date and Time format pattern 如何自定义Joda Time日期格式的数字格式? - How can I customise the number format for Joda Time date formatting?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM