SELECT t1.* FROM awb t1 INNER JOIN awb t2 ON t1.HAWB_NO = t2.HAWB_NO2 OR t1.HAWB_NO = t2.AGENT_NO WHERE t1.HAWB_NO = t2.HAWB_NO and t1.ARR_DATE>getdate()-350语句优化
时间: 2024-02-25 11:52:24 浏览: 75
这个 SQL 查询语句已经比较简单,没有太多可以进行优化的地方。以下是一些可能的优化点:
1. 索引:如果 `awb` 表中的 `HAWB_NO`、`HAWB_NO2` 和 `AGENT_NO` 字段没有建立索引,可以考虑为这三个字段建立索引,以提高查询效率。
2. 子查询:如果 `t1.ARR_DATE > getdate() - 350` 这个条件过于复杂,可以将其转化为子查询,如下所示:
```
SELECT t1.*
FROM awb t1
INNER JOIN awb t2
ON t1.HAWB_NO = t2.HAWB_NO2 OR t1.HAWB_NO = t2.AGENT_NO
WHERE t1.HAWB_NO = t2.HAWB_NO
AND t1.ARR_DATE > (SELECT DATEADD(day, -350, GETDATE()))
```
这样做可以使 SQL 查询更加清晰,便于维护和优化。
3. 数据库分区:如果 `awb` 表中的数据量非常大,可以考虑将其进行分区,以提高查询效率。分区可以根据时间、地域等因素进行,具体的分区策略需要根据实际情况进行设计和调整。
需要注意的是,以上优化措施并不一定都适用于所有场景,需要根据具体情况进行评估和调整。
相关问题
SELECT AWB_ID,HAWB_NO,TADDRESS FROM AWB WHERE TRANS_ID IN (SELECT COMP_ID FROM COMPANY WHERE COMP_CODE IN('FEDJ','FPAK')) AND AGENT_ID in(select comp_id from company where BILLSEVERTYPE=1) and ARR_DATE>'2023-05-01' and isnull(REGION,'')='Y' and PAY_SET='Y' and isnull(TADDRESS,'')!='' and (select count(*) from REMARK where awb_id=awb.AWB_ID and REMARK like '%修改出货服务商%' and CONVERT(char(10),GETDATE(),120)=CONVERT(char(10),MODI_DATE,120))>=1 语句优化
可以尝试以下优化方法:
1. 使用JOIN代替IN和子查询
```
SELECT AWB.AWB_ID, AWB.HAWB_NO, AWB.TADDRESS
FROM AWB
JOIN COMPANY ON AWB.TRANS_ID = COMPANY.COMP_ID
JOIN COMPANY AS AGENT ON AWB.AGENT_ID = AGENT.COMP_ID
LEFT JOIN REMARK ON AWB.AWB_ID = REMARK.AWB_ID
WHERE COMPANY.COMP_CODE IN ('FEDJ', 'FPAK')
AND AGENT.BILLSEVERTYPE = 1
AND AWB.ARR_DATE > '2023-05-01'
AND ISNULL(AWB.REGION, '') = 'Y'
AND AWB.PAY_SET = 'Y'
AND ISNULL(AWB.TADDRESS, '') != ''
AND REMARK.REMARK LIKE '%修改出货服务商%'
AND CONVERT(char(10), GETDATE(), 120) = CONVERT(char(10), REMARK.MODI_DATE, 120)
```
2. 使用索引
为了使查询更快,可以在AWB表上创建以下联合索引:
```
CREATE INDEX idx_AWB ON AWB (TRANS_ID, AGENT_ID, ARR_DATE, REGION, PAY_SET, TADDRESS)
```
为了支持LEFT JOIN REMARK,可以在REMARK表上创建以下索引:
```
CREATE INDEX idx_REMARK ON REMARK (AWB_ID, REMARK, MODI_DATE)
```
3. 避免使用函数
在WHERE子句中使用函数会影响查询性能。如果可能的话,可以将查询条件中的函数转换为变量或常量。
例如,将以下条件:
```
CONVERT(char(10), GETDATE(), 120) = CONVERT(char(10), REMARK.MODI_DATE, 120)
```
转换为:
```
@today = CONVERT(char(10), GETDATE(), 120)
...
AND @today = CONVERT(char(10), REMARK.MODI_DATE, 120)
```
SELECT PIS.SHOW_FLT_DETAIL AS SHOW_FLT_DETAIL -- new , PIS.SHOW_AWB_DETAIL AS SHOW_AWB_DETAIL -- new , PIS.DISPLAY_AIRLINE_CODE AS CARRIER_CODE , DECODE(PIS.REVERT_FLOW,'N',PIS.FLOW_TYPE,DECODE(PIS.FLOW_TYPE,'I','E','I')) AS FLOW_TYPE , PIS.SHIP_TO_LOCATION AS SHIP_TO_LOCATION , PIS.INVOICE_SEQUENCE AS INVOICE_SEQUENCE , PFT.FLIGHT_DATE AS FLIGHT_DATE , PFT.FLIGHT_CARRIER_CODE AS FLIGHT_CARRIER_CODE , PFT.FLIGHT_SERIAL_NUMBER AS FLIGHT_SERIAL_NUMBER , PFT.FLOW_TYPE AS AIRCRAFT_FLOW , FAST.AIRCRAFT_SERVICE_TYPE AS AIRCRAFT_SERVICE_TYPE , PPT.AWB_NUMBER AS AWB_NUMBER , PPT.WEIGHT AS WEIGHT , PPT.CARGO_HANDLING_OPERATOR AS CARGO_HANDLING_OPERATOR , PPT.SHIPMENT_PACKING_TYPE AS SHIPMENT_PACKING_TYPE , PPT.SHIPMENT_FLOW_TYPE AS SHIPMENT_FLOW_TYPE , PPT.SHIPMENT_BUILD_TYPE AS SHIPMENT_BUILD_TYPE , PPT.SHIPMENT_CARGO_TYPE AS SHIPMENT_CARGO_TYPE , PPT.REVENUE_TYPE AS REVENUE_TYPE , PFT.JV_FLIGHT_CARRIER_CODE AS JV_FLIGHT_CARRIER_CODE , PPT.PORT_TONNAGE_UID AS PORT_TONNAGE_UID , PPT.AWB_UID AS AWB_UID , PIS.INVOICE_SEPARATION_UID AS INVOICE_SEPARATION_UID , PFT.FLIGHT_TONNAGE_UID AS FLIGHT_TONNAGE_UID FROM PN_FLT_TONNAGES PFT , FZ_AIRLINES FA , PN_TONNAGE_FLT_PORTS PTFP , PN_PORT_TONNAGES PPT , FF_AIRCRAFT_SERVICE_TYPES FAST , SR_PN_INVOICE_SEPARATIONS PIS --new , SR_PN_INVOICE_SEP_DETAILS PISD--new , SR_PN_INV_SEP_PORT_TONNAGES PISPT --new WHERE PFT.FLIGHT_OPERATION_DATE >= trunc( CASE :rundate WHEN TO_DATE('01/01/1900', 'DD/MM/YYYY') THEN ADD_MONTHS(SYSDATE,-1) ELSE ADD_MONTHS(:rundate,-1) END, 'MON') AND PFT.FLIGHT_OPERATION_DATE < trunc( CASE :rundate WHEN TO_DATE('01/01/1900', 'DD/MM/YYYY') THEN TRUNC(SYSDATE) ELSE TRUNC(:rundate) END, 'MON') AND PFT.TYPE IN ('C', 'F') AND PFT.RECORD_TYPE = 'M' AND (PFT.TERMINAL_OPERATOR NOT IN ('X', 'A') OR (PFT.TERMINAL_OPERATOR <> 'X' AND FA.CARRIER_CODE IN (SELECT * FROM SPECIAL_HANDLING_AIRLINE) AND PPT.REVENUE_TYPE IN (SELECT * FROM SPECIAL_REVENUE_TYPE) AND PPT.SHIPMENT_FLOW_TYPE IN (SELECT * FROM SPECIAL_SHIPMENT_FLOW_TYPE) AND PFT.FLIGHT_OPERATION_DATE >= (select EFF_DATE from SPECIAL_HANDLING_EFF_DATE) )) AND PFT.DELETING_DATETIME IS NULL AND FA.AIRLINE_UID = PFT.AIRLINE_UID AND FA.DELETING_DATETIME IS NULL AND PTFP.FLIGHT_TONNAGE_UID = PFT.FLIGHT_TONNAGE_UID AND PTFP.RECORD_TYPE = 'M' AND PTFP.DELETING_DATETIME IS NULL AND PPT.TONNAGE_FLIGHT_PORT_UID (+)= PTFP.TONNAGE_FLIGHT_PORT_UID AND PPT.RECORD_TYPE (+)= 'M' AND PPT.DISCREPANCY_TYPE (+)= 'NONE' AND PPT.ADJUSTMENT_INC_FLAG (+)= 'Y' AND PPT.DELETING_DATETIME (+) IS NULL AND FAST.AIRCRAFT_SERVICE_TYPE_UID = PFT.AIRCRAFT_SERVICE_TYPE_UID AND FAST.DELETING_DATETIME IS NULL AND PIS.TEMPORAL_NAME = TO_CHAR((CASE :rundate --new WHEN TO_DATE('01/01/1900', 'DD/MM/YYYY') THEN TRUNC(SYSDATE) ELSE TRUNC(:rundate) END ), 'YYYYMM') || '00' AND PIS.INVOICE_SEPARATION_UID = PISD.INVOICE_SEPARATION_UID --new AND PISD.INVOICE_SEP_DETAIL_UID = PISPT.INVOICE_SEP_DETAIL_UID --new AND PISPT.PORT_TONNAGE_UID = PPT.PORT_TONNAGE_UID --new AND PIS.PRINT_SUPPORTING_DOC = 'Y';上面是oracle的写法,请转成spark SQL的写法。
以下是转换为 Spark SQL 查询的代码:
```
SELECT
PIS.SHOW_FLT_DETAIL AS SHOW_FLT_DETAIL,
PIS.SHOW_AWB_DETAIL AS SHOW_AWB_DETAIL,
PIS.DISPLAY_AIRLINE_CODE AS CARRIER_CODE,
CASE
WHEN PIS.REVERT_FLOW = 'N' THEN PIS.FLOW_TYPE
ELSE CASE
WHEN PIS.FLOW_TYPE = 'I' THEN 'E'
ELSE 'I'
END
END AS FLOW_TYPE,
PIS.SHIP_TO_LOCATION AS SHIP_TO_LOCATION,
PIS.INVOICE_SEQUENCE AS INVOICE_SEQUENCE,
PFT.FLIGHT_DATE AS FLIGHT_DATE,
PFT.FLIGHT_CARRIER_CODE AS FLIGHT_CARRIER_CODE,
PFT.FLIGHT_SERIAL_NUMBER AS FLIGHT_SERIAL_NUMBER,
PFT.FLOW_TYPE AS AIRCRAFT_FLOW,
FAST.AIRCRAFT_SERVICE_TYPE AS AIRCRAFT_SERVICE_TYPE,
PPT.AWB_NUMBER AS AWB_NUMBER,
PPT.WEIGHT AS WEIGHT,
PPT.CARGO_HANDLING_OPERATOR AS CARGO_HANDLING_OPERATOR,
PPT.SHIPMENT_PACKING_TYPE AS SHIPMENT_PACKING_TYPE,
PPT.SHIPMENT_FLOW_TYPE AS SHIPMENT_FLOW_TYPE,
PPT.SHIPMENT_BUILD_TYPE AS SHIPMENT_BUILD_TYPE,
PPT.SHIPMENT_CARGO_TYPE AS SHIPMENT_CARGO_TYPE,
PPT.REVENUE_TYPE AS REVENUE_TYPE,
PFT.JV_FLIGHT_CARRIER_CODE AS JV_FLIGHT_CARRIER_CODE,
PPT.PORT_TONNAGE_UID AS PORT_TONNAGE_UID,
PPT.AWB_UID AS AWB_UID,
PIS.INVOICE_SEPARATION_UID AS INVOICE_SEPARATION_UID,
PFT.FLIGHT_TONNAGE_UID AS FLIGHT_TONNAGE_UID
FROM
PN_FLT_TONNAGES PFT
JOIN FZ_AIRLINES FA ON FA.AIRLINE_UID = PFT.AIRLINE_UID AND FA.DELETING_DATETIME IS NULL
JOIN PN_TONNAGE_FLT_PORTS PTFP ON PTFP.FLIGHT_TONNAGE_UID = PFT.FLIGHT_TONNAGE_UID AND PTFP.RECORD_TYPE = 'M' AND PTFP.DELETING_DATETIME IS NULL
LEFT JOIN PN_PORT_TONNAGES PPT ON PPT.TONNAGE_FLIGHT_PORT_UID = PTFP.TONNAGE_FLIGHT_PORT_UID AND PPT.RECORD_TYPE = 'M' AND PPT.DISCREPANCY_TYPE = 'NONE' AND PPT.ADJUSTMENT_INC_FLAG = 'Y' AND PPT.DELETING_DATETIME IS NULL
JOIN FF_AIRCRAFT_SERVICE_TYPES FAST ON FAST.AIRCRAFT_SERVICE_TYPE_UID = PFT.AIRCRAFT_SERVICE_TYPE_UID AND FAST.DELETING_DATETIME IS NULL
JOIN SR_PN_INVOICE_SEPARATIONS PIS ON PIS.TEMPORAL_NAME = CONCAT(YEAR(:rundate), RIGHT(CONCAT('0', MONTH(:rundate)), 2), '00') AND PIS.INVOICE_SEPARATION_UID = PISD.INVOICE_SEPARATION_UID AND PIS.PRINT_SUPPORTING_DOC = 'Y'
JOIN SR_PN_INVOICE_SEP_DETAILS PISD ON PISD.INVOICE_SEP_DETAIL_UID = PISPT.INVOICE_SEP_DETAIL_UID
JOIN SR_PN_INV_SEP_PORT_TONNAGES PISPT ON PISPT.PORT_TONNAGE_UID = PPT.PORT_TONNAGE_UID
WHERE
PFT.FLIGHT_OPERATION_DATE >= trunc(CASE
WHEN :rundate = TO_DATE('01/01/1900', 'DD/MM/YYYY') THEN ADD_MONTHS(SYSDATE, -1)
ELSE ADD_MONTHS(:rundate, -1)
END, 'MON')
AND PFT.FLIGHT_OPERATION_DATE < trunc(CASE
WHEN :rundate = TO_DATE('01/01/1900', 'DD/MM/YYYY') THEN TRUNC(SYSDATE)
ELSE TRUNC(:rundate)
END, 'MON')
AND PFT.TYPE IN ('C', 'F')
AND PFT.RECORD_TYPE = 'M'
AND (
PFT.TERMINAL_OPERATOR NOT IN ('X', 'A')
OR (
PFT.TERMINAL_OPERATOR <> 'X'
AND FA.CARRIER_CODE IN (SELECT * FROM SPECIAL_HANDLING_AIRLINE)
AND PPT.REVENUE_TYPE IN (SELECT * FROM SPECIAL_REVENUE_TYPE)
AND PPT.SHIPMENT_FLOW_TYPE IN (SELECT * FROM SPECIAL_SHIPMENT_FLOW_TYPE)
AND PFT.FLIGHT_OPERATION_DATE >= (SELECT EFF_DATE FROM SPECIAL_HANDLING_EFF_DATE)
)
)
AND PFT.DELETING_DATETIME IS NULL;
```
请注意,需要将 `TRUNC` 和 `CONCAT` 函数替换为 Spark SQL 中的相应函数,并使用 `JOIN` 语句替换 `LEFT JOIN` 语句。还需要更改零散的子查询,以便在 Spark SQL 中使用。
阅读全文