mirror of
https://github.com/benjamin-wilson/public-pool.git
synced 2026-09-29 09:05:06 -07:00
query
This commit is contained in:
@@ -44,11 +44,12 @@ export class ClientStatisticsService {
|
|||||||
public async doBulkAsyncUpdate(){
|
public async doBulkAsyncUpdate(){
|
||||||
// Step 1: Prepare data for bulk update
|
// Step 1: Prepare data for bulk update
|
||||||
const values = this.bulkAsyncUpdates.map(stat =>
|
const values = this.bulkAsyncUpdates.map(stat =>
|
||||||
`('${stat.clientId}', ${stat.time}, ${stat.shares || 0}, ${stat.acceptedCount || 0}, NOW())`
|
`('${stat.clientId}', ${stat.time}, ${stat.shares ?? 0}, ${stat.acceptedCount ?? 0}, NOW())`
|
||||||
).join(',');
|
).join(',');
|
||||||
|
|
||||||
// Step 2: Use a temp table and single UPDATE
|
const query = `
|
||||||
await this.clientStatisticsRepository.query(`
|
DO $$
|
||||||
|
BEGIN
|
||||||
CREATE TEMP TABLE temp_stats (
|
CREATE TEMP TABLE temp_stats (
|
||||||
"clientId" UUID,
|
"clientId" UUID,
|
||||||
time BIGINT,
|
time BIGINT,
|
||||||
@@ -66,7 +67,16 @@ export class ClientStatisticsService {
|
|||||||
"updatedAt" = ts."updatedAt"
|
"updatedAt" = ts."updatedAt"
|
||||||
FROM temp_stats ts
|
FROM temp_stats ts
|
||||||
WHERE cse."clientId" = ts."clientId" AND cse.time = ts.time;
|
WHERE cse."clientId" = ts."clientId" AND cse.time = ts.time;
|
||||||
`);
|
END;
|
||||||
|
$$;
|
||||||
|
`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await this.clientStatisticsRepository.query(query);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Bulk update failed:', error.message, query);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
this.bulkAsyncUpdates = [];
|
this.bulkAsyncUpdates = [];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user