Q:

DynamoDB Flask API : retrieve the result for particular item and iterate over the item

Need help on how to retrieve the result from dynamodb and iterate over the result
{
    "Count": 1,
    "Items": [
        {  "userID" :100,
            "userVisits": [
                {
                    "Accomapnied_relation": "Accomapnied Test relation",
                    "Accompanied_By": "Accompanied by test",
                    "Additional_Notes": "Additional notes Test",
                    "Advance_Directive": "Advance_Directive for visit id 123",
                    "Chief_Complaints": "Test Chief_Complaints for Visit Id 123",
                    "Condition_at_discharge": "Test Condition_at_discharge",
                    "Course_in_the_hospital": [
                        {
                            "Dose": "test Dose",
                            "Drug": "Test Drug",
                            "Duration": "3months",
                            "Given_DTTM": "Givend DTTM",
                            "Schedule": "Schedule Test"
                        }
                    ],
                    "mode_of_admission": "Test Attempt",
                    "userVisitId": "135"
                },
                {
                    "Accomapnied_relation": "Accomapnied Test relation",
                    "Accompanied_By": "Accompanied by test",
                    "Additional_Notes": "Additional notes Test",
                    "Advance_Directive": "Advance_Directive for visit id 123",
                    "Chief_Complaints": "Test Chief_Complaints for Visit Id 123",
                    "Condition_at_discharge": "Test Condition_at_discharge",
                    "Course_in_the_hospital": [
                        {
                            "Dose": "test Dose",
                            "Drug": "Test Drug",
                            "Duration": "3months",
                            "Given_DTTM": "Givend DTTM",
                            "Schedule": "Schedule Test"
                        }
                    ],
                    "mode_of_admission": "Suicide Attempt",
                    "userVisitId": "134"
                }
            ]
        }
    ],

}

I want to write an API which get the result from the above dynamodb, 
API GET : /api/users//visits/
the below is the code which i have written in flask 
@app.route("/api/users/<userID>/visits/<visitID>")
def getUserVisits(userID,visitID):
    userID = int(userID)
    visitId = str(visitId)

    result = table.query(
    ProjectionExpression="#userID, userVisits",
    ExpressionAttributeNames={ "#userID": "userID" }, # Expression Attribute Names for Projection Expression only.
    KeyConditionExpression=Key('userID').eq(userID)
    )
    if not result['Items']:
    raise  InvalidUsage('No User Visits Details found with User :{}  and visit Id :{} '.format(userID, visitID), status_code=410)
for  item in result['Items']:
    if not item:
        raise  InvalidUsage('No USer Visits Details found with User :{}  and visit Id :{} '.format(userID, visitID), status_code=410)
    for visits in item['userVisits']:
        if(visitId == visits['visitId']):
            return jsonify(visits), 200
        if not visits:
            raise  InvalidUsage('No User Visits Details found with User :{}  and visit Id :{} '.format(userID, visitID), status_code=410)

raise  InvalidUsage('No User Visits Details found with User :{}  and visit Id :{} '.format(userID, visitID), status_code=410)

The above is working fine but its taking lot time , is there any functionality in dynamoDB get the userVisits for that userID and visitID, can we improve the above code , I am new tho the Flask and DynamoDb , can you please help me on this to improve the code any best parctices.
Thanks a lot!!

A:

Firstly, you have mentioned that it takes lot of time without mentioning how long (in msecs) it took. There are only two operations in the visits API.
1) Dynamodb query to get the item. The Dynamodb Query API should not take more time as you are querying the table by partition key
2) Loop through the array to find the userVisits by userVisitId. Typically, this iteration shouldn't make more time for the medium size array. Also, Dynamodb item size can be only 400 KB maximum. So, I presume that the iteration should not take more time.
Still, if you think that the above solution takes more time. I would propose an alternate solution for the above data model.
Please restructure the data model using the below table definition. So that you can query the table directly using the partition and range key in KeyConditionExpression.

Partition Key : userID
Range Key : userVisitId

The alternate solution will eliminate the iteration step to match the userVisitId.

