ditto/packages/ditto/cron.ts
Chad Curtis baa7968305 Optimize top_authors materialized view for better performance
- Limit materialized view to authors with followers_count > 0 and top 10k records
- Add proper indexes (search GIN, pubkey B-tree, followers_count DESC)
- Use CONCURRENTLY for non-blocking refreshes in cron job
- Reduces sorting overhead and memory usage during refresh

The original materialized view was expensive because it sorted all authors
regardless of follower count. This optimization reduces the dataset to a
manageable size while maintaining search functionality.

Fixes expensive query issue in top_authors materialized view.
2025-09-14 21:11:14 +00:00

24 lines
923 B
TypeScript

import { sql } from 'kysely';
import {
type TrendsCtx,
updateTrendingEvents,
updateTrendingHashtags,
updateTrendingLinks,
updateTrendingPubkeys,
updateTrendingZappedEvents,
} from '@/trends.ts';
/** Start cron jobs for the application. */
export function cron(ctx: TrendsCtx) {
Deno.cron('update trending pubkeys', '0 * * * *', () => updateTrendingPubkeys(ctx));
Deno.cron('update trending zapped events', '7 * * * *', () => updateTrendingZappedEvents(ctx));
Deno.cron('update trending events', '15 * * * *', () => updateTrendingEvents(ctx));
Deno.cron('update trending hashtags', '30 * * * *', () => updateTrendingHashtags(ctx));
Deno.cron('update trending links', '45 * * * *', () => updateTrendingLinks(ctx));
Deno.cron('refresh top authors', '20 * * * *', async () => {
const { kysely } = ctx.db;
await sql`refresh materialized view CONCURRENTLY top_authors`.execute(kysely);
});
}