Skip to content

Commit d819a6b

Browse files
authored
[db] Improve performance of DBUserStorageResource.update(...) (#3151)
* [db] UserStorageResource: make userId char(36) instead of varchar(255) * [db] Use INSERT INTO ... ON DUPLICATE KEY UPDATE for user storage
1 parent f1d8250 commit d819a6b

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {MigrationInterface, QueryRunner} from "typeorm";
2+
3+
export class UserStorageUserIdChar1612781029090 implements MigrationInterface {
4+
5+
public async up(queryRunner: QueryRunner): Promise<any> {
6+
await queryRunner.query("ALTER TABLE d_b_user_storage_resource MODIFY userId char(36);");
7+
}
8+
9+
public async down(queryRunner: QueryRunner): Promise<any> {
10+
}
11+
12+
}

components/gitpod-db/src/typeorm/user-storage-resources-db-impl.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { injectable, inject } from "inversify";
99
import { TypeORM } from "./typeorm";
1010
import { UserStorageResourcesDB } from "../user-storage-resources-db";
1111
import { DBUserStorageResource } from "./entity/db-user-storage-resource";
12-
import { log } from '@gitpod/gitpod-protocol/lib/util/logging';
1312

1413
@injectable()
1514
export class TypeORMUserStorageResourcesDBImpl implements UserStorageResourcesDB {
@@ -31,19 +30,16 @@ export class TypeORMUserStorageResourcesDBImpl implements UserStorageResourcesDB
3130
}
3231

3332
async update(userId: string, uri: string, content: string): Promise<void> {
33+
// docs: https://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html
3434
const repo = await this.getUserStorageResourceRepo();
35-
let resource = await this.getResource(userId, uri);
36-
if (resource) {
37-
log.info({ userId }, 'updating resource', { uri });
38-
await repo.update(resource, { content });
39-
} else {
40-
log.info({ userId }, 'saving resource', { uri });
41-
resource = new DBUserStorageResource();
42-
resource.userId = userId;
43-
resource.uri = uri;
44-
resource.content = content;
45-
await repo.save(resource);
46-
}
35+
await repo.query(`
36+
INSERT INTO d_b_user_storage_resource
37+
(userId, uri, content)
38+
VALUES
39+
(?, ?, ?)
40+
ON DUPLICATE KEY UPDATE
41+
content = VALUES(content);
42+
`, [ userId, uri, content ]);
4743
}
4844

4945
async deleteAllForUser(userId: string): Promise<void> {

0 commit comments

Comments
 (0)