title: Simple Google App Engine Datastore operation taking too long: causing DeadlineExceededException/DatastoreTimeoutException
body: <p>I have a fairly simple App Engine Java app that has Accounts, Orders and OrderItems - nothing crazy.</p>
  
  <p>Just in the last 12 hours I have started getting exceptions thrown out of some fairly straight forward code that adds orders to accounts and then saves them.</p>
  
  <p>I created a trivial testing servlet to replicate the issue, it looks like this:</p>
  
  <pre><code>public void doGet(HttpServletRequest req, HttpServletResponse resp) 
      throws IOException {
  
          String key = req.getParameter("key");
  
          PersistenceManager pm = PMF.get().getPersistenceManager();
          Account account = pm.getObjectById(Account.class, KeyFactory.stringToKey(key));
  
          Order order = new Order();
          order.setExternalOrderID("ASHLEY-TESTING");
  
          Item item = new Item();
          item.setSku("ASHLEY-WIDGET-A");
          item.setQuantity(2);
          Item item2 = new Item();
          item2.setSku("ASHLEY-WIDGET-B");
          item2.setQuantity(2);
  
          order.addItem(item);
          order.addItem(item2);
  
          account.addOrder(order);
          order.setAccount(account);
          pm.makePersistent(order);
          pm.close();
      }
  
  addOrder is implemented as a pretty standard lazy init:
  
      public void addOrder(Order order) {
          if (getOrders() == null) {
              setOrders(new ArrayList&lt;Order&gt;());
          }
          getOrders().add(order);
      }
  
  The relevant parts of the entities:
  
  @PersistenceCapable
  
  public class Account {
  
  //...
  
  @Persistent(mappedBy="account")
  @javax.jdo.annotations.Order(extensions = @Extension(vendorName="datanucleus", key="list-ordering", value="orderDate desc"))
  private List&lt;Order&gt; orders;
  
  //...
  
  }
  
  and the Order has an account field:
  
      @Persistent
      private Account account;
  </code></pre>
  
  <p>This code is failing on the <code>account.addOrder()</code> line. if it is run directly in the browser it fails with DeadlineExceededException (after 30s) and if I enqueue it to run via a task queue it fails with DatastoreTimeoutException after a minute or two. It uses a truck load of CPU time in the process.</p>
  
  <p>I would estimate that the Account would have fewer than 2000 Orders children under it, each Order with 1-3 OrderItem children.</p>
  
  <p>My question is, why would this all of a sudden start failing it has worked to add the 1000's of orders already in there. And have I missed something vital? do I need to add indexes? could the data store really be this slow? Am I abusing it, should I not have children relationships with this many children - perhaps a set of keys would be a better approach?</p>