refactor: 重构分区索引策略,移除显式创建索引的方法并更新相关流程
This commit is contained in:
@@ -23,7 +23,6 @@ class DatabaseInitializer {
|
||||
|
||||
// 3. Ensure Partitions for the next month
|
||||
await partitionManager.ensurePartitions(30);
|
||||
await partitionManager.ensureIndexesForExistingPartitions();
|
||||
|
||||
console.log('Database initialization completed successfully.');
|
||||
logger.info('Database initialization completed successfully.');
|
||||
|
||||
@@ -26,75 +26,6 @@ class PartitionManager {
|
||||
return { startMs, endMs, partitionSuffix };
|
||||
}
|
||||
|
||||
async ensurePartitionIndexes(client, schema, table, partitionSuffix) {
|
||||
const startedAt = Date.now();
|
||||
const partitionName = `${schema}.${table}_${partitionSuffix}`;
|
||||
const indexBase = `${table}_${partitionSuffix}`;
|
||||
|
||||
const indexSpecs = [
|
||||
{ name: `idx_${indexBase}_ts`, column: 'ts_ms' },
|
||||
{ name: `idx_${indexBase}_hid`, column: 'hotel_id' },
|
||||
{ name: `idx_${indexBase}_mac`, column: 'mac' },
|
||||
{ name: `idx_${indexBase}_did`, column: 'device_id' },
|
||||
{ name: `idx_${indexBase}_rid`, column: 'room_id' },
|
||||
{ name: `idx_${indexBase}_cs`, column: 'current_status' }
|
||||
];
|
||||
|
||||
for (const spec of indexSpecs) {
|
||||
await client.query(`CREATE INDEX IF NOT EXISTS ${spec.name} ON ${partitionName} (${spec.column});`);
|
||||
}
|
||||
|
||||
await client.query(`ANALYZE ${partitionName};`);
|
||||
|
||||
const elapsedMs = Date.now() - startedAt;
|
||||
if (elapsedMs > 1000) {
|
||||
logger.warn(`Partition index ensure slow`, { partitionName, elapsedMs });
|
||||
}
|
||||
}
|
||||
|
||||
async ensureIndexesForExistingPartitions() {
|
||||
const startedAt = Date.now();
|
||||
const client = await dbManager.pool.connect();
|
||||
try {
|
||||
const schema = config.db.schema;
|
||||
const table = config.db.table;
|
||||
|
||||
const res = await client.query(
|
||||
`
|
||||
SELECT c.relname AS relname
|
||||
FROM pg_inherits i
|
||||
JOIN pg_class p ON i.inhparent = p.oid
|
||||
JOIN pg_namespace pn ON pn.oid = p.relnamespace
|
||||
JOIN pg_class c ON i.inhrelid = c.oid
|
||||
WHERE pn.nspname = $1 AND p.relname = $2
|
||||
ORDER BY c.relname;
|
||||
`,
|
||||
[schema, table]
|
||||
);
|
||||
|
||||
const suffixes = new Set();
|
||||
const pattern = new RegExp(`^${table}_(\\d{8})$`);
|
||||
for (const row of res.rows) {
|
||||
const relname = row?.relname;
|
||||
if (typeof relname !== 'string') continue;
|
||||
const match = relname.match(pattern);
|
||||
if (!match) continue;
|
||||
suffixes.add(match[1]);
|
||||
}
|
||||
|
||||
for (const suffix of suffixes) {
|
||||
await this.ensurePartitionIndexes(client, schema, table, suffix);
|
||||
}
|
||||
|
||||
const elapsedMs = Date.now() - startedAt;
|
||||
if (elapsedMs > 5000) {
|
||||
logger.warn('Ensure existing partition indexes slow', { schema, table, partitions: suffixes.size, elapsedMs });
|
||||
}
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure partitions exist for the past M days and next N days.
|
||||
* @param {number} daysAhead - Number of days to pre-create.
|
||||
@@ -132,8 +63,6 @@ class PartitionManager {
|
||||
`;
|
||||
await client.query(createSql);
|
||||
}
|
||||
|
||||
await this.ensurePartitionIndexes(client, schema, table, partitionSuffix);
|
||||
}
|
||||
logger.info('Partition check completed.');
|
||||
} catch (err) {
|
||||
@@ -185,8 +114,6 @@ class PartitionManager {
|
||||
FOR VALUES FROM (${startMs}) TO (${endMs});
|
||||
`);
|
||||
}
|
||||
|
||||
await this.ensurePartitionIndexes(client, schema, table, partitionSuffix);
|
||||
}
|
||||
} finally {
|
||||
client.release();
|
||||
|
||||
Reference in New Issue
Block a user