0

Please how can I optimize this query?

FROM OrderEntity WHERE id IN (SELECT DISTINCT orderId FROM ProductEntity 
  WHERE code = :myCode OR extCode = :myCode)

This query is too slow for the tables which has around million rows

Order 1 ... * Product

Thank you for help

EDIT

In pure sql I would use this query which is fast, but I don't know how to form it in HQL:

SELECT * FROM order o, (SELECT DISTINCT order_id FROM product WHERE code = 'ABC' 
   OR ext_code = 'ABC') p WHERE o.id = p.order_id
4

1 回答 1

0

我猜 ProductEntity 可以与 OrderEntity (Foreign key order_id) 的关联进行映射。

如果为真,请尝试对其进行映射并测试此查询(内连接可能更快):

SELECT o 
FROM
ProductEntity p 
INNER JOIN p.orderEntity o 
WHERE p.code = :myCode OR p.extCode = :myCode
于 2013-04-18T09:04:45.627 回答