我有一个大约需要 4 分钟才能运行的查询,并且确实需要对此进行改进。我基本上是将数据旋转到列中。知道如何改进此查询吗?我主要加入索引。导致问题的是聚合
有没有更好的方法来像这样旋转数据?
SELECT
SKU.SKUIDX,
CTYPE_STYLE.ARRCZIDX ARRCZ,
MAX(IMPCOST.PURRATE) AS PURRATE,
MAX(IMPCOST.PURCURR) AS PURCURR,
MAX(CASE WHEN COSTFORM.DESCRIP = 'Purchase Cost' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS PURCHASE_COST,
MAX(CASE WHEN COSTFORM.DESCRIP = 'Trim Fee' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS TRIM_FEE,
MAX(CASE WHEN COSTFORM.DESCRIP = 'Hanger Fee' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS HANGER_FEE,
MAX(CASE WHEN COSTFORM.DESCRIP = 'Duty Rate' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS DUTY_RATE,
MAX(CASE WHEN COSTFORM.DESCRIP = 'Duty Fees' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS DUTY_FEE,
MAX(CASE WHEN COSTFORM.DESCRIP = 'Freight Fee' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS FREIGHT_FEE,
MAX(CASE WHEN COSTFORM.DESCRIP = 'Agent Rate' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS AGENT_RATE,
MAX(CASE WHEN COSTFORM.DESCRIP = 'Agent Fees' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS AGENT_FEE,
MAX(CASE WHEN COSTFORM.DESCRIP = 'Clearance Rate' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS CLEARANCE_RATE,
MAX(CASE WHEN COSTFORM.DESCRIP = 'Clearance Fee' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS CLEARANCE_FEE,
MAX(CASE WHEN COSTFORM.DESCRIP = 'Finance Rate' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS FINANCE_RATE,
MAX(CASE WHEN COSTFORM.DESCRIP = 'Finance Fee' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS FINANCE_FEE,
MAX(CASE WHEN COSTFORM.DESCRIP = 'Fabric Testing Rate' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS FABRIC_TESTING_RATE,
MAX(CASE WHEN COSTFORM.DESCRIP = 'Fabric Testing Fee' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS FABRIC_TESTING_FEE,
MAX(CASE WHEN COSTFORM.DESCRIP = 'PickPack Fee' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS PICK_PACK_FEE,
MAX(CASE WHEN COSTFORM.DESCRIP = 'Local Processing Fee' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS LOCAL_PROCESSING_FEE,
MAX(CASE WHEN COSTFORM.DESCRIP = 'Rework Fee' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS REWORK_FEE,
MAX(CASE WHEN COSTFORM.DESCRIP = 'Wastage Rate' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS WASTAGE_RATE,
MAX(CASE WHEN COSTFORM.DESCRIP = 'Wastage Fee' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS WASTAGE_FEE,
MAX(CASE WHEN COSTFORM.DESCRIP = 'Total Landed Cost' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS TOTAL_LANDED_COST,
MAX(CASE WHEN COSTFORM.DESCRIP = 'Wholesale Price' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS WHOLESALE_PRICE,
MAX(CASE WHEN COSTFORM.DESCRIP = 'First Margin%' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS FIRST_MARGIN,
MAX(CASE WHEN COSTFORM.DESCRIP = 'Royalty Rate' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS ROYALTY_RATE,
MAX(CASE WHEN COSTFORM.DESCRIP = 'Royalty Fee' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS ROYALTY_FEE,
MAX(CASE WHEN COSTFORM.DESCRIP = 'Second Margin%' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS SECOND_MARGIN,
MAX(CASE WHEN COSTFORM.DESCRIP = 'CODB %' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS CODB_RATE,
MAX(CASE WHEN COSTFORM.DESCRIP = 'CODB' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS CODB_FEE,
MAX(CASE WHEN COSTFORM.DESCRIP = 'Third Margin%' THEN CTYPE_COSTSHT.COST ELSE NULL END) AS THIRD_MARGIN
FROM
AP21.SKU
INNER JOIN
AP21.IMPCOST_CLR ON
IMPCOST_CLR.CLRIDX = SKU.CLRIDX
INNER JOIN
AP21.CLR ON
CLR.CLRIDX = IMPCOST_CLR.CLRIDX
INNER JOIN
AP21.IMPCOST ON
IMPCOST.ICIDX = IMPCOST_CLR.ICIDX
INNER JOIN
AP21.CTYPE_STYLE ON
CTYPE_STYLE.STYLEIDX = CLR.STYLEIDX
AND CTYPE_STYLE.CSIDX = IMPCOST.BSCSIDX
AND CTYPE_STYLE.ACTIVE = 1
INNER JOIN
AP21.CTYPE_COSTSHT ON
CTYPE_COSTSHT.CSIDX = CTYPE_STYLE.CSIDX
AND CTYPE_COSTSHT.SKUIDX = SKU.SKUIDX
INNER JOIN
AP21.COSTLABL ON
COSTLABL.COSTIDX = CTYPE_STYLE.COSTIDX
AND COSTLABL.LABNUM = CTYPE_COSTSHT.LABNUM
INNER JOIN
AP21.COSTFORM ON
COSTFORM.COSTIDX = CTYPE_STYLE.COSTIDX
AND COSTFORM.POS = COSTLABL.POS
WHERE
SKU.ACTIVE = 1
GROUP BY
SKU.SKUIDX,
CTYPE_STYLE.ARRCZIDX,
IMPCOST.PURRATE,
IMPCOST.PURCURR
我的解释计划:
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 2077781369
------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 427 | 96 (4)| 00:00:02 |
| 1 | HASH GROUP BY | | 1 | 427 | 96 (4)| 00:00:02 |
| 2 | NESTED LOOPS | | | | | |
| 3 | NESTED LOOPS | | 1 | 427 | 96 (4)| 00:00:02 |
| 4 | VIEW | VW_GBC_37 | 1 | 416 | 94 (4)| 00:00:02 |
| 5 | HASH GROUP BY | | 1 | 107 | 94 (4)| 00:00:02 |
| 6 | NESTED LOOPS | | | | | |
| 7 | NESTED LOOPS | | 1 | 107 | 93 (3)| 00:00:02 |
| 8 | NESTED LOOPS | | 1 | 85 | 90 (3)| 00:00:02 |
| 9 | NESTED LOOPS | | 1 | 69 | 86 (3)| 00:00:02 |
|* 10 | HASH JOIN | | 1 | 55 | 84 (3)| 00:00:02 |
|* 11 | HASH JOIN | | 1122 | 50490 | 27 (4)| 00:00:01 |
|* 12 | HASH JOIN | | 1122 | 28050 | 9 (12)| 00:00:01 |
| 13 | TABLE ACCESS FULL | IMPCOST | 776 | 12416 | 5 (0)| 00:00:01 |
| 14 | TABLE ACCESS FULL | IMPCOST_CLR | 1122 | 10098 | 3 (0)| 00:00:01 |
|* 15 | TABLE ACCESS FULL | CTYPE_STYLE | 8121 | 158K| 18 (0)| 00:00:01 |
| 16 | TABLE ACCESS FULL | CLR | 17469 | 170K| 56 (0)| 00:00:01 |
|* 17 | TABLE ACCESS BY INDEX ROWID| SKU | 4 | 56 | 2 (0)| 00:00:01 |
|* 18 | INDEX RANGE SCAN | SKU2IDX | 4 | | 1 (0)| 00:00:01 |
| 19 | TABLE ACCESS BY INDEX ROWID | CTYPE_COSTSHT | 1 | 16 | 4 (0)| 00:00:01 |
|* 20 | INDEX RANGE SCAN | SYS_C006104 | 1 | | 3 (0)| 00:00:01 |
|* 21 | INDEX RANGE SCAN | CF1IDX | 19 | | 1 (0)| 00:00:01 |
| 22 | TABLE ACCESS BY INDEX ROWID | COSTFORM | 3 | 66 | 3 (0)| 00:00:01 |
|* 23 | INDEX RANGE SCAN | SYS_C006080 | 1 | | 1 (0)| 00:00:01 |
|* 24 | TABLE ACCESS BY INDEX ROWID | COSTLABL | 1 | 11 | 2 (0)| 00:00:01 |
------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
10 - access("CLR"."CLRIDX"="IMPCOST_CLR"."CLRIDX" AND
"CTYPE_STYLE"."STYLEIDX"="CLR"."STYLEIDX")
11 - access("CTYPE_STYLE"."CSIDX"="IMPCOST"."BSCSIDX")
12 - access("IMPCOST"."ICIDX"="IMPCOST_CLR"."ICIDX")
15 - filter("CTYPE_STYLE"."ACTIVE"=1)
17 - filter("SKU"."ACTIVE"=1)
18 - access("IMPCOST_CLR"."CLRIDX"="SKU"."CLRIDX")
20 - access("CTYPE_COSTSHT"."CSIDX"="CTYPE_STYLE"."CSIDX" AND
"CTYPE_COSTSHT"."SKUIDX"="SKU"."SKUIDX")
filter("CTYPE_COSTSHT"."SKUIDX"="SKU"."SKUIDX")
21 - access("COSTFORM"."COSTIDX"="CTYPE_STYLE"."COSTIDX")
23 - access("COSTLABL"."COSTIDX"="ITEM_2" AND "ITEM_3"="COSTLABL"."POS")
filter("ITEM_3"="COSTLABL"."POS")
24 - filter("COSTLABL"."LABNUM"="ITEM_1")
49 rows selected