for (const referralUserId of directReferrals) { const SponsorId = referralUserId.id;…
- User
for (const referralUserId of directReferrals) { const SponsorId = referralUserId.id;
codeconst stake = this._addNewStakeSponserRepository .createQueryBuilder() .select('SUM(amount)', 'totalStakeAmount') .where('sponsor_id = :userId', { userId: SponsorId }) .getRawOne(); const award = this._userIncomeRepository .createQueryBuilder() .from(UserIncomeEntity, 'user_income') .addFrom(MasterIncomeEntity, 'master_income') .select('SUM(user_income.income_amount_genrate) as totalIncome') .where( `user_income.quser_id = ${SponsorId} AND master_income.code = 'AWD' AND master_income.id = user_income.master_income_id`, ) .getRawOne(); const [stakeAmount, awrdPromise] = await Promise.all([stake, award]); // const award = awrdPromise?.totalIncome?.toFixed(4) || '0'; const awardd = awrdPromise?.totalIncome?.toFixed(4) || '0'; // console.log(award); const totalStakeAmount = stakeAmount?.totalStakeAmount || 0; Referaluser.push({ referralUserId, totalStakeAmount, awardd }); } - Assistant
The above code snippet loops through each
referralUserIdin thedirectReferralsarray and performs the following actions:- Assigns the
referralUserIdto theSponsorIdvariable. - Queries the
_addNewStakeSponserRepositoryto calculate the total stake amount for the givenSponsorId. - Queries the
_userIncomeRepositoryto calculate the total income or award amount for the givenSponsorIdusing a join with theMasterIncomeEntitytable. - Uses
Promise.allto wait for both the stake and award queries to finish and assigns the results tostakeAmountandawrdPromiserespectively. - Formats the
awrdPromise?.totalIncomevalue to a fixed decimal with four digits after the decimal point and assigns it to theawarddvariable. IfawrdPromise.totalIncomeis undefined, the default value '0' is assigned. - Assigns the
totalStakeAmountfrom thestakeAmountresult or 0 if undefined to the variable.
- Assigns the
- User
after removing the award it run fast but with award api take much time to load
- Assistant
If the addition of the award API is causing a significant increase in the loading time, there are a few things you can try to improve the performance:
-
Check the database indexes: Ensure that the necessary indexes are created on the columns used in the query conditions (
quser_id,code,id). Indexing can greatly improve the performance of database queries. -
Optimize the query: Review the query used in the award API and verify if it can be optimized. Consider things like using appropriate table joins, removing unnecessary conditions, and ensuring that the query execution plan is efficient.
-
Batch processing: Instead of querying for each
SponsorIdone by one, consider batching or chunking the requests. This means that you can query multiple values in a single database query or split them into smaller batches to reduce the number of database round trips.
-