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:

<pre class="lang:mysql decode:true " >
select itemName() from my_domain where date > '2009-01-01'
</pre>

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.

4 comments

Leave a Reply

Your email address will not be published. Required fields are marked *