python - MongoEngine Query Optimization -
i have 2 collections scenariodrivers
, modeldrivers
has 1 many relationship each other.
class scenariodrivers(document): meta = { 'collection': 'scenariodrivers' } scenarioid = referencefield('modelscenarios') driverid = referencefield('modeldrivers') drivercalibrationmethod = stringfield() segmentname = stringfield() drivervalue = listfield() calibrationstatus = stringfield() adjustedvalues = listfield(default=[]) createdate = datetimefield(default=objectid().generation_time) lastupdatedate = datetimefield(default=datetime.utcnow()) class modeldrivers(document): meta = { 'collection': 'modeldrivers' } portfoliomodelid = referencefield('portfoliomodels') drivername = stringfield() createdate = datetimefield(default=objectid().generation_time) lastupdatedate = datetimefield(default=datetime.utcnow()) fieldformat = stringfield() driverdata = listfield()
my query this.
class getcalibrateddrivers(resource): def get(self, scenario_id): scenario_drivers_list = [] scenario_drivers = scenariodrivers.objects(scenarioid=scenario_id).exclude('scenarioid').select_related(1) scenario_driver in scenario_drivers: scenario_driver_dict = { 'id': str(scenario_driver.id), 'driverid': str(scenario_driver.driverid.id), 'segmentname': scenario_driver.segmentname, 'calibrationmethod': scenario_driver.drivercalibrationmethod, 'calibratedvalues': exchange(scenario_driver.drivervalue), 'adjustedvalues': scenario_driver.adjustedvalues, 'lastupdatedate': formatted_date(scenario_driver.lastupdatedate), 'fieldformat': scenario_driver.driverid.fieldformat } scenario_drivers_list.append(scenario_driver_dict) return { 'drivercalibrations': scenario_drivers_list }
the query matches 1140
records , construct dictionary , make list.
but api call takes 30s
process 1140
records. missing? please help. using latest version of pymongo , mongoengine.
i think problem not query, looping on 1140 records. not see use of referenced objects should consider removing select_related(1)
. once that, if want convert reference object ids string, can use as_pymongo()
default you. , if must read data in specific format formatted_date
or exchange
, better save them part of document. i.e. save formattedlastupdatedate
lastupdatedate
. in mongodb, have think read specific logic when save document.
Comments
Post a Comment