任何人都可以帮助我解决导致字符串连接时间过长的ORA-01489问题。
我正在尝试从Oracle数据库针对Impala创建DDL脚本,并使用listAGG来获取数据列及其dataType,这是我面临的串联问题。
由于我是Oracle的新手,因此我一直致力于解决由于ListAGG和查询中的大量串联操作导致的问题,因此欢迎使用任何优化的查询。 Oracle版本:Oracle数据库11g企业版。
以下是我正在使用的查询:
`
WITH META AS (
SELECT
t.table_name
, t.column_name AS columnName
, t.data_type AS DataType
, t.DATA_LENGTH AS DataLENGTH
, CASE WHEN t.DATA_Precision < 10 AND t.DATA_SCALE = 0 THEN 'INT'
WHEN t.DATA_Precision < 19 AND t.DATA_SCALE = 0 THEN 'BIGINT'
WHEN t.DATA_Precision > 19 AND t.DATA_SCALE > 0 THEN 'DECIMAL( ' || t.DATA_SCALE || t.DATA_Precision || ')'
ELSE NULL END AS DataPrecision
, uc.constraint_type AS ConstraintSymbol
, t.NULLABLE AS Constraint_Text
, t.column_ID as COL_ID
FROM all_tab_columns t
LEFT JOIN all_cons_columns cc
ON (cc.table_name = t.table_name AND
cc.column_name = t.column_name)
LEFT JOIN all_constraints uc
ON (t.table_name = uc.table_name AND
uc.constraint_name = cc.constraint_name )
WHERE t.table_name = '$table' and t.owner='$schema'
ORDER BY t.column_ID
)
SELECT
'USE $$"DATABASE"' || chr(10)
||'CREATE EXTERNAL TABLE IF NOT EXISTS '
|| table_name || chr(10)
|| '(' || chr(10)
|| TO_CLOB(ListAGG(columnName || ' ' ||
TO_CLOB(ListAGG(columnName || ' ' ||
-- IMPALA CONVERSIONS
CASE WHEN DataType IN ('CHAR', 'NCHAR') THEN 'CHAR'
WHEN DataType IN ('VARCHAR', 'NVARCHAR', 'VARCHAR2', 'NVARCHAR2') THEN 'VARCHAR'
WHEN DataType IN ('DATE', 'DATETIME', 'DATETIME2', 'TIME') THEN 'TIMESTAMP'
WHEN DataType = 'BINARY_DOUBLE' THEN 'DOUBLE'
WHEN DataType = 'BINARY_FLOAT' THEN 'FLOAT'
WHEN DataType IN ('BIT', 'BINARY', 'BLOB', 'CLOB', 'ROWID', 'UROWID', 'NCLOB', 'UNIQUEIDENTIFIER', 'INTERVALYM', 'INTERVALDS', 'LONG') THEN 'STRING'
WHEN DataType = 'NUMBER' THEN DataPrecision
ELSE 'UNKNOWN' END
|| CASE WHEN DataType NOT LIKE '%NUMBER%' THEN '(' || TO_CHAR(DataLENGTH) || ')' END
|| CASE WHEN Constraint_Text like '%N%' THEN ' COMMENT "<source value>:not-nullable"'
WHEN Constraint_Text like '%Y%' THEN ' COMMENT "<source value>:not-nullable"' END || ' '
|| CASE
WHEN ConstraintSymbol like '%P%' THEN ' COMMENT "<source value>:not-nullable|PK"'
WHEN ConstraintSymbol like '%U%' THEN ' COMMENT "<source value>:not-nullable|UNIQUE"' END || chr(10), ',' )
WITHIN GROUP (ORDER BY COL_ID))
FROM META
GROUP BY table_name
`