2017-11-03 6 views
-1

나는이 예제를 구현하기 위해 노력하고있어 :가져 오는 방법 데이터를 빠르게

https://github.com/Azure/azure-documentdb-python/blob/master/samples/DatabaseManagement/Program.py

는 푸른 documentdb에서 데이터를 가져오고 일부 시각화 작업을 수행하려면. 그러나 대신 여기에 #error라고 쓰여있는 줄에 쿼리를 사용하고 싶습니다.

def read_database(client, id): 
    print('3. Read a database by id') 

    try: 

     db = next((data for data in client.ReadDatabases() if data['id'] == database_id)) 
     coll = next((coll for coll in client.ReadCollections(db['_self']) if coll['id'] == database_collection)) 
     return list(itertools.islice(client.ReadDocuments(coll['_self']), 0, 100, 1)) 

    except errors.DocumentDBError as e: 
     if e.status_code == 404: 
      print('A Database with id \'{0}\' does not exist'.format(id)) 
     else: 
      raise errors.HTTPFailure(e.status_code) 

내가 10k를 초과하는 항목을 가져 오려면 가져 오기가 정말 느립니다. 어떻게 개선 할 수 있습니까?

감사합니다.

+0

쿼리를 실행할 때 오류가 있습니까? 그렇다면 질문을 편집하고 오류 정보를 포함하십시오. 또한 귀하의 컬렉션이 분할되어 있는지 알려주십시오. –

+0

안녕하세요, 지금 진행 중이십니까? –

답변

0

데이터베이스 엔터티를 통해 직접 문서를 쿼리 할 수 ​​없습니다.

코드에서 사용되는 ReadDocuments() 메서드의 매개 변수는 컬렉션 링크 및 쿼리 옵션이어야합니다.

def ReadDocuments(self, collection_link, feed_options=None): 
    """Reads all documents in a collection. 

    :Parameters: 
     - `collection_link`: str, the link to the document collection. 
     - `feed_options`: dict 

    :Returns: 
     query_iterable.QueryIterable 

    """ 
    if feed_options is None: 
     feed_options = {} 

    return self.QueryDocuments(collection_link, None, feed_options) 

그래서, 당신은 다음과 같이 코드를 수정할 수 :

# Initialize the Python DocumentDB client 
client = document_client.DocumentClient(config['ENDPOINT'], {'masterKey': config['MASTERKEY']}) 

db = "db" 
coll = "coll" 

try: 
    database_link = 'dbs/' + db 
    database = client.ReadDatabase(database_link) 

    collection_link = 'dbs/' + db + "/colls/" + coll 
    collection = client.ReadCollection(collection_link) 

    # options = {} 
    # options['enableCrossPartitionQuery'] = True 
    # options['partitionKey'] = 'jay' 
    docs = client.ReadDocuments(collection_link) 
    print(list(docs)) 

except errors.DocumentDBError as e: 
    if e.status_code == 404: 
     print('A Database with id \'{0}\' does not exist'.format(id)) 
    else: 
     raise errors.HTTPFailure(e.status_code) 

당신이 컬렉션의 파티션을 조회 할 경우, 위의 코드에서 주석 코드 스 니펫을 추가하시기 바랍니다.

options = {} 
    options['enableCrossPartitionQuery'] = True 
    options['partitionKey'] = 'jay' 

문제가 푸른 코스모스 DB 쿼리 성능에 초점을 맞춘 것 같다.

쿼리 성능을 향상 시키려면 다음 사항을 참조하십시오.

파티션

는 낮은 지연 시간을 필요로하고 낮은 RU에 소모 있도록 단일 파티션 키에 필터 절 데이터베이스와 쿼리의 파티션 키를 설정할 수 있습니다.

처리량

당신은 단위 시간에 푸른 코스모스 DB 성능이 크게 향상 될 수 있도록 조금 더 큰 처리량을 설정할 수 있습니다. 물론 이것은 비용 상승으로 이어질 것입니다.

인덱싱 정책

인덱싱 경로의 사용은 향상된 성능과 낮은 대기 시간을 제공 할 수 있습니다.

자세한 내용은 official performance documentation을 참조하시기 바랍니다.

희망이 있으면 도움이됩니다.

+0

문제는 db에서 항목을 가져올 수 있고, 전에 db를 읽고 coll을 반환합니다. 반환 목록 (itertools.islice (client.ReadDocuments (coll [ '_ self']), 0, 100, 1)) 그리고 잘 작동합니다. 그러나 항목 수가 10k-100k까지 올라가면 속도가 매우 느립니다. 내 질문은 어떻게 개선 할 수 있 었는가? 죄송합니다. 조금 질문을 업데이트했습니다. – kai

+0

@kai 내 업데이트 답변을 확인하십시오. 감사합니다. –

+0

나는이 팁을 시도하고 다시 연락 할 것이다. 고맙습니다! – kai