首页 > 解决方案 > PostgreSQL 的 TypeORM 性能问题

问题描述

我对 TypeORM 和 PostgreSQL 有一个奇怪的问题。下面是nest.js 中代码的简单部分

  @Get()
  async getHello(): Promise<any> {
    const keywords = 'wxample';
    const now1: any = new Date();
    await this.typeormConnection.query(`
            select admins.first_name from sessions
            JOIN admins ON sessions.admin_id = admins."id"
            LEFT JOIN companies ON companies."id" = admins.company_id
            WHERE admin_sessions.token = '${keywords}'
        `);
    const now2: any = new Date();
    await this.pgConnection.query(`
            select admins.first_name from sessions
            JOIN admins ON sessions.admin_id = admins."id"
            LEFT JOIN companies ON companies."id" = admins.company_id
            WHERE admin_sessions.token = '${keywords}'
    `);
    console.log(`typeorm => ${now2 - now1}ms`);
    console.log(`pg => ${(new Date() as any) - now2}ms`);
    return 'ok';
  }

如果我将它连接到 Azure PostgreSQL,平均结果时间将是:
typeorm => 1300ms
pg => 30ms

但如果我将此代码连接到 AWS,结果将是:
typeorm => 100ms
pg => 30ms

我想知道为什么 tge pure pg 模块在 Azure 上如此之快,但是 typeorm 需要时间,而 typeorm 在 AWS 中可以使用相同的数据库设置?

标签: node.jspostgresqlazurenestjstypeorm

解决方案


推荐阅读