node.js - How to improve the performance of the query -


r.db('db').table('tb').group('message').map(function(doc){   return {     count: 1,     from: doc('timestamp'),     to: doc('timestamp'),     browsers: r([doc('ua')('family')])   } }).reduce(function(left, right){   return {     count: left('count').add(right('count')),     from: r.branch(left('from').lt(right('from')), left('from'), right('from')),     to: r.branch(left('to').gt(right('to')), left('to'), right('to')),     browsers: left('browsers').setunion(right('browsers'))   } }).ungroup().orderby(r.desc(r.row('reduction')('count'))).limit(100) 

what want to:

  1. group documents message
  2. find earliest , latest timestamp each group
  3. find distinct tags in group

i result like:

{     'message': 'message content shared group',     'earliest': 1431307840000,     'latest': 1431307849999,     'tags': ['tag1', 'tag2', 'tag3'] } 

i need perform query quite (to render initial content of webpage), performance concern. performance upsetting. table of 15k documents, query executed 4.5s. any idea on how improve?

the profile of query here:

[   {     "description": "evaluating limit.",     "duration(ms)": 4134.771499,     "sub_tasks": [       {         "description": "evaluating orderby.",         "duration(ms)": 4134.327403,         "sub_tasks": [           {             "description": "evaluating ungroup.",             "duration(ms)": 4113.544033,             "sub_tasks": [               {                 "description": "evaluating reduce.",                 "duration(ms)": 4113.241578,                 "sub_tasks": [                   {                     "description": "evaluating map.",                     "duration(ms)": 0.082467,                     "sub_tasks": [                       {                         "description": "evaluating group.",                         "duration(ms)": 0.073697,                         "sub_tasks": [                           {                             "description": "evaluating table.",                             "duration(ms)": 0.028392,                             "sub_tasks": [                               {                                 "description": "evaluating db.",                                 "duration(ms)": 0.015104,                                 "sub_tasks": [                                   {                                     "description": "evaluating datum.",                                     "duration(ms)": 0.001565,                                     "sub_tasks": []                                   }                                 ]                               },                               {                                 "description": "evaluating datum.",                                 "duration(ms)": 0.000356,                                 "sub_tasks": []                               }                             ]                           },                           {                             "description": "evaluating datum.",                             "duration(ms)": 0.000331,                             "sub_tasks": []                           }                         ]                       },                       {                         "description": "evaluating func.",                         "duration(ms)": 0.00122,                         "sub_tasks": []                       }                     ]                   },                   {                     "description": "evaluating func.",                     "duration(ms)": 0.000805,                     "sub_tasks": []                   },                   {                     "description": "perform read.",                     "duration(ms)": 4112.207454,                     "sub_tasks": [                       {                         "parallel_tasks": [                           [                             {                               "description": "perform read on shard.",                               "duration(ms)": 4044.006493,                               "sub_tasks": [                                 {                                   "description": "do range scan on primary index.",                                   "duration(ms)": 4043.993961,                                   "sub_tasks": []                                 }                               ]                             }                           ],                           [                             {                               "description": "perform read on shard.",                               "duration(ms)": 4078.153662,                               "sub_tasks": [                                 {                                   "description": "do range scan on primary index.",                                   "duration(ms)": 4078.142774,                                   "sub_tasks": []                                 }                               ]                             }                           ],                           [                             {                               "description": "perform read on shard.",                               "duration(ms)": 3972.17768,                               "sub_tasks": [                                 {                                   "description": "do range scan on primary index.",                                   "duration(ms)": 3972.167594,                                   "sub_tasks": []                                 }                               ]                             }                           ],                           [                             {                               "description": "perform read on shard.",                               "duration(ms)": 4024.771149,                               "sub_tasks": [                                 {                                   "description": "do range scan on primary index.",                                   "duration(ms)": 4024.760748,                                   "sub_tasks": []                                 }                               ]                             }                           ],                           [                             {                               "description": "perform read on shard.",                               "duration(ms)": 4036.958792,                               "sub_tasks": [                                 {                                   "description": "do range scan on primary index.",                                   "duration(ms)": 4036.948958,                                   "sub_tasks": []                                 }                               ]                             }                           ],                           [                             {                               "description": "perform read on shard.",                               "duration(ms)": 3995.32926,                               "sub_tasks": [                                 {                                   "description": "do range scan on primary index.",                                   "duration(ms)": 3995.318271,                                   "sub_tasks": []                                 }                               ]                             }                           ],                           [                             {                               "description": "perform read on shard.",                               "duration(ms)": 3916.118836,                               "sub_tasks": [                                 {                                   "description": "do range scan on primary index.",                                   "duration(ms)": 3916.106312,                                   "sub_tasks": []                                 }                               ]                             }                           ],                           [                             {                               "description": "perform read on shard.",                               "duration(ms)": 3970.473735,                               "sub_tasks": [                                 {                                   "description": "do range scan on primary index.",                                   "duration(ms)": 3970.462526,                                   "sub_tasks": []                                 }                               ]                             }                           ]                         ]                       }                     ]                   }                 ]               }             ]           },           {             "description": "evaluating desc.",             "duration(ms)": 0.003855,             "sub_tasks": [               {                 "description": "evaluating func.",                 "duration(ms)": 0.002151,                 "sub_tasks": []               }             ]           },           {             "description": "sorting in-memory.",             "mean_duration(ms)": 0.014517,             "n_samples": 1245           }         ]       },       {         "description": "evaluating datum.",         "duration(ms)": 0.000359,         "sub_tasks": []       },       {         "description": "evaluating stream eagerly.",         "mean_duration(ms)": 0.000061,         "n_samples": 101       }     ]   } ] 


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -