0

I'm using Google Cloud Function with google-cloud/datastore modules. My data is structured as 1 kind with 4 string properties, only 1000 entities, indexed on all properties. My query is:

if (/^[a-z0-9]+$/i.test(name)) {
  name = name.toLowerCase();
  query = datastore.createQuery('IPPhone').filter('email', '>=', name).filter('email', '<', name + '\uffff');
} else if (name.includes('<')) {
  query = datastore.createQuery('IPPhone').filter('department', '>=', name).filter('department', '<', name + '\uffff');
  isDepartment = true;
} else {
  name = fixName(name);
  query = datastore.createQuery('IPPhone').filter('name', '=', name);
}

When I query from Google Cloud Function the query time is 14-17 second. However, doing the same thing on my local machine, the query time is much shorter around 800 - 1000 ms. I'm from Hanoi, Vietnam but the only option I can choose for Cloud Function is us-central1.

BrettJ
  • 6,801
  • 1
  • 23
  • 26
  • In general it's not a good idea to compare cloud vs local machine performance. See this answer (it's about GAE, not Cloud Functions, I know, but some of the same reasoning applies): https://stackoverflow.com/questions/41389295/why-does-google-cloud-sql-using-jdbc-take-longer-to-insert-records-from-google/41390857#41390857 – Dan Cornilescu Aug 01 '17 at 13:38
  • Thank you for your information. However, I have timed every part of my Cloud Function. The part where I create query and use the results was very fast (< 100 ms), only the part between right before I ran the query and when I got the results took questionably long (14-17 seconds). What can I do to fix it beside upgrading GAE instance class? – Nguyễn Huy Aug 01 '17 at 14:02
  • Where is your datastore location? https://cloud.google.com/datastore/docs/locations. If it's not `us-central` then you could try using that. – Dan Cornilescu Aug 01 '17 at 14:19
  • My project dashboard show that I have these resources App Engine, Cloud Functions, and Cloud Storage. After checking, it seem they're all in us region http://i.imgur.com/SyhFDAx.png – Nguyễn Huy Aug 01 '17 at 15:06
  • Can you also check the AppEngine dashboard in your project? – Dan Cornilescu Aug 01 '17 at 17:03
  • Thank you for your help. Turn out I should declare my datastore first in the function, not right before I need to use it. – Nguyễn Huy Aug 02 '17 at 06:29
  • You should detail that with code snippets in an answer to your own question, it could help others. – Dan Cornilescu Aug 02 '17 at 13:26

0 Answers0