Skip to content

Commit

Permalink
Optimized aggregation. New Index
Browse files Browse the repository at this point in the history
Aggregation with single $match stage instead of multiple stages. Native to Mongo (no $where).
More efficient use of indexes (+ new index added).

Signed-off-by: Alexander Pyatakov <[email protected]>
  • Loading branch information
Pyatakov committed Nov 11, 2024
1 parent ec878ea commit 76719f7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
16 changes: 5 additions & 11 deletions common/src/helpers/db-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,20 +481,14 @@ export class DataBaseHelper<T extends BaseEntity> extends AbstractDataBaseHelper
$match: {
sent: true,
done: { $ne: true },
},
},
{
$addFields: {
timeDifference: {
$subtract: ['$processedTime', '$createDate'],
$expr: {
$gt: [
{ $subtract: ['$processedTime', '$createDate'] },
processTimeout
]
},
},
},
{
$match: {
timeDifference: { $gt: processTimeout },
},
},
],
};

Expand Down
3 changes: 2 additions & 1 deletion queue-service/src/entity/task.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Entity, Property } from '@mikro-orm/core';
import { Entity, Index, Property } from '@mikro-orm/core';
import { ITask, WorkerTaskType } from '@guardian/interfaces';
import { BaseEntity } from '@guardian/common';

@Entity()
@Index({ name: 'task_entity_idx', properties: ['sent', 'done', 'processedTime', 'createDate'] })
export class TaskEntity extends BaseEntity implements ITask{
@Property({nullable: true})
userId: string | null;
Expand Down

0 comments on commit 76719f7

Please sign in to comment.