TypeORM performance optimization techniques:
-
Use indexes:
typescript@Column() @Index() email: string; -
Avoid N+1 queries: Use relations or join to preload related data.
-
Select only necessary fields:
typescriptuserRepository.find({ select: ['id', 'name'] }); -
Use pagination:
typescriptuserRepository.find({ skip: 0, take: 10 }); -
Batch operations: Use batch methods of insert and update instead of loop operations.
-
Use caching: Enable cache options in DataSource.
-
Optimize queries:
- Use QueryBuilder to build efficient queries
- Avoid SELECT *
- Use WHERE conditions reasonably
-
Connection pool configuration: Reasonably set poolSize and connection pool parameters.
-
Use native SQL: For complex queries, consider using native SQL.
-
Monitoring and logging: Enable logging option to monitor SQL query performance.