Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance Improvement for Querying Records and Summing Total Amount #52

Open
YuechengHao626 opened this issue Sep 30, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@YuechengHao626
Copy link
Contributor

YuechengHao626 commented Sep 30, 2024

Description:

Currently, the application is experiencing performance issues due to inefficient querying of records, specifically when calculating the total amount of moneyAdded over a certain time range. All records have to be traversed, even when they fall outside the required date range, leading to unnecessary computations and reduced performance as the data size grows. I'm not going to do this improvement now, because it need revise a lot of things, which breaks our code structure and database structure. However, team members can do this optimazation in the next stage.

Additionally, there is no easy way to determine the chronological order between the setDate (goal creation date) and the creation of individual records on the same day. This creates ambiguity when trying to decide which records should be included in the total amount for a given time period.

Current Structure:

Each record in the database is identified by a recordId, and records contain several fields, such as amount, category, and date (in yyyy-mm-dd format).

No timestamps are currently being used, which makes it difficult to perform efficient range queries based on time.

In the above structure, the date field uses a yyyy-mm-dd format. Due to the lack of precision (no time component), it is impossible to distinguish whether a record was created before or after the setDate on the same day, and calculating totals requires fetching and traversing all records in the database.

Issues:

Performance:

Full traversal required: In order to calculate totals for a specific date range, the system currently has to iterate through all records, including those that do not fall within the desired time range. This leads to performance degradation, especially as the dataset grows.

No chronological precision:

Without timestamps, it's challenging to determine the exact order of events when records and setDate fall on the same day, making it difficult to accurately compute totals based on creation order.

Proposed Solution:

Add a timestamp field:

Each record should store a timestamp (in Unix time, seconds or milliseconds) representing the exact moment the record was created. This would allow for efficient range queries and would make it easy to differentiate records created on the same day.

Create an index on the timestamp field:

By indexing the timestamp field in Firebase Realtime Database rules, range queries can be optimized, ensuring that only records within the required time range are fetched.

Improve querying logic:

Implement orderByChild('timestamp') and use startAt() and endAt() for querying records based on the timestamp range. This would avoid the need for a full scan of all records.

Modify existing records:

Migrate existing records by adding a timestamp field based on the available date information or by assigning the current time where precise creation time is unavailable.

@YuechengHao626 YuechengHao626 added the enhancement New feature or request label Sep 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Todo
Development

No branches or pull requests

1 participant