Q34:
写了个不甚优雅的SQL竟然很不优雅地溢出了:
WITH t AS (SELECT ROWNUM n,TO_CHAR(ROWNUM) s FROM DUAL CONNECT BY ROWNUM<=9999999)
SELECT * FROM t
WHERE DECODE(SUBSTR(s,1,1),0,1,1,1,2,2,3,6,4,24,5,120,6,720,7,5040,8,40320,9,362880,0)
+DECODE(SUBSTR(s,2,1),0,1,1,1,2,2,3,6,4,24,5,120,6,720,7,5040,8,40320,9,362880,0)
+DECODE(SUBSTR(s,3,1),0,1,1,1,2,2,3,6,4,24,5,120,6,720,7,5040,8,40320,9,362880,0)
+DECODE(SUBSTR(s,4,1),0,1,1,1,2,2,3,6,4,24,5,120,6,720,7,5040,8,40320,9,362880,0)
+DECODE(SUBSTR(s,5,1),0,1,1,1,2,2,3,6,4,24,5,120,6,720,7,5040,8,40320,9,362880,0)
+DECODE(SUBSTR(s,6,1),0,1,1,1,2,2,3,6,4,24,5,120,6,720,7,5040,8,40320,9,362880,0)
+DECODE(SUBSTR(s,7,1),0,1,1,1,2,2,3,6,4,24,5,120,6,720,7,5040,8,40320,9,362880,0)
=n
ERROR:
ORA-30009: Not enough memory for CONNECT BY operation
6位的没有问题, 但是比NB哥的写法慢了两倍:
WITH t AS (
SELECT t.*
,DECODE(SUBSTR(s,1,1),0,1,1,1,2,2,3,6,4,24,5,120,6,720,7,5040,8,40320,9,362880,0)
+DECODE(SUBSTR(s,2,1),0,1,1,1,2,2,3,6,4,24,5,120,6,720,7,5040,8,40320,9,362880,0)
+DECODE(SUBSTR(s,3,1),0,1,1,1,2,2,3,6,4,24,5,120,6,720,7,5040,8,40320,9,362880,0)
+DECODE(SUBSTR(s,4,1),0,1,1,1,2,2,3,6,4,24,5,120,6,720,7,5040,8,40320,9,362880,0)
+DECODE(SUBSTR(s,5,1),0,1,1,1,2,2,3,6,4,24,5,120,6,720,7,5040,8,40320,9,362880,0)
+DECODE(SUBSTR(s,6,1),0,1,1,1,2,2,3,6,4,24,5,120,6,720,7,5040,8,40320,9,362880,0)
+DECODE(SUBSTR(s,7,1),0,1,1,1,2,2,3,6,4,24,5,120,6,720,7,5040,8,40320,9,362880,0)
AS sm
FROM (SELECT ROWNUM n,TO_CHAR(ROWNUM) s FROM DUAL CONNECT BY ROWNUM<=999999) t
)
SELECT * FROM t
WHERE sm=n OR 1+sm=1e6+n OR 2+sm=2e6+n OR 6+sm=3e6+n OR 24+sm=4e6+n
OR 120+sm=5e6+n OR 720+sm=6e6+n OR 5040+sm=7e6+n OR 40320+sm=8e6+n OR 362880+sm=9e6+n;
[ 本帖最后由 newkid 于 2010-12-21 23:51 编辑 ] |