Refactor relationship retrieval in FalukantService for improved performance
- Optimized the process of finding relationships by using two separate queries for better index utilization, reducing data overhead. - Enhanced loading of related characters and traits by implementing parallel data fetching, improving efficiency in data retrieval. - Updated timing metrics to reflect changes in the relationship loading process, ensuring accurate performance tracking.
This commit is contained in:
@@ -3588,58 +3588,47 @@ class FalukantService extends BaseService {
|
||||
if (!myChar) throw new Error('Character not found');
|
||||
timings.step1_user_character = Date.now() - step1Start;
|
||||
|
||||
// 2) Beziehung finden und „anderen" Character bestimmen (optimiert: nur benötigte Felder)
|
||||
// 2) Beziehung finden (zwei separate Queries für bessere Index-Nutzung)
|
||||
const step2Start = Date.now();
|
||||
const rel = await Relationship.findOne({
|
||||
where: {
|
||||
[Op.or]: [
|
||||
{ character1Id: myChar.id },
|
||||
{ character2Id: myChar.id }
|
||||
]
|
||||
},
|
||||
include: [
|
||||
{
|
||||
model: FalukantCharacter,
|
||||
as: 'character1',
|
||||
attributes: ['id', 'moodId'],
|
||||
include: [{
|
||||
model: CharacterTrait,
|
||||
as: 'traits',
|
||||
attributes: ['id'],
|
||||
through: { attributes: [] },
|
||||
required: false
|
||||
}],
|
||||
required: false
|
||||
},
|
||||
{
|
||||
model: FalukantCharacter,
|
||||
as: 'character2',
|
||||
attributes: ['id', 'moodId'],
|
||||
include: [{
|
||||
model: CharacterTrait,
|
||||
as: 'traits',
|
||||
attributes: ['id'],
|
||||
through: { attributes: [] },
|
||||
required: false
|
||||
}],
|
||||
required: false
|
||||
}
|
||||
]
|
||||
});
|
||||
const [relAsChar1, relAsChar2] = await Promise.all([
|
||||
Relationship.findOne({
|
||||
where: { character1Id: myChar.id },
|
||||
attributes: ['character1Id', 'character2Id']
|
||||
}),
|
||||
Relationship.findOne({
|
||||
where: { character2Id: myChar.id },
|
||||
attributes: ['character1Id', 'character2Id']
|
||||
})
|
||||
]);
|
||||
const rel = relAsChar1 || relAsChar2;
|
||||
timings.step2_relationship = Date.now() - step2Start;
|
||||
|
||||
// 3) Wenn keine Beziehung gefunden, alle Gifts ohne Filter zurückgeben
|
||||
// 3) Related Character und Traits laden (nur wenn Relationship existiert)
|
||||
const step3Start = Date.now();
|
||||
let relatedTraitIds = [];
|
||||
let relatedMoodId = null;
|
||||
|
||||
if (rel) {
|
||||
const relatedChar = rel.character1.id === myChar.id ? rel.character2 : rel.character1;
|
||||
// Trait-IDs und Mood des relatedChar
|
||||
relatedTraitIds = relatedChar.traits ? relatedChar.traits.map(t => t.id) : [];
|
||||
relatedMoodId = relatedChar.moodId;
|
||||
const relatedCharId = rel.character1Id === myChar.id ? rel.character2Id : rel.character1Id;
|
||||
|
||||
// Parallel: Character (moodId) und Traits laden
|
||||
const [relatedChar, traitRows] = await Promise.all([
|
||||
FalukantCharacter.findOne({
|
||||
where: { id: relatedCharId },
|
||||
attributes: ['id', 'moodId']
|
||||
}),
|
||||
FalukantCharacterTrait.findAll({
|
||||
where: { characterId: relatedCharId },
|
||||
attributes: ['traitId']
|
||||
})
|
||||
]);
|
||||
|
||||
if (relatedChar) {
|
||||
relatedMoodId = relatedChar.moodId;
|
||||
relatedTraitIds = traitRows.map(t => t.traitId);
|
||||
}
|
||||
}
|
||||
timings.step3_process_relationship = Date.now() - step3Start;
|
||||
timings.step3_load_character_and_traits = Date.now() - step3Start;
|
||||
|
||||
// 4) Gifts laden – mit Mood/Trait-Filter nur wenn Beziehung existiert
|
||||
const step4Start = Date.now();
|
||||
|
||||
Reference in New Issue
Block a user