Skip to content

Commit

Permalink
Merge pull request #2657 from owid/index-updated-at
Browse files Browse the repository at this point in the history
✨ index posts_gdocs.updatedAt
  • Loading branch information
ikesau authored Sep 26, 2023
2 parents 14ef2b0 + 1c13803 commit 48cb429
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
13 changes: 10 additions & 3 deletions adminSiteServer/apiRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions db/migration/1695651135934-IndexUpdatedAt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { MigrationInterface, QueryRunner } from "typeorm"

export class IndexUpdatedAt1695651135934 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
queryRunner.query(
`ALTER TABLE posts_gdocs ADD INDEX idx_updatedAt (updatedAt);`
)
}

public async down(queryRunner: QueryRunner): Promise<void> {
queryRunner.query(
`ALTER TABLE posts_gdocs DROP INDEX idx_updatedAt (updatedAt);`
)
}
}

0 comments on commit 48cb429

Please sign in to comment.