我需要从两个时间戳之间的某些表中获取所有ID
select Distinct PK1 from(
SELECT A.PK1
FROM T1 A , T2 B, T3 C, T4 D
WHERE TO_CHAR(A.DATE1,'YYYY-MM-DD HH24:MI:SS' )>= '2016-06-01 00:00:00'
AND TO_CHAR(A.DATE1,'YYYY-MM-DD HH24:MI:SS' ) <= '2016-06-02 00:00:00' and a.PK2 is not null
AND A.PK2=B.PK2 AND B.PK3 IS NOT NULL AND B.PK3=C.PK3 AND C.PK4 IS NOT NULL
AND C.PK4=D.PK4 AND D.PK5 IS NOT NULL
union
SELECT A.PK1
FROM T1 a, T2 B , T3 C, T4 D
WHERE TO_CHAR(A.DATE2,'YYYY-MM-DD HH24:MI:SS' )>= '2016-06-01 00:00:00'
AND TO_CHAR(A.DATE2,'YYYY-MM-DD HH24:MI:SS' ) <='2016-06-02 00:00:00' and a.PK2 is not null
AND A.PK2=B.PK2 AND B.PK3 IS NOT NULL AND B.PK3=C.PK3 AND C.PK4 IS NOT NULL
AND C.PK4=D.PK4 AND D.PK5 IS NOT NULL
UNION
select A.PK1
from T11 A, T1 B , T2 C, T3 D, T4 E WHERE
TO_CHAR(A.DATE1,'YYYY-MM-DD HH24:MI:SS')>='2016-06-01 00:00:00'
AND TO_CHAR(A.DATE1,'YYYY-MM-DD HH24:MI:SS')<='2016-06-02 00:00:00'
AND A.PK1=B.PK1 AND B.PK2 IS
NOT NULL AND B.PK2=C.PK2 AND C.PK3 IS NOT NULL AND C.PK3=D.PK3 AND D.PK4 IS NOT NULL
AND D.PK4=E.PK4 AND E.PK5 IS NOT NULL
UNION
select A.PK1
from T15 A , T1 B, T2 C, T3 D, T4 E WHERE
TO_CHAR(A.DATE1,'YYYY-MM-DD HH24:MI:SS')>='2016-06-01 00:00:00'
AND TO_CHAR(A.DATE1,'YYYY-MM-DD HH24:MI:SS') <='2016-06-02 00:00:00'
AND A.PK1=B.PK1 AND B.PK2 IS NOT NULL
AND B.PK2=C.PK2 AND C.PK3 IS NOT NULL AND C.PK3=D.PK3 AND D.PK4 IS NOT NULL
AND D.PK4=E.PK4 AND E.PK5 IS NOT NULL
UNION
select A.PK1
from T12 a, T6 b, T1 C, T2 D, T3 E, T4 F WHERE
TO_CHAR(b.DATE1,'YYYY-MM-DD HH24:MI:SS')>='2016-06-01 00:00:00'
AND TO_CHAR(b.DATE1,'YYYY-MM-DD HH24:MI:SS') <='2016-06-02 00:00:00' and A.PK6=b.PK6 AND A.PK1=C.PK1 AND
C.PK2 IS NOT NULL AND C.PK2=D.PK2 AND D.PK3 IS NOT NULL AND D.PK3=E.PK3
AND E.PK4 IS NOT NULL AND E.PK4=F.PK4 AND F.PK5 IS NOT NULL
UNION
select A.PK1
from T13 A, T7 b, T1 C, T2 D, T3 E, T4 F WHERE
TO_CHAR(b.DATE1,'YYYY-MM-DD HH24:MI:SS')>='2016-06-01 00:00:00'
AND TO_CHAR(b.DATE1,'YYYY-MM-DD HH24:MI:SS') <='2016-06-02 00:00:00'
and A.PK7=b.PK7 AND A.PK1=C.PK1 AND
C.PK2 IS NOT NULL AND C.PK2=D.PK2 AND D.PK3 IS NOT NULL AND D.PK3=E.PK3
AND E.PK4 IS NOT NULL AND E.PK4=F.PK4 AND F.PK5 IS NOT NULL
UNION
select A.PK1
from T13 A, T7 b, T1 C, T2 D, T3 E, T4 F WHERE
TO_CHAR(b.DATE2,'YYYY-MM-DD HH24:MI:SS')>='2016-06-01 00:00:00'
AND TO_CHAR(b.DATE2,'YYYY-MM-DD HH24:MI:SS') <='2016-06-02 00:00:00'
and A.PK7=b.PK7 AND A.PK1=C.PK1 AND
C.PK2 IS NOT NULL AND C.PK2=D.PK2 AND D.PK3 IS NOT NULL AND D.PK3=E.PK3
AND E.PK4 IS NOT NULL AND E.PK4=F.PK4 AND F.PK5 IS NOT NULL
UNION
select A.PK1
from T13 A, T7 b, T1 C, T2 D, T3 E, T4 F WHERE
TO_CHAR(b.DATE3,'YYYY-MM-DD HH24:MI:SS')>='2016-06-01 00:00:00'
AND TO_CHAR(b.DATE3,'YYYY-MM-DD HH24:MI:SS') <='2016-06-02 00:00:00'
and A.PK7=b.PK7 AND A.PK1=C.PK1 AND
C.PK2 IS NOT NULL AND C.PK2=D.PK2 AND D.PK3 IS NOT NULL AND D.PK3=E.PK3
AND E.PK4 IS NOT NULL AND E.PK4=F.PK4 AND F.PK5 IS NOT NULL
UNION
select A.PK1
from T14 a, T8 b, T1 C, T2 D, T3 E,
T4 F WHERE
TO_CHAR(b.DATE1,'YYYY-MM-DD HH24:MI:SS')>='2016-06-01 00:00:00'
AND TO_CHAR(b.DATE1,'YYYY-MM-DD HH24:MI:SS') <='2016-06-02 00:00:00' and A.PK8=b.PK8 AND A.PK1=C.PK1 AND
C.PK2 IS NOT NULL AND C.PK2=D.PK2 AND D.PK3 IS NOT NULL
AND D.PK3=E.PK3
AND E.PK4 IS NOT NULL AND E.PK4=F.PK4 AND F.PK5 IS NOT NULL
)
我的问题是如何优化查询以获得更好的性能。
答案 0 :(得分:2)
您正在将日期转换为字符。如果使用datetype日期进行比较,并将索引添加到日期列,则性能可能会提高。转换对性能非常不利,导致表扫描。
答案 1 :(得分:0)
在这里使用to_char将每行中的日期转换为characterset,然后将其与右侧的值进行比较。在代码的顶部,将右侧的值转换为datetime,并将其直接与日期类型的列进行比较。这将使它更快。
此外,您可以查看估计的查询执行路径,并最大限度地降低各个步骤的成本。