-- ================================================================
-- V1.0.5 清理非湖北省区域数据
-- 执行说明：先检查湖北省的region_id，确认为42后再执行删除
-- 执行时间：需在业务低峰期执行
-- ================================================================

-- 0. 首先确认湖北省的region_id
SELECT region_id, region_name, parent_id, ancestors FROM pg_region WHERE region_name = '湖北省';
-- 预期结果：region_id = 42

-- 1. 查看要删除的数据量（预检查，不会执行删除）
SELECT COUNT(*) as '待删除的区域数量' FROM pg_region 
WHERE region_id != 42 
  AND ancestors NOT LIKE '0,42%';

-- 2. 物理删除非湖北省的区域数据
-- 删除所有ancestors不以"0,42"开头的区域（即非湖北省及其下级区域）
-- 同时排除湖北省本身(region_id=42)
DELETE FROM pg_region 
WHERE region_id != 42 
  AND ancestors NOT LIKE '0,42%';

-- 3. 删除其他省份的根节点（parent_id = 0 且 region_id != 42）
DELETE FROM pg_region 
WHERE parent_id = 0 
  AND region_id != 42;

-- 4. 验证结果
SELECT COUNT(*) as '剩余区域数量' FROM pg_region;
SELECT region_name, level, COUNT(*) as '数量' FROM pg_region GROUP BY level, region_name LIMIT 20;
