2017-09-26 5 views
0

IndexedDB으로 작업하려면 Dexie.JS을 사용하고 있습니다. 현재와 같은 바보 같은 쿼리를 작성했습니다 : 그것은 제대로 작동IndexedDB 쿼리 최적화

return db.events.each((element) => { 
    let d = element.cause.data; 
    if (d.hasOwnProperty('deleted') && (false == d.deleted) && 
     d.hasOwnProperty('abbreviation') && 
     d.hasOwnProperty('contents') && (d.abbreviation == key)) { 
    snippet = d.contents; 
    } 
}).then(() => { 
    return snippet; 
}); 

하지만, 대용량 데이터베이스에 당밀로 천천히. where을 적용한 db.events 컬렉션에서 각각을 실행해야합니까? 성능을 향상시킬 수 있습니까?

먼저 확인 : 문자열, 숫자, 날짜,하는 TypedArray 또는 배열,이 같은 쿼리를 최적화 할 수 있습니다

은 "키"변수를 가정 것은 색인 유형 인 경우

+0

'(cause.data)'와 같은 여러 절에서'keyPath'를 사용하여 시도했습니다 - 빠진 색인에 대한 예외가 있습니다. –

답변

1

예 감사 db.events에 인덱스 "cause.data.abbreviation"를 추가합니다 :

db.version(2).stores({ 
    events: 'yourPrimaryKey, cause.data.abbreviation' 
}); 

을 그리고,이 같은 쿼리 재 작성 :

return db.events 
    // Let indexedDB sort out all items matching given key: 
    .where('cause.data.abbreviation').equals(key) 
    // Filter the rest manually using Collection.filter(): 
    .filter(element => { 
    let d = element.cause.data; 
    return (d.hasOwnProperty('deleted') && (false == d.deleted) && 
     d.hasOwnProperty('contents')); 
    }) 
    // Execute the query and only return the first match: 
    .first();