简体   繁体   中英

change not in subquery to left outer join in HQL

I have the following HQL with me

select distinct(ordr) 
from OfOrder ordr, 
SalesChannel sc  
where  
   ordr.status in (:statusList) and 
   ordr.ofOrderId not in (
          select ordrLabel2.ofOrder.ofOrderId 
          from OrderLabel ordrLabel2 
          where 
             ordrLabel2.ofLabel.labelId in ( :ofLabels )
          ) and 
  ordr.orderMain.salesChannel.channelId in (:channelIds)  and 
  ordr.deliveryChannel.deliveryChannelId in (:deliveryChannels) 
order by ordr.deliveryDate asc,   
CASE   
  when ordr.status = 'SU' then 1 
  when ordr.status = 'SU' then 1 
  when ordr.status = 'SU' then 1 
  when ordr.status = 'AC' then 2 
  when ordr.status = 'AC' then 2 
  when ordr.status = 'AC' then 2 
  when ordr.status = 'AC' then 3 
  when ordr.status = 'AC' then 4 
  when ordr.status = 'AC' then 4 
  when ordr.status = 'SH' then 5 
  when ordr.status = 'SH' then 5 
  when ordr.status = 'DD' then 6 
  when ordr.status = 'R' then 7 
  when ordr.status = 'C' then 8 
  ELSE 9999    
END

Hibernate generated the following query for the same

select distinct oforder0_.`of_order_id`
as of_order1_27_, oforder0_.`assigned_to`
as assigned2_27_, oforder0_.`box_size`
as box_size3_27_, oforder0_.`boxes`
as boxes4_27_, oforder0_.`created_date`
as created_5_27_, oforder0_.`delivery_channel_id`
as deliver25_27_, oforder0_.`delivery_channel_order_id`
as delivery6_27_, oforder0_.`delivery_date`
as delivery7_27_, oforder0_.`delivery_labelled`
as delivery8_27_, oforder0_.`delivery_status`
as delivery9_27_, oforder0_.`description`
as descrip10_27_, oforder0_.`display_name`
as display11_27_, oforder0_.`fulfillment_center_id`
as fulfill26_27_, oforder0_.`kitchen_labelled`
as kitchen12_27_, oforder0_.`message`
as message13_27_, oforder0_.`group_id`
as group_i27_27_, oforder0_.`shipping_address_id`
as shippin28_27_, oforder0_.`order_main_id`
as order_m29_27_, oforder0_.`order_type`
as order_t14_27_, oforder0_.`packing_priority`
as packing15_27_, oforder0_.`quantity`
as quantit16_27_, oforder0_.`requiresShipping`
as require17_27_, oforder0_.`shipping_line_id`
as shippin30_27_, oforder0_.`split_number`
as split_n18_27_, oforder0_.`status`
as status19_27_, oforder0_.`total_price`
as total_p20_27_, oforder0_.`tracking_id`
as trackin21_27_, oforder0_.`updated_date`
as updated22_27_, oforder0_.`vendor_id`
as vendor_23_27_, oforder0_.`vendor_name`
as vendor_24_27_
from `of_order` oforder0_
cross join `sales_channel` saleschann1_
    cross join `order_main` ordermain3_
where oforder0_.`order_main_id`=ordermain3_.`order_main_id`
        and (oforder0_.`status` in ('SU' , 'AC'))
        and (oforder0_.`of_order_id`
        not in  (
select orderlabel2_.`of_order_id`
            from `order_label` orderlabel2_
            where orderlabel2_.`label_id`
            in (29 , 37)
            )
)
and (ordermain3_.`channel_id`
in (1 , 4)
            )
        and (oforder0_.`delivery_channel_id`
in (2 , 5)
            )
order by oforder0_.`delivery_date` asc,
    case
when oforder0_.`status`='SU' then 1
        when oforder0_.`status`='SU' then 1
        when oforder0_.`status`='SU' then 1
        when oforder0_.`status`='AC' then 2
        when oforder0_.`status`='AC' then 2
        when oforder0_.`status`='AC' then 2
        when oforder0_.`status`='AC' then 3
        when oforder0_.`status`='AC' then 4
        when oforder0_.`status`='AC' then 4
        when oforder0_.`status`='SH' then 5
        when oforder0_.`status`='SH' then 5
        when oforder0_.`status`='DD' then 6
        when oforder0_.`status`='R' then 7
        when oforder0_.`status`='C' then 8
        else 9999
end
limit 50, 50;

The above query gives me the exact result that I need. But I think join queries will improve the performance. Please correct me if not. How to do that I need to change the subquery part to left outer join

(select ordrLabel2.ofOrder.ofOrderId from OrderLabel ordrLabel2 where ordrLabel2.ofLabel.labelId in (:ofLabels )

How to to achieve the same result by outer join. I need a HQL for the above result with left outer join Some one please help me to achieve this

If you need performance, don't rely on hql/sql conversion, go for native queries and don't select all fields, but only one you really need.

Another strategy is to build a view on your data ( investigate on materialized view too ) and then hql / sql on that, in this way you can have a simpler data model, good performance and fine control on how to handle data on your database.

if it makes sense to do left join instead of cross join, you can chnage your query to this, also for your order by, you can simplify it as well:

select distinct(ordr) 
from OfOrder as ordr left join ordr.SalesChannel as sc  
where  
   ordr.status in (:statusList) 
   and ordr.ofOrderId not in (
          select ordrLabel2.ofOrder.ofOrderId 
          from OrderLabel ordrLabel2 
          where ordrLabel2.ofLabel.labelId in (:ofLabels )
          ) 
    and ordr.orderMain.salesChannel.channelId in (:channelIds)
    and ordr.deliveryChannel.deliveryChannelId in (:deliveryChannels) 
order by ordr.deliveryDate asc,   
CASE   
  when ordr.status = 'SU' then 1 
  when ordr.status = 'AC' then 2 
  when ordr.status = 'SH' then 3
  when ordr.status = 'DD' then 4 
  when ordr.status = 'R' then 5
  when ordr.status = 'C' then 6 
  ELSE 9999    
END

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