61 lines
2.1 KiB
SQL
61 lines
2.1 KiB
SQL
-- Phase 5: 추천 크리에이터 동시 팔로우 중복 방지 운영 DB 반영 SQL
|
|
-- 목적: creator_following 테이블의 동일 회원/크리에이터 중복 row를 정리하고 유니크 제약을 추가한다.
|
|
-- 주의: 운영 반영 전 아래 중복 조회 결과를 검토하고, 삭제 대상 row가 운영 정책상 보존 대상인지 확인한다.
|
|
|
|
-- 1. 중복 데이터 사전 점검
|
|
select
|
|
member_id,
|
|
creator_id,
|
|
count(*) as duplicate_count,
|
|
group_concat(id order by id asc) as duplicate_ids
|
|
from creator_following
|
|
group by member_id, creator_id
|
|
having count(*) > 1;
|
|
|
|
-- 2. 중복 row 정리
|
|
-- 동일 member_id/creator_id 조합에서 가장 작은 id 1개만 유지한다.
|
|
-- 유지 row는 중복 row 중 하나라도 활성 상태였으면 활성 상태로 보정한다.
|
|
update creator_following keep_cf
|
|
join (
|
|
select
|
|
member_id,
|
|
creator_id,
|
|
min(id) as keep_id,
|
|
max(case when is_active = true then 1 else 0 end) as any_active,
|
|
max(case when is_notify = true then 1 else 0 end) as any_notify
|
|
from creator_following
|
|
group by member_id, creator_id
|
|
having count(*) > 1
|
|
) duplicate_cf on keep_cf.id = duplicate_cf.keep_id
|
|
set
|
|
keep_cf.is_active = duplicate_cf.any_active = 1,
|
|
keep_cf.is_notify = duplicate_cf.any_notify = 1;
|
|
|
|
delete duplicate_cf
|
|
from creator_following duplicate_cf
|
|
join (
|
|
select
|
|
member_id,
|
|
creator_id,
|
|
min(id) as keep_id
|
|
from creator_following
|
|
group by member_id, creator_id
|
|
having count(*) > 1
|
|
) keep_cf on duplicate_cf.member_id = keep_cf.member_id
|
|
and duplicate_cf.creator_id = keep_cf.creator_id
|
|
and duplicate_cf.id <> keep_cf.keep_id;
|
|
|
|
-- 3. 중복 정리 결과 재확인: 결과가 없어야 한다.
|
|
select
|
|
member_id,
|
|
creator_id,
|
|
count(*) as duplicate_count,
|
|
group_concat(id order by id asc) as duplicate_ids
|
|
from creator_following
|
|
group by member_id, creator_id
|
|
having count(*) > 1;
|
|
|
|
-- 4. 유니크 제약 추가
|
|
alter table creator_following
|
|
add constraint uk_creator_following_member_creator unique (member_id, creator_id);
|