0

I am trying to take values from a DB table where It has keys as enabled and values as true, false and empty, I want to take only True values from enabled keys and other keys and rows related to it.

Table

    id    name    enabled dept
     1    abd     TRUE    cs
     2    cdew    FALSE   ds
     3    sda             sd
     4    asd     TRUE    as

I want only enabled = true values from table. how can I do it? without help of pandas? I want like

    id    name    enabled dept
     1    abd     TRUE    cs
     4    asd     TRUE    as
6
  • Hi, @Mia ! Which database do you use? In case of SQL databases like MySQL or PostgreSQL it would be better to use SQL for filtering data, not Python. Commented Nov 7, 2022 at 11:43
  • from aws dynamodb Commented Nov 7, 2022 at 11:46
  • 1
    Are you using the Python API? boto3.amazonaws.com/v1/documentation/api/latest/reference/… KeyConditionExpression=Key('enabled').eq(True) Commented Nov 7, 2022 at 12:00
  • no im using table scan with ProjectionExpression and ExpressionAttributeNames Commented Nov 7, 2022 at 12:12
  • 1
    Better create a GSI on the enabled column and use the query operation instead of scan. Scan operation is costly as your data will increase Commented Nov 7, 2022 at 12:29

2 Answers 2

1

Best thing to do is create a Sparse Index which only had the True values.

To do this, only set enabled for True values and if False just omit it as DynamoDB is schemaless.

Now create a GSI with the partition or sort key as enabled, now the GSI will only contain the items you want. To fetch them simply Scan the GSI. GSI:

id(pk)  enabled(sk) name   dept
 1       TRUE       abd     cs
 4       TRUE       asd     as
Sign up to request clarification or add additional context in comments.

2 Comments

pls send any docs related to the exact GSI
0

I solved it using below code. thanks all for your help

resp= key.get('enabled',None)
if resp:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.