请优化这段代码select * from ( select order_bill_id , counterfoil_no , counterfoil_amt , acceptor , company_id , cert_no , company_name , rate , expiry_date , transaction_amt , create_date , update_date , partner_id , partner_name , order_status_code , order_status_msg , transaction_date , tenant_id , platform_id , channel_id , share_profit , settle , row_number()over(partition by order_bill_id order by update_date desc )rn from (select * from dwd_mec_adds_order_bill_inc where dt='2023-06-07' union all select * from dwd_mec_adds_order_bill_inc where dt='2023-06-06')t )r where rn=1
时间: 2024-03-16 11:41:20 浏览: 56
这段代码可以进行如下优化:
1. 使用`UNION ALL`的语句可以合并为一个子查询,避免了多次扫描表的开销。
2. 在子查询中使用`WHERE`条件过滤查询日期,避免了查询全表的开销。
3. 可以使用具体的列名代替`SELECT *`,避免了不必要的数据传输开销。
综上所述,可以优化成如下代码:
```
SELECT order_bill_id, counterfoil_no, counterfoil_amt, acceptor, company_id, cert_no, company_name, rate, expiry_date, transaction_amt, create_date, update_date, partner_id, partner_name, order_status_code, order_status_msg, transaction_date, tenant_id, platform_id, channel_id, share_profit, settle
FROM (
SELECT order_bill_id, counterfoil_no, counterfoil_amt, acceptor, company_id, cert_no, company_name, rate, expiry_date, transaction_amt, create_date, update_date, partner_id, partner_name, order_status_code, order_status_msg, transaction_date, tenant_id, platform_id, channel_id, share_profit, settle, ROW_NUMBER() OVER (PARTITION BY order_bill_id ORDER BY update_date DESC) rn
FROM dwd_mec_adds_order_bill_inc
WHERE dt IN ('2023-06-06', '2023-06-07')
) t
WHERE rn = 1;
```
这样可以使代码更加简洁,同时也可以提高查询效率。
阅读全文