我有一个表格,其主要组合名称,年龄,地址基于我们必须从DUMMY_TEST
表中找到id的组合。
对于DUMMY_TEST
表中的id,必须获取应与DUMMY_ID_VALUE
表映射的所有名称,年龄,地址组合,以获得Valuee列结果。
需要以下给定的预期结果而不加入同一个表DUMMY_TEST
两次,因为它实时有数百万条记录会产生性能问题。
任何人都可以帮助创建一个性能良好的查询,而无需使用相同的表两次。
--table creation
CREATE TABLE DUMMY(NAME VARCHAR2(100),AGE VARCHAR2(100),ADDRESS VARCHAR2(100), ID VARCHAR2(100));
--data loading
INSERT INTO DUMMY VALUES('SAM','30','ITALY','100');
INSERT INTO DUMMY VALUES('RAGHU','20','VENICE','300');
INSERT INTO DUMMY VALUES('TOM','40','JAPAN','200');
CREATE TABLE DUMMY_TEST(NAME VARCHAR2(100),AGE VARCHAR2(100),ADDRESS VARCHAR2(100), ID VARCHAR2(100));
INSERT INTO DUMMY_TEST VALUES('SAM','30','ITALY','100');
INSERT INTO DUMMY_TEST VALUES('TOM','40','JAPAN','200');
INSERT INTO DUMMY_TEST VALUES('BROSNAN','20','INDIA','100');
INSERT INTO DUMMY_TEST VALUES('ARJUN','30','AMERICA','200');
INSERT INTO DUMMY_TEST VALUES('SAMUEL','40','BERLIN','100');
INSERT INTO DUMMY_TEST VALUES('RAM','60','GERMANY','200');
--table creation
CREATE TABLE DUMMY_ID_VALUE(ID VARCHAR2(100),VALUEE VARCHAR2(100));
--data loading
INSERT INTO DUMMY_ID_VALUE VALUES(100,'INCLUDED');
INSERT INTO DUMMY_ID_VALUE VALUES(200,'exclueded');
INSERT INTO DUMMY_ID_VALUE VALUES(300,'PARTIAL');
查询
SELECT DT.NAME,DT.AGE,DT.ADDRESS,DT.ID,DUMMY_ID_VALUE.VALUEE
FROM DUMMY
LEFT OUTER JOIN DUMMY_TEST A
ON DUMMY.NAME = A.NAME AND DUMMY.AGE = A.AGE AND DUMMY.ADDRESS = A.ADDRESS
LEFT OUTER JOIN DUMMY_TEST DT
ON DT.ID = A.ID
INNER JOIN DUMMY_ID_VALUE
ON DUMMY_ID_VALUE.ID = DT.ID;
实际结果
NAME AGE ADDRESS ID VALUEE
---------------------------------
SAM 30 ITALY 100 INCLUDED
BROSNAN 20 INDIA 100 INCLUDED
SAMUEL 40 BERLIN 100 INCLUDED
预期结果:
NAME AGE ADDRESS ID VALUEE
---------------------------------
SAM 30 ITALY 100 INCLUDED
BROSNAN 20 INDIA 100 INCLUDED
SAMUEL 40 BERLIN 100 INCLUDED
RAGHU 20 VENICE 300 PARTIAL
答案 0 :(得分:0)
所以,这里有两个问题,一个(避免双重加入)很容易解决:
select A.NAME, A.AGE, A.ADDRESS, A.ID, DUMMY_ID_VALUE.VALUEE
FROM DUMMY
left OUTER JOIN DUMMY_TEST A ON (DUMMY.NAME = A.NAME AND DUMMY.AGE = A.AGE AND DUMMY.ADDRESS = A.ADDRESS) or (DUMMY.ID = A.ID)
INNER JOIN DUMMY_ID_VALUE on DUMMY_ID_VALUE.ID = A.ID
但我不确定它是否会改善表现 另一个是你得到的结果不符合你的期望,假设你在查询中输入的结果应该是6行。
答案 1 :(得分:0)
您的查询没问题,join
除外。我会去:
SELECT DT.NAME, DT.AGE, DT.ADDRESS, DT.ID, DUMMY_ID_VALUE.VALUEE
FROM DUMMY LEFT OUTER JOIN
DUMMY_TEST A
ON DUMMY.NAME = A.NAME AND DUMMY.AGE = A.AGE AND DUMMY.ADDRESS = A.ADDRESS LEFT OUTER JOIN
DUMMY_TEST DT
ON DT.ID = A.ID LEFT JOIN
DUMMY_ID_VALUE
ON DUMMY_ID_VALUE.ID = DT.ID;
最终加入必须是LEFT JOIN
。为了性能,您的查询没问题。 DUMMY_TEST(NAME, AGE, ADDRESS)
上的索引将有助于您的表现。