PynamoDB icon indicating copy to clipboard operation
PynamoDB copied to clipboard

multithread scan table spend so much time

Open gangchzu opened this issue 1 year ago • 1 comments

my code is below: from concurrent.futures import thread from typing import List from pynamodb.attributes import UnicodeAttribute, NumberAttribute from pynamodb.models import Model

import time import threading

class WorkBaseModel(Model): instance_id = UnicodeAttribute(default="", hash_key=True) worker_id = UnicodeAttribute(default="", range_key=True) version = UnicodeAttribute(default="0") region_name = UnicodeAttribute(default="") instance_state = UnicodeAttribute(default="") work_state = NumberAttribute(default=0) last_report_time = NumberAttribute(default=0) create_time = NumberAttribute(default=0) protected = NumberAttribute(default=0) instance_type = UnicodeAttribute(default="")

class MetaBase: aws_access_key_id = "" aws_secret_access_key = "" max_retry_attempts = 6 max_pool_connections = 40

class ConferenceAssistantInstance(Model): instance_id = UnicodeAttribute(default="", hash_key=True) instance_state = UnicodeAttribute(default="") create_time = NumberAttribute() protected = NumberAttribute(default=0) region_name = UnicodeAttribute(default="")

class Meta(MetaBase):
    table_name = "conference_assistant_instance"
    region = "ap-northeast-1"

if name == "main":

def run(i):
    now = time.time()
    all_instance_list: List[ConferenceAssistantInstance] = list(
        ConferenceAssistantInstance.scan()
    )
    print(len(all_instance_list))
    print(f"user time: {i}", time.time()-now)

threads = []
for i in range(40):
    t = threading.Thread(target=run, args=(i,))
    threads.append(t)
    t.start()
    
for thread in threads:
    thread.join()
    
print('over')

I have 100 items in table conference_assistant_instance, when I run this script, spend time is about 3 seconds, but when I replace scan by boto3, it is just 100 milliseconds

gangchzu avatar Sep 18 '24 13:09 gangchzu

Could you test with version 5.5.1? Maybe this performance issue is related to my issue #1253

jbsilva avatar Jan 16 '25 15:01 jbsilva