diff --git a/backend/services/falukantService.js b/backend/services/falukantService.js index d6e1619..8a2088a 100644 --- a/backend/services/falukantService.js +++ b/backend/services/falukantService.js @@ -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();