Skip to content

Commit

Permalink
fix: optimize COUNT calculations via the use of count tables (#175)
Browse files Browse the repository at this point in the history
* feat: first iteration

* fix: rollbacks

* fix: block height ranges

* fix: block hash count

* fix: add custom count support

* feat: add cursed filter

* feat: genesis address filter
  • Loading branch information
rafaelcr authored Aug 1, 2023
1 parent af06a71 commit 31498bd
Show file tree
Hide file tree
Showing 10 changed files with 805 additions and 251 deletions.
149 changes: 149 additions & 0 deletions migrations/1690476164909_count-views-to-tables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate';

export const shorthands: ColumnDefinitions | undefined = undefined;

export function up(pgm: MigrationBuilder): void {
pgm.dropMaterializedView('mime_type_counts');
pgm.createTable('counts_by_mime_type', {
mime_type: {
type: 'text',
notNull: true,
primaryKey: true,
},
count: {
type: 'bigint',
notNull: true,
default: 1,
},
});
pgm.sql(`
INSERT INTO counts_by_mime_type (
SELECT mime_type, COUNT(*) AS count FROM inscriptions GROUP BY mime_type
)
`);

pgm.dropMaterializedView('sat_rarity_counts');
pgm.createTable('counts_by_sat_rarity', {
sat_rarity: {
type: 'text',
notNull: true,
primaryKey: true,
},
count: {
type: 'bigint',
notNull: true,
default: 1,
},
});
pgm.sql(`
INSERT INTO counts_by_sat_rarity (
SELECT sat_rarity, COUNT(*) AS count FROM inscriptions GROUP BY sat_rarity
)
`);

pgm.dropMaterializedView('address_counts');
pgm.createTable('counts_by_address', {
address: {
type: 'text',
notNull: true,
primaryKey: true,
},
count: {
type: 'bigint',
notNull: true,
default: 1,
},
});
pgm.sql(`
INSERT INTO counts_by_address (
SELECT address, COUNT(*) AS count FROM current_locations GROUP BY address
)
`);

pgm.createTable('counts_by_genesis_address', {
address: {
type: 'text',
notNull: true,
primaryKey: true,
},
count: {
type: 'bigint',
notNull: true,
default: 1,
},
});
pgm.sql(`
INSERT INTO counts_by_genesis_address (
SELECT address, COUNT(*) AS count FROM genesis_locations GROUP BY address
)
`);

pgm.dropMaterializedView('inscription_count');
pgm.createTable('counts_by_type', {
type: {
type: 'text',
notNull: true,
primaryKey: true,
},
count: {
type: 'bigint',
notNull: true,
default: 1,
},
});
pgm.sql(`
INSERT INTO counts_by_type (
SELECT 'blessed' AS type, COUNT(*) AS count FROM inscriptions WHERE number >= 0
)
`);
pgm.sql(`
INSERT INTO counts_by_type (
SELECT 'cursed' AS type, COUNT(*) AS count FROM inscriptions WHERE number < 0
)
`);

pgm.createIndex('inscriptions_per_block', ['block_hash']);
}

export function down(pgm: MigrationBuilder): void {
pgm.dropTable('counts_by_mime_type');
pgm.createMaterializedView(
'mime_type_counts',
{ data: true },
`SELECT mime_type, COUNT(*) AS count FROM inscriptions GROUP BY mime_type`
);
pgm.createIndex('mime_type_counts', ['mime_type'], { unique: true });

pgm.dropTable('counts_by_sat_rarity');
pgm.createMaterializedView(
'sat_rarity_counts',
{ data: true },
`
SELECT sat_rarity, COUNT(*) AS count
FROM inscriptions AS i
GROUP BY sat_rarity
`
);
pgm.createIndex('sat_rarity_counts', ['sat_rarity'], { unique: true });

pgm.dropTable('counts_by_address');
pgm.createMaterializedView(
'address_counts',
{ data: true },
`SELECT address, COUNT(*) AS count FROM current_locations GROUP BY address`
);
pgm.createIndex('address_counts', ['address'], { unique: true });

pgm.dropTable('counts_by_type');
pgm.createMaterializedView(
'inscription_count',
{ data: true },
`SELECT COUNT(*) AS count FROM inscriptions`
);
pgm.createIndex('inscription_count', ['count'], { unique: true });

pgm.dropIndex('inscriptions_per_block', ['block_hash']);

pgm.dropTable('counts_by_genesis_address');
}
5 changes: 5 additions & 0 deletions src/api/routes/inscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
BlockHeightParam,
BlockInscriptionTransferSchema,
BlockParam,
CursedParam,
InscriptionIdParamCType,
InscriptionIdentifierParam,
InscriptionIdsParam,
Expand Down Expand Up @@ -83,9 +84,11 @@ const IndexRoutes: FastifyPluginCallback<Record<never, never>, Server, TypeBoxTy
number: Type.Optional(InscriptionNumbersParam),
output: Type.Optional(OutputParam),
address: Type.Optional(AddressesParam),
genesis_address: Type.Optional(AddressesParam),
mime_type: Type.Optional(MimeTypesParam),
rarity: Type.Optional(SatoshiRaritiesParam),
recursive: Type.Optional(RecursiveParam),
cursed: Type.Optional(CursedParam),
// Pagination
offset: Type.Optional(OffsetParam),
limit: Type.Optional(LimitParam),
Expand Down Expand Up @@ -120,9 +123,11 @@ const IndexRoutes: FastifyPluginCallback<Record<never, never>, Server, TypeBoxTy
number: request.query.number,
output: request.query.output,
address: request.query.address,
genesis_address: request.query.genesis_address,
mime_type: request.query.mime_type,
sat_rarity: request.query.rarity,
recursive: request.query.recursive,
cursed: request.query.cursed,
},
{
order_by: request.query.order_by ?? OrderBy.genesis_block_height,
Expand Down
6 changes: 6 additions & 0 deletions src/api/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ export const RecursiveParam = Type.Boolean({
examples: [false],
});

export const CursedParam = Type.Boolean({
title: 'Cursed',
description: 'Whether or not the inscription is cursed',
examples: [false],
});

export const OffsetParam = Type.Integer({
minimum: 0,
title: 'Offset',
Expand Down
Loading

0 comments on commit 31498bd

Please sign in to comment.