TypeORM Postgres-从代码中查询10K行需要几分钟,从pgAdmin中查询速度要快得多



我有一个使用TypeORM和NestJS的应用程序。

每当我使用WHERE子句将集合查询到关系字段时,执行和返回结果都需要几分钟时间。

当我从pgAdmin中查询时,它会更快。一般来说,我认为获取一张表的10K行不应该花那么长时间。我已经用mongodb以更快的方式处理了更多的数据。

这是与类型ORM有关还是我做错了什么?

const assetResponse = await getRepository(Asset).find({ where: { collection: collection } })

资产实体类

@PrimaryGeneratedColumn()
asset_id: number;
@Column({ unique: true })
id: number;
@Column({ nullable: false })
token_id: string;
@Column({ nullable: true })
name: string;
@Column({ default: 0 })
num_sales: number;
@Column({ nullable: true })
background_color: string;
@Column({ nullable: true })
image_url: string;
@Column({ nullable: true })
image_preview_url: string;
@Column({ nullable: true })
image_thumbnail_url: string;
@Column({ nullable: true })
image_original_url: string;
@Column({ nullable: true })
animation_original_url: string;
@Column({ nullable: true })
external_link: string;
@Column({ nullable: true })
description: string;
@Column('simple-json', { nullable: true })
asset_contract: AssetContract;
@Column({ nullable: true })
permalink: string;
@ManyToOne(() => Collection, (collection: Collection) => collection.assets, { onDelete: "CASCADE" })
collection: Collection;

您可以使用getManager 使用原始SQL

async findAll(collection : string) {
return await getManager().query(`
SELECT * FROM "Asset" WHERE collection = '${collection}'
`);
}

最新更新