title: Most efficient way to query across partition with CosmosDB for bulk operations
body: <p>I have a cross partition query that returns the rows for each partition in turn, which makes sense, all of partition 1’s results, all of partition 2’s etc.</p>
  
  <p>For each row returned I need to perform an action, could be a delete or update.</p>
  
  <p>There are too many records to read them all in and then perform the actions, so I need to stream in the results and perform the actions at the same time.</p>
  
  <p>The issue I get is I run out of RU very quickly as my actions get run on each partition in turn and a single partition has a tenth of the RUs allocated.  </p>
  
  <p>I can specify a <code>PartitionKey</code> in the <code>FeedOptions</code> but that does not help me as I don’t know what the key will be.</p>
  
  <p>My query looks like</p>
  
  <pre><code>select r.* from r where r.deleted 
  </code></pre>
  
  <p>the <code>partition</code> is on a field called <code>container</code> </p>
  
  <p>Imagine I have the following items</p>
  
  <pre><code>container|title    |deleted
  jamjar   |jam      |true      &lt;--- stored in partition 5
  jar      |pickles  |true      &lt;--- stored in partition 5
  tin      |cookies  |true      &lt;--- stored in partition 8
  tub      |sweets   |true      &lt;--- stored in partition 9
  </code></pre>
  
  <p>I do <code>select r.title from r where r.deleted</code>
  my query will return the rows in the following order</p>
  
  <pre><code>jam      &lt;--- stored in partition 5
  pickles  &lt;--- stored in partition 5
  cookies  &lt;--- stored in partition 8
  sweets   &lt;--- stored in partition 9
  </code></pre>
  
  <p>I use an <code>ActionBlock</code> to allow me to spin up 2 threads to do my action on each row returned, so I work on <code>jam</code>  and <code>pickles</code> then <code>cookies</code> and <code>sweets</code> thus consuming RUs from partion 5 when I am carrying out the action on <code>jam</code> and <code>pickles</code></p>
  
  <p>I would like the results to be returned as: </p>
  
  <pre><code>jam      &lt;--- stored in partition 5
  cookies  &lt;--- stored in partition 8
  sweets   &lt;--- stored in partition 9
  pickles  &lt;--- stored in partition 5
  </code></pre>
  
  <p>For normal API calls we always know the <code>container</code>, this is a requirement for a bulk and very infrequent delete.</p>
  
  <p>If know the number of partition and could supply the partition number to the query that would be fine, I would be happy to issue 10 query and just treat this as 10 separate jobs.</p>