pangu-user-platform/scripts/sql/V1.0.5__clean_region_data.sql

31 lines
1.2 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- ================================================================
-- 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;