Question: Say I have this array in PHP.

```
$ids = [
    246,
    8362,
    5241,
    2586,
    6548,
    9372,
    28504,
    14,
    5729
];

```

These array elements correspond to items in a bucket 'articles' and sometimes this array is maybe like 1000 elements deep.

Currently, I am looping through all of them and pulling the data out 1 by 1.

```
$articles = [];
foreach($ids as $id)
{
    $articles[] = Riak::get("articles.$id");
}

```

This is more time than I'd like to spend when the lists of ids get pretty long.

Is there a faster way to pull a list of items out of a Riak bucket? I've looked around a bit and map-reduce is useful but apparently causes more overhead than I would save with sequential GET requests.

Answer: 
Getting large number of records from Riak efficiently is unfortunately hard as the are spread out across the cluster and have to be retrieved individually. Some of the client libraries have recently received support for client side multi-get, which allows the objects to be fetched in parallel using multiple connections, but I do not think this has reached the PHP client yet.

Using mapreduce to do this will reduce the number of calls to the database but will not result in a quorum read as it only queries a covering set of partitions. Mapreduce is however a reasonably extensive way of querying data and does not scale or perform nearly as well as direct key access.

If this is a common operation you will need to perform, it may be a sign that your data model is too normalised, and you may need to de-normalise in order to make it better support your application and its access patterns.

Exactly how to best do this requires knowledge about your data and your access and query patterns. One way might be to store multiple articles in a single object with a known and predictable key based on e.g. a fixed range of article ids. This will make retrieval of articles much more efficient as dramatically fewer objects need to be read but maybe cost you a bit more complexity when updating and inserting.

Another option is to de-normalise by duplicating data. If you e.g. only need some information from the articles, you could store this data in separate objects specifically aimed at efficiently serving these queries. 

It is in Riak generally better to retrieve larger objects with more information than you need and filter at the application level than try to get just exactly the records you need from the database.
Based on <https://github.com/basho/riak-php-client> PHP example:

```
/**
     * Retrieve JSON-encoded objects from Riak.
     * @param  array(int) $keys - List of the key.
     * @return RiakObjects
     */
    public function multiGet($keys) {
        if (!$keys || !count($keys))
            return array();
        $query = null;
        foreach ($keys as $key) {
            if ($query) {
                $query->add($this->getBucketName(), $key);
            } else {
                $query = $this->riak->add($this->getBucketName(), $key);
            }
        }
        $results = $query->map(('function(riakObject){return [{"key":riakObject.key, "document":riakObject.values}];}'))->run();
        if (!$results || !count($results)) {
            return array();
        }

        $objects = array();
        foreach ($results as $data) {
            if (isset($data->key) && isset($data->document[0]->data) && !empty($data->document[0]->data)) {
                $object = json_decode($data->document[0]->data, true);
                if ($object) {
                    $objects[$data->key] = $object;
                }
            }
        }
        return $objects;
    }

```