Inner Join, Outer Join, 그리고 독특한 Open SQL 문법인 FOR ALL ENTRIES IN 까지.
정리가 잘 안 되어서(특히 performance 입장에서) 여러 문서를 찾아다녀 보았다.
아래는 그에 대한 간단한 정리.
- database view는 inner join을 이용한다. 즉 database view는 모든 table에 공통적으로 있는 records만 제공한다.
- help views와 maintenance views는 outer join을 이용한다.
- INNER JOIN 과 FOR ALL ENTRIES IN 중 어떤 것이 낫다고 단정짓기 어렵다. 보통 FOR ALL ENTRIES IN 을 추천하지만 경우에 따라서 INNER JOIN 이 나은 성능을 보여주기도 하기 때문이다.
- FOR ALL ENTRIES IN 은 ‘outer join’이다… 라기보다, 이 문법을 통해 outer join을 구현할 수 있다 – 고 이해해야 한다.
단 주의할 점이 몇가지 있다.
- FOR ALL ENTRIES IN 을 사용할 땐 Select 하는 Field들이 모여서 자동으로 키 값이 되어 중복되는 값을 모두 배제해버린다. (즉 SELECT * FROM … 과 SELECT COL1 COL2 COL3 FROM … 의 결과가 틀려질 수 있다) 따라서 반드시 원래 테이블의 키 값들을 SELECT 다음에 다 써주는 것이 좋다.
- If the “one” table (the table that appears in the clause FOR ALL ENTRIES IN) is empty, all rows in the “many” table (the table that appears in the SELECT INTO clause) are selected. Therefore make sure you check that the “one” table has rows before issuing a select with the “FOR ALL ENTRIES IN” clause.
- If the table on which the For All ENTRIES IN clause is based is very large, the performance will go down. Hence attempt should be made to keep the table size to a moderate level.
- SELECT … FOR ALL ENTRIES IN <itab> WHERE <cond> 의 로직은 다음과 같다.
If you specify a field of the internal table <itab> as an operand in a condition, you address all lines of the internal table. The comparison is then performed for each line of the internal table.
For each line, the system selects the lines from the database table that satisfy the condition. The result set of the SELECT statement is the union of the individual selections for each line of the internal table.
Duplicate lines are automatically eliminated from the result set. If <itab> is empty, the addition FOR ALL ENTRIES is disregarded, and all entries are read.