1
내가 기본 기록을 정의하는 간단한 딕셔너리가 upserts : 다음pymongo은 ensureIndex하고 아래와 같이
record = {
'h': site_hash, #combination of date (below) and site id hashed with md5
'dt': d, # date - YYYYMMDD
'si': data['site'], # site id
'cl': data['client'], # client id
'nt': data['type'], # site type
}
I 호출은 다음과 같이 존재하지 않는 경우 레코드를 업데이트하려면 다음
collection.update(
record,
{'$inc':updates}, # updates contain some values that increase such as events: 1, actions:1, etc
True # do upsert
);
아래의 코드가 h/dt/si/cl/nt 대신 'h'값만 보이기 때문에 성능이 좋으면 위의 내용을 변경하면 궁금합니다. 'h'필드의 ensureIndex. 그러나 분명히 $ set은 $ inc와 반대되는 기록을 더 많이 기록 할 때마다 실행됩니다.
record = {
'h': site_hash, #combination of date (below) and site id hashed with md5
}
values = {
'dt': d, # date - YYYYMMDD
'si': data['site'], # site id
'cl': data['client'], # client id
'nt': data['type'], # site type
}
collection.update(
record,
{'$inc':updates,'$set':values},
True # do upsert
);
모범 사례에 대한 조언이나 제안 사항이 있으십니까?
upsert를 수행 할 때 mongo는 색인 된 값만 검색하여 레코드가 존재하는지 확인합니까? 그래서 "h"에 "고유 인덱스"를 만든 경우 레코드를 검색 할 때 "dt", "si"등의 필드를 완전히 무시합니다 (해당 필드에 키 인덱스가 있더라도?) – Joe
아니요, MongoDB는 전체 문서를 비교해야하지만 색인을 사용하여 검색 한 다음 나머지 필드를 비교할 수 있습니다. 이는 매우 빠릅니다. –