Fetching multiple SimpleDB items in a single request

The only way to retrieve multiple items at a time, along with their attributes, from Amazon SimpleDB is with the Select operation. Typically, you query for some property of the items which you want to retrieve, not by item name:

select * from my_domain where date > '2009-01-01'

The only documented way to retrieve the attributes of an item by item name is with the GetAttributes operation, which only gets one item at a time. But what if you have a list of item names you want to retrieve, and you just want to avoid the overhead of making a separate GetAttributes call for each one?

A clue towards the solution can be found in the fact that the special itemName() token can be used in the field list for Select:

select itemName() from my_domain where date > '2009-01-01'

I didn't find this explicitly documented anywhere, but you can also use itemName() in a query condition:

select * from my_domain where itemName() = 'ABC123'

…and from there it’s a short step to our solution, which uses the IN predicate:

select * from my_domain where itemName() in ('ABC123', 'DEF456', ...)

And there you have it, a way to retrieve multiple items by name in a single request, subject only to limits on query size and response size. I don’t know if this is common knowledge to longtime SimpleDB users, but it was new to me, so I thought I’d share it.

Creative Commons License
This work, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.

Related posts:

  1. Copying Amazon SimpleDB Domains
  2. The State of SimpleDB Clones
  3. Array-ifying Values
  4. ISO8601 Dates in Ruby
  5. Using Hashes as Caches
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.
  • robtweed

    Pleased to report that M/DB supports this mechanism too :-)

  • Peter

    Thank you! I spent 3h to figure out how to do that! Simple itemName()…

  • Peter

    Thank you! I spent 3h to figure out how to do that! Simple itemName()…