使用DISTINCT和rownum获取记录

时间:2013-07-17 10:41:17

标签: sql performance

我在执行以下查询时遇到了性能问题。花了太多时间来获取结果。它一次获取整个结果(比如3000)。我试图使用rownum <11获取10条记录。但它只显示3-4条记录。重复header_ids存在。我们可以在这里使用DISTINCT和rownum获得10行。有没有其他方法可以解决这个问题?

SELECT DISTINCT oh.header_id,
oh.cust_po_number purchase_order,
oh.order_number,
DECODE(oh.orig_sys_document_ref, NULL, oh.orig_sys_document_ref, SUBSTR(oh.orig_sys_document_ref, LENGTH('OE_ORDER_HEADERS_ALL')+1)) web_reference_number,
TO_CHAR(oh.ordered_date, 'FMMon-DD-YYYY') date_ordered,
(SELECT MIN(schedule_ship_date)
FROM oe_order_lines_all
WHERE header_id = oh.header_id
And Oh.Sold_To_Org_Id = 12338
) oldest_schedule_ship_date,
oe_totals_grp.get_order_total(ol.header_id,NULL,'ALL') total_value,
oh.transactional_curr_code currency_code,
COUNT(*) over() AS total_count
FROM oe_order_headers_all oh,
oe_order_lines_all ol
Where Oh.Header_Id = Ol.Header_Id
-- AND ol.actual_shipment_date BETWEEN sysdate - 180 AND sysdate
AND oh.sold_to_org_id = 12338

1 个答案:

答案 0 :(得分:3)

如果您使用的是rownum,那么我认为您使用的是Oracle。

在应用distinct后,而不是之前,Oracle会执行rownum 。唉。我认为这也是后来应用order by的逻辑的一部分。

您可以使用子查询修复它:

select s.*
from (<your query here>) s
where rownum < 11;

部分性能问题可能是由select中的子查询引起的。我认为你可以用分析函数替换它:

SELECT DISTINCT oh.header_id,
      oh.cust_po_number purchase_order, oh.order_number,
      DECODE(oh.orig_sys_document_ref, NULL, oh.orig_sys_document_ref,
      SUBSTR(oh.orig_sys_document_ref, LENGTH('OE_ORDER_HEADERS_ALL')+1)) web_reference_number,
      TO_CHAR(oh.ordered_date, 'FMMon-DD-YYYY') date_ordered,
      min(schedule_ship_date) over (partition by oh.header_id) as oldest_schedule_ship_date,
      oe_totals_grp.get_order_total(ol.header_id,NULL,'ALL') total_value,
      oh.transactional_curr_code currency_code,
      COUNT(*) over() AS total_count
FROM oe_order_headers_all oh join
     oe_order_lines_all ol
     on Oh.Header_Id = Ol.Header_Id

- 和ol.actual_shipment_date BETWEEN sysdate - 180 AND sysdate     oh.sold_to_org_id = 12338

实际上,对我来说,整个查询看起来应该使用group by而不是distinct来编写分析函数。这也可能对表现有所帮助。