From 17bd9477937723f283c876e2ec0e9c33ad01e044 Mon Sep 17 00:00:00 2001 From: Ike Saunders Date: Mon, 25 Sep 2023 18:06:36 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20index=20posts=5Fgdocs.updatedAt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/migration/1695651135934-IndexUpdatedAt.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 db/migration/1695651135934-IndexUpdatedAt.ts diff --git a/db/migration/1695651135934-IndexUpdatedAt.ts b/db/migration/1695651135934-IndexUpdatedAt.ts new file mode 100644 index 00000000000..bda83685700 --- /dev/null +++ b/db/migration/1695651135934-IndexUpdatedAt.ts @@ -0,0 +1,15 @@ +import { MigrationInterface, QueryRunner } from "typeorm" + +export class IndexUpdatedAt1695651135934 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + queryRunner.query( + `ALTER TABLE posts_gdocs ADD INDEX idx_updatedAt (updatedAt);` + ) + } + + public async down(queryRunner: QueryRunner): Promise { + queryRunner.query( + `ALTER TABLE posts_gdocs DROP INDEX idx_updatedAt (updatedAt);` + ) + } +} From 9d6b18ff1bbf00743bac1a1d516e94cd5c48a3a3 Mon Sep 17 00:00:00 2001 From: Ike Saunders Date: Tue, 26 Sep 2023 20:49:26 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=90=9B=20sort=20gdocs.json=20in-memor?= =?UTF-8?q?y?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- adminSiteServer/apiRouter.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/adminSiteServer/apiRouter.ts b/adminSiteServer/apiRouter.ts index 5666f90faca..a88f84b82ff 100644 --- a/adminSiteServer/apiRouter.ts +++ b/adminSiteServer/apiRouter.ts @@ -2423,9 +2423,16 @@ apiRouter.put("/deploy", async (req: Request, res: Response) => { triggerStaticBuild(res.locals.user, "Manually triggered deploy") }) -apiRouter.get("/gdocs", async () => - Gdoc.find({ relations: ["tags"], order: { updatedAt: "DESC" } }) -) +apiRouter.get("/gdocs", async () => { + // orderBy was leading to a sort buffer overflow (ER_OUT_OF_SORTMEMORY) with MySQL's default sort_buffer_size + // when the posts_gdocs table got larger than 9MB, so we sort in memory + return Gdoc.find({ relations: ["tags"] }).then((gdocs) => + gdocs.sort((a, b) => { + if (!a.updatedAt || !b.updatedAt) return 0 + return b.updatedAt.getTime() - a.updatedAt.getTime() + }) + ) +}) apiRouter.get("/gdocs/:id", async (req, res) => { const id = req.params.id