简体   繁体   中英

How to make hibernate query faster

I am currently working on a task to improve performance of an action and the slowest part of code is this hibernate query:

.createQuery( "SELECT distinct p FROM CSParcel p " +
                              " WHERE p.bag.destination.loop.line.changeover.barcode = :barcode AND p.created > :limit " +
                              " AND (p.bag.destination.loop.courierLoop.id = :courierId OR p.bag.destination.loop.line.courier.id = :courierId)" +
                              " AND p.latestAction.actionType <> :doruceni" + " AND p.id NOT IN " +
                              "(SELECT pa.parcel.id " + "   FROM CSParcelAction pa " +
                              " WHERE pa.actionType = :predani AND pa.courier.id = :courierId AND pa.timestamp > :limit" +
                              ")", CSParcel.class )
                .setParameter( "barcode", changeoverBarcode.toUpperCase() )
                .setParameter( "limit", Time.setTime( 0, 0, 0, cal.getTime() ) )
                .setParameter( "courierId", courierId )
                .setParameter( "predani", CSParcelActionType.CHANGEOVER )
                .setParameter( "doruceni", CSParcelActionType.DELIVERY )
                .getResultList();

are there any ways to make it run faster?

Thank you for every suggestion.

You can monitor with some tools and you will see how queries behave, normally adding some indices where is needed is enough, if you databases alredy have indices you can do some kind of maintenance to tables you use. Here is some information

enter link description here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM