変化の遅いディメンションが必要です。
これは電子メールと電話でのみ行うので、ご理解ください (2 つのキーを使用することに注意してください。1 つはテーブル内で一意であり、もう 1 つは関係するユーザーに固有です。これはテーブルです。キーはレコードを識別し、ユーザー キーはユーザーを識別します):
table_id、user_id、メール、電話番号、created_at、inactive_at、is_current
- 1, 1, mario@yahoo.it, 123456, 2012-01-02, , 2013-04-01, いいえ
- 2, 2, erik@telecom.de, 123457, 2012-01-03, 2013-02-28, いいえ
- 3, 3, vanessa@o2.de, 1234568, 2012-01-03, null, はい
- 4, 2, erik@telecom.de, 123459, 2012-02-28, null, はい
- 5、1、super.mario@yahoo.it、654321、2013-04-01、2013-04-02、いいえ
- 6, 1, super.mario@yahoo.it, 123456,2013-04-02, null, はい
データベースの最新の状態
select * from FooTable where inactive_at is null
また
select * from FooTable where is_current = 'yes'
マリオへのすべての変更 (マリオは user_id 1)
select * from FooTable where user_id = 1;
2013 年 1 月 1 日から 2013 年 5 月 1 日までのすべての変更
select * from FooTable where created_at between '2013-01-01' and '2013-05-01';
古いバージョンと比較する必要があります(ストアドプロシージャ、Javaまたはphpコードの助けを借りて...選択しました)
select * from FooTable where incative_at between '2013-01-01' and '2013-05-01';
必要に応じて、派手なSQLステートメントを実行できます
select f1.table_id, f1.user_id,
case when f1.email = f2.email then 'NO_CHANGE' else concat(f1.email , ' -> ', f2.email) end,
case when f1.phone = f2.phone then 'NO_CHANGE' else concat(f1.phone , ' -> ', f2.phone) end
from FooTable f1 inner join FooTable f2
on(f1.user_id = f2.user_id)
where f2.created_at in
(select max(f3.created_at) from Footable f3 where f3.user_id = f1.user_id
and f3.created_at < f1.created_at and f1.user_id=f3.user_id)
and f1.created_at between '2013-01-01' and '2013-05-01' ;
ジューシーなクエリを見ることができるように、user_with をプレビューのユーザー行と比較するには...
2013-03-01 のデータベースの状態
select * from FooTable where table_id in
(select max(table_id) from FooTable where inactive_at <= '2013-03-01' group by user_id
union
select id from FooTable where inactive_at is null group by user_id having count(table_id) =1 );
これがあなたが望むものを実装する最も簡単な方法だと思います...数百万のテーブルのリレーショナルモデルを実装できますが、それをクエリするのは面倒です
あなたのデータベースは十分な大きさではありません。私は毎日、さらに大きなデータベースで作業しています。新しいサーバーで節約できるお金は、非常に複雑なリレーショナル モデルに費やす時間に見合うものでしょうか?
ところで、データの変更が速すぎる場合、このアプローチは使用できません...
ボーナス: 最適化: