-- 创建行数缓存表
CREATE TABLE table_row_count (
table_name VARCHAR(255) PRIMARY KEY,
row_count INT
);
-- 初始化行数
INSERT INTO table_row_count (table_name, row_count)
VALUES ('your_table_name', (SELECT COUNT(*) FROM your_table_name));
-- 创建插入触发器
CREATE TRIGGER after_insert_trigger
AFTER INSERT ON your_table_name
FOR EACH ROW
BEGIN
UPDATE table_row_count
SET row_count = row_count + 1
WHERE table_name = 'your_table_name';
END;
-- 创建删除触发器
CREATE TRIGGER after_delete_trigger
AFTER DELETE ON your_table_name
FOR EACH ROW
BEGIN
UPDATE table_row_count
SET row_count = row_count - 1
WHERE table_name = 'your_table_name';
END;
-- 查询行数
SELECT row_count
FROM table_row_count
WHERE table_name = 'your_table_name';
SELECT SUM(partition_count)
FROM (
SELECT COUNT(*) AS partition_count
FROM your_table_name PARTITION (partition_1)
UNION ALL
SELECT COUNT(*) AS partition_count
FROM your_table_name PARTITION (partition_2)
-- 继续添加其他分区
) AS total_count;