diff --git a/databases/_private_indexes.sql b/databases/_private_indexes.sql new file mode 100644 index 0000000..d7fb746 --- /dev/null +++ b/databases/_private_indexes.sql @@ -0,0 +1,32 @@ +-- sponsorTimes + +CREATE INDEX IF NOT EXISTS "idx_16928_sponsorTimes_hashedIP" + ON public."sponsorTimes" USING btree + ("hashedIP" COLLATE pg_catalog."default" ASC NULLS LAST) + TABLESPACE pg_default; + +CREATE INDEX IF NOT EXISTS "sponsorTimes_hashedIP" + ON public."sponsorTimes" USING btree + ("hashedIP" COLLATE pg_catalog."default" ASC NULLS LAST) + TABLESPACE pg_default; + +-- votes + +CREATE INDEX IF NOT EXISTS "votes_userID" + ON public.votes USING btree + ("UUID" COLLATE pg_catalog."default" ASC NULLS LAST) + TABLESPACE pg_default; + +-- shadowBannedUsers + +CREATE INDEX IF NOT EXISTS "shadowBannedUsers_index" + ON public."shadowBannedUsers" USING btree + ("userID" COLLATE pg_catalog."default" ASC NULLS LAST) + TABLESPACE pg_default; + +-- categoryVotes + +CREATE INDEX IF NOT EXISTS "categoryVotes_UUID" + ON public."categoryVotes" USING btree + ("UUID" COLLATE pg_catalog."default" ASC NULLS LAST, "userID" COLLATE pg_catalog."default" ASC NULLS LAST, "hashedIP" COLLATE pg_catalog."default" ASC NULLS LAST, category COLLATE pg_catalog."default" ASC NULLS LAST) + TABLESPACE pg_default; \ No newline at end of file diff --git a/databases/_sponsorTimes_indexes.sql b/databases/_sponsorTimes_indexes.sql new file mode 100644 index 0000000..33f1913 --- /dev/null +++ b/databases/_sponsorTimes_indexes.sql @@ -0,0 +1,66 @@ +-- sponsorTimes + +CREATE INDEX IF NOT EXISTS "sponsorTiems_timeSubmitted" + ON public."sponsorTimes" USING btree + ("timeSubmitted" ASC NULLS LAST) + TABLESPACE pg_default; + +CREATE INDEX IF NOT EXISTS "sponsorTime_userID" + ON public."sponsorTimes" USING btree + ("userID" COLLATE pg_catalog."default" ASC NULLS LAST) + TABLESPACE pg_default; + +CREATE INDEX IF NOT EXISTS "sponsorTimes_UUID" + ON public."sponsorTimes" USING btree + ("UUID" COLLATE pg_catalog."default" ASC NULLS LAST) + TABLESPACE pg_default; + +CREATE INDEX IF NOT EXISTS "sponsorTimes_hashedVideoID" + ON public."sponsorTimes" USING btree + ("hashedVideoID" COLLATE pg_catalog."default" ASC NULLS LAST, category COLLATE pg_catalog."default" ASC NULLS LAST, "startTime" ASC NULLS LAST) + TABLESPACE pg_default; + +CREATE INDEX IF NOT EXISTS "sponsorTimes_videoID" + ON public."sponsorTimes" USING btree + ("videoID" COLLATE pg_catalog."default" ASC NULLS LAST, service COLLATE pg_catalog."default" ASC NULLS LAST, category COLLATE pg_catalog."default" ASC NULLS LAST, "timeSubmitted" ASC NULLS LAST) + TABLESPACE pg_default; + +-- userNames + +CREATE INDEX IF NOT EXISTS "userNames_userID" + ON public."userNames" USING btree + ("userID" COLLATE pg_catalog."default" ASC NULLS LAST) + TABLESPACE pg_default; + +-- vipUsers + +CREATE INDEX IF NOT EXISTS "vipUsers_index" + ON public."vipUsers" USING btree + ("userID" COLLATE pg_catalog."default" ASC NULLS LAST) + TABLESPACE pg_default; + +-- warnings + +CREATE INDEX IF NOT EXISTS warnings_index + ON public.warnings USING btree + ("userID" COLLATE pg_catalog."default" ASC NULLS LAST, "issueTime" DESC NULLS LAST, enabled DESC NULLS LAST) + TABLESPACE pg_default; + +CREATE INDEX IF NOT EXISTS "warnings_issueTime" + ON public.warnings USING btree + ("issueTime" ASC NULLS LAST) + TABLESPACE pg_default; + +-- noSegments + +CREATE INDEX IF NOT EXISTS "noSegments_videoID" + ON public."noSegments" USING btree + ("videoID" COLLATE pg_catalog."default" ASC NULLS LAST, category COLLATE pg_catalog."default" ASC NULLS LAST) + TABLESPACE pg_default; + +-- categoryVotes + +CREATE INDEX IF NOT EXISTS "categoryVotes_UUID_public" + ON public."categoryVotes" USING btree + ("UUID" COLLATE pg_catalog."default" ASC NULLS LAST, category COLLATE pg_catalog."default" ASC NULLS LAST) + TABLESPACE pg_default; \ No newline at end of file diff --git a/src/databases/Postgres.ts b/src/databases/Postgres.ts index bae2da6..1f87e8f 100644 --- a/src/databases/Postgres.ts +++ b/src/databases/Postgres.ts @@ -23,6 +23,8 @@ export class Postgres implements IDatabase { // Upgrade database if required await this.upgradeDB(this.config.fileNamePrefix, this.config.dbSchemaFolder); + + await this.applyIndexes(this.config.fileNamePrefix, this.config.dbSchemaFolder); } } @@ -118,6 +120,15 @@ export class Postgres implements IDatabase { Logger.debug('db update: no file ' + path); } + private async applyIndexes(fileNamePrefix: string, schemaFolder: string) { + const path = schemaFolder + "/_" + fileNamePrefix + "_indexes.sql"; + if (fs.existsSync(path)) { + await this.pool.query(fs.readFileSync(path).toString()); + } else { + Logger.debug('failed to apply indexes to ' + fileNamePrefix); + } + } + private processUpgradeQuery(query: string): string { let result = query; result = result.replace(/sha256\((.*?)\)/gm, "digest($1, 'sha256')");