33. #ccc_m7
Bi-temporalを深掘り
• 住所変更する
住所 from_z thru_z in_z out_z
東京都 2019/4/1 9999/12/1 2019/3/25 2019/5/3 A
東京都 2019/4/1 2019/5/1 2019/5/3 9999/12/1 B
埼玉県 2019/5/1 9999/12/1 2019/5/3 9999/12/1 C
Inf
6/1
5/3
5/1
4/1
3/25
BusinessTime(from_z/thru_z)
システム開始日「2019/5/3」の新たなレコードを追加
B
C
3/25 4/1 5/1 3 4 6/1 Inf
Processing Time (in_z/out_z)
A
34. #ccc_m7
Bi-temporalを深掘り
• 住所変更の訂正をする
住所 from_z thru_z in_z out_z
東京都 2019/4/1 9999/12/1 2019/3/25 2019/5/3 A
東京都 2019/4/1 2019/5/1 2019/5/3 9999/12/1 B
埼玉県 2019/5/1 9999/12/1 2019/5/3 2019/5/4 C
Inf
6/1
5/3
5/1
4/1
3/25
BusinessTime(from_z/thru_z)
訂正対象のシステム終了日を
「2019/5/4」に更新して締める
3/25 4/1 5/1 3 4 6/1 Inf
Processing Time (in_z/out_z)
B
C
A
35. #ccc_m7
Bi-temporalを深掘り
• 住所変更の訂正をする
住所 from_z thru_z in_z out_z
東京都 2019/4/1 9999/12/1 2019/3/25 2019/5/3 A
東京都 2019/4/1 2019/5/1 2019/5/3 9999/12/1 B
埼玉県 2019/5/1 9999/12/1 2019/5/3 2019/5/4 C
神奈川県 2019/5/1 9999/12/1 2019/5/4 9999/12/1 D
Inf
6/1
5/3
5/1
4/1
3/25
BusinessTime(from_z/thru_z)
訂正後のレコードを追加する
D
3/25 4/1 5/1 3 4 6/1 Inf
Processing Time (in_z/out_z)
B
C
A
36. #ccc_m7
Bi-temporalを深掘り
• 利用停止する
住所 from_z thru_z in_z out_z
東京都 2019/4/1 9999/12/1 2019/3/25 2019/5/3 A
東京都 2019/4/1 2019/5/1 2019/5/3 9999/12/1 B
埼玉県 2019/5/1 9999/12/1 2019/5/3 2019/5/4 C
神奈川県 2019/5/1 9999/12/1 2019/5/4 2019/6/1 D
Inf
6/1
5/3
5/1
4/1
3/25
BusinessTime(from_z/thru_z)
最新行のシステム終了日を
「2019/6/1」に更新して締める
3/25 4/1 5/1 3 4 6/1 Inf
Processing Time (in_z/out_z)
D
B
C
A
37. #ccc_m7
Bi-temporalを深掘り
• 利用停止する
住所 from_z thru_z in_z out_z
東京都 2019/4/1 9999/12/1 2019/3/25 2019/5/3 A
東京都 2019/4/1 2019/5/1 2019/5/3 9999/12/1 B
埼玉県 2019/5/1 9999/12/1 2019/5/3 2019/5/4 C
神奈川県 2019/5/1 9999/12/1 2019/5/4 2019/6/1 D
神奈川県 2019/5/1 2019/6/1 2019/6/1 9999/12/1 E
Inf
6/1
5/3
5/1
4/1
3/25
BusinessTime(from_z/thru_z)
利用停止のレコードを追加
E
3/25 4/1 5/1 3 4 6/1 Inf
Processing Time (in_z/out_z)
D
B
C
A
38. #ccc_m7
Bi-temporalを深掘り
• 4月中旬の状態
住所 from_z thru_z in_z out_z
東京都 2019/4/1 9999/12/1 2019/3/25 2019/5/3 A
東京都 2019/4/1 2019/5/1 2019/5/3 9999/12/1 B
埼玉県 2019/5/1 9999/12/1 2019/5/3 2019/5/4 C
神奈川県 2019/5/1 9999/12/1 2019/5/4 2019/6/1 D
神奈川県 2019/5/1 2019/6/1 2019/6/1 9999/12/1 E
Inf
6/1
5/3
5/1
4/1
3/25
BusinessTime(from_z/thru_z)
SELECT * FROM USER
WHERE in_z <= ‘2019-04-15’ AND
out_z > ‘2019-04-15’
E
3/25 4/1 5/1 3 4 6/1 Inf
Processing Time (in_z/out_z)
D
B
C
A
39. #ccc_m7
Bi-temporalを深掘り
• 5月中旬の状態
住所 from_z thru_z in_z out_z
東京都 2019/4/1 9999/12/1 2019/3/25 2019/5/3 A
東京都 2019/4/1 2019/5/1 2019/5/3 9999/12/1 B
埼玉県 2019/5/1 9999/12/1 2019/5/3 2019/5/4 C
神奈川県 2019/5/1 9999/12/1 2019/5/4 2019/6/1 D
神奈川県 2019/5/1 2019/6/1 2019/6/1 9999/12/1 E
Inf
6/1
5/3
5/1
4/1
3/25
BusinessTime(from_z/thru_z)
E
3/25 4/1 5/1 3 4 6/1 Inf
Processing Time (in_z/out_z)
D
B
C
A
SELECT * FROM USER
WHERE in_z <= ‘2019-05-15’ AND
out_z > ‘2019-05-15’
40. #ccc_m7
Bi-temporalを深掘り
• 最新の状態
住所 from_z thru_z in_z out_z
東京都 2019/4/1 9999/12/1 2019/3/25 2019/5/3 A
東京都 2019/4/1 2019/5/1 2019/5/3 9999/12/1 B
埼玉県 2019/5/1 9999/12/1 2019/5/3 2019/5/4 C
神奈川県 2019/5/1 9999/12/1 2019/5/4 2019/6/1 D
神奈川県 2019/5/1 2019/6/1 2019/6/1 9999/12/1 E
Inf
6/1
5/3
5/1
4/1
3/25
BusinessTime(from_z/thru_z)
E
3/25 4/1 5/1 3 4 6/1 Inf
Processing Time (in_z/out_z)
D
B
C
A
SELECT * FROM USER
WHERE out_z = ‘9999-12-01’
88. #ccc_m7
Reladomoの実装例
• CRUD操作の例 – R(select)
Operation id = UserFinder.userId().eq(1);
Operation bz = UserFinder.businessDate().equalsEdgePoint();
Operation pr = UserFinder.processingDate().equalsInfinity();
User user = UserFinder.findOne(id.and(bz).and(pr));
findOneメソッドで1件検索
select * from user
where user_id = 1 and
out_z = '9999-12-01 23:59:00.000'
Inf
6/1
5/3
5/1
4/1
3/25
BusinessTime(from_z/thru_z)
E
3/25 4/1 5/1 3 4 6/1 Inf
Processing Time (in_z/out_z)
D
B
C
A
89. #ccc_m7
Reladomoの実装例
• CRUD操作の例 – R(select)
Operation id = UserFinder.userId().eq(1);
Operation bz = UserFinder.businessDate().equalsEdgePoint();
Operation pr = UserFinder.processingDate().equalsInfinity();
UserList userList = UserFinder.findMany(id.and(bz).and(pr));
findManyメソッドで複数件検索
Inf
6/1
5/3
5/1
4/1
3/25
BusinessTime(from_z/thru_z)
E
3/25 4/1 5/1 3 4 6/1 Inf
Processing Time (in_z/out_z)
D
B
C
A
select * from user
where user_id = 1 and
out_z = '9999-12-01 23:59:00.000'
90. #ccc_m7
Reladomoの実装例
• CRUD操作の例 – R(select)
Operation id = UserFinder.userId().eq(1);
Operation bz = UserFinder.businessDate().equalsEdgePoint();
Operation pr = UserFinder.processingDate().eq(Timestamp.valueOf("2019-05-15"));
UserList userList = UserFinder.findMany(id.and(bz).and(pr));
過去を切り取る
select * from user
where user_id = 1 and
in_z <= '2019-05-15 00:00:00.000' and
out_z > '2019-05-15 00:00:00.000'
Inf
6/1
5/3
5/1
4/1
3/25
BusinessTime(from_z/thru_z)
E
3/25 4/1 5/1 3 4 6/1 Inf
Processing Time (in_z/out_z)
D
B
C
A
91. #ccc_m7
Reladomoの実装例
• CRUD操作の例 – R(select)
Operation id = UserFinder.userId().eq(1);
Operation bz = UserFinder.businessDate().eq(Timestamp.valueOf("2019-05-20"));
Operation pr = UserFinder.processingDate().equalsInfinity();
User user = UserFinder.findOne(id.and(bz).and(pr));
businessDateの指定も可能
Inf
6/1
5/3
5/1
4/1
3/25
BusinessTime(from_z/thru_z)
E
3/25 4/1 5/1 3 4 6/1 Inf
Processing Time (in_z/out_z)
D
B
C
A
select * from user
where user_id = 1 and
from_z <= '2019-05-20 00:00:00.000' and
thru_z > '2019-05-20 00:00:00.000‘ and
out_z = '9999-12-01 23:59:00.000'
102. #ccc_m7
Reladomoの実装例
• リレーションの定義
問題マスターを全件読み、問題文を出力
Operation operation = QuestionMasterFinder.all();
QuestionMasterList list = QuestionMasterFinder.findMany(operation); ①
for (QuestionMaster master : list) {
for (Question question : master.getQuestions()) { ②
System.out.println("問題:" + question.getContents());
・・・
① select * from question_master
② select * from question where question_master_id = 1
select * from question where question_master_id = 2
select * from question where question_master_id = 3
・・・
103. #ccc_m7
Reladomoの実装例
• リレーションの定義
問題マスターを全件読み、問題文を出力
Operation operation = QuestionMasterFinder.all();
QuestionMasterList list = QuestionMasterFinder.findMany(operation); ①
for (QuestionMaster master : list) {
for (Question question : master.getQuestions()) { ②
System.out.println("問題:" + question.getContents());
・・・
① select * from question_master
② select * from question where question_master_id = 1
select * from question where question_master_id = 2
select * from question where question_master_id = 3
・・・
N+1クエリ問題
104. #ccc_m7
Reladomoの実装例
• リレーションの定義
問題マスターを全件読み、問題文を出力
Operation operation = QuestionMasterFinder.all();
QuestionMasterList list = QuestionMasterFinder.findMany(operation); ①
for (QuestionMaster master : list) {
for (Question question : master.getQuestions()) { ②
System.out.println("問題:" + question.getContents());
・・・
① select * from question_master
② select * from question where question_master_id = 1
select * from question where question_master_id = 2
select * from question where question_master_id = 3
N+1クエリ問題 DeepFetchがあるよ
105. #ccc_m7
Reladomoの実装例
• リレーションの定義
問題マスターを全件読み、問題文を出力
Operation operation = QuestionMasterFinder.all();
QuestionMasterList list = QuestionMasterFinder.findMany(operation); ①
list.deepFetch(QuestionMasterFinder.questions());
for (QuestionMaster master : list) {
for (Question question : master.getQuestions()) { ②
System.out.println("問題:" + question.getContents());
・・・
① select * from question_master
② select * from question where question_master_id in ( 1 , 2 , 3 )
106. #ccc_m7
Reladomoの実装例
• リレーションの定義
問題マスターを全件読み、問題文を出力
Operation operation = QuestionMasterFinder.all();
QuestionMasterList list = QuestionMasterFinder.findMany(operation); ①
list.deepFetch(QuestionMasterFinder.questions());
for (QuestionMaster master : list) {
for (Question question : master.getQuestions()) { ②
System.out.println("問題:" + question.getContents());
・・・
① select * from question_master
② select * from question where question_master_id in ( 1 , 2 , 3 )
一度findManyやfindOneで
検索しないといけない
JOINしてSQL 1発で取れないのか?