这是一个常见的问题,需要高效解决。测试用表如下:DROP TABLE T PURGE; CREATE TABLE t AS SELECT '2' AS col1 ,'4' AS col2 FROM dual UNION ALL SELECT '1' AS col1 ,'5' AS col2 FROM dual UNION ALL SELECT '2' AS col1 ,'5' AS col2 FROM dual UNION ALL SELECT '3' AS col1 ,'3' AS col2 FROM dual UNION ALL SELECT '12' AS col1, '16' AS col2 FROM dual UNION ALL SELECT '11' AS col1 ,'15' AS col2 FROM dual UNION ALL SELECT '13' AS col1 ,'13' AS col2 FROM dual UNION ALL SELECT '12' AS col1 ,'17' AS col2 FROM dual;目标是返回col1到col2之间的最大区间。原始查询如下: SELECT to_char(lengthb(col2), 'FM000') || chr(0) num_length, col1, col2 FROM t WHERE NOT EXISTS (SELECT 1 FROM t a WHERE a.col1 = t.col2 AND (t.col1 != a.col1 OR t.col2 != a.col2) AND lengthb(a.col1) = lengthb(t.col1)); 这种写法虽然常见,但执行速度较慢。执行计划如下:PLAN_TABLE_OUTPUT ----- Plan hash value: 366813129 --- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --- | 0 | SELECT STATEMENT | | 1 | 6 | 12 (0)| 00:01 | |* 1 | FIL