From 5be1ace5462994b492b811e20d8a90caac0fcffd Mon Sep 17 00:00:00 2001 From: Kevin Benton Date: Thu, 29 Jun 2017 15:46:21 -0700 Subject: [PATCH] Use subqueryload in l2pop DB for binding ports This adjusts the l2pop DB queries that leverage the port relationship on the PortBinding and DistributedPortBinding models to ensure that the port gets loaded from the DB as part of the initial query. Without this the number of DB queries will increase with the number of ports since the port wasn't being loaded until the 'port' attribute was referenced on its corresponding binding object. Closes-Bug: #1701288 Change-Id: I7c3b08d525b2c90100c9fe4efaee973cf3a076f3 --- neutron/plugins/ml2/drivers/l2pop/db.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/neutron/plugins/ml2/drivers/l2pop/db.py b/neutron/plugins/ml2/drivers/l2pop/db.py index 9cfedb14077..894a4bab187 100644 --- a/neutron/plugins/ml2/drivers/l2pop/db.py +++ b/neutron/plugins/ml2/drivers/l2pop/db.py @@ -16,6 +16,7 @@ from neutron_lib import constants as const from oslo_serialization import jsonutils from oslo_utils import timeutils +from sqlalchemy import orm from neutron.db.models import agent as agent_model from neutron.db.models import l3ha as l3ha_model @@ -70,6 +71,7 @@ def _get_active_network_ports(session, network_id): query = query.join(agent_model.Agent, agent_model.Agent.host == ml2_models.PortBinding.host) query = query.join(models_v2.Port) + query = query.options(orm.subqueryload(ml2_models.PortBinding.port)) query = query.filter(models_v2.Port.network_id == network_id, models_v2.Port.status == const.PORT_STATUS_ACTIVE) return query @@ -109,6 +111,8 @@ def get_dvr_active_network_ports(session, network_id): agent_model.Agent.host == ml2_models.DistributedPortBinding.host) query = query.join(models_v2.Port) + query = query.options( + orm.subqueryload(ml2_models.DistributedPortBinding.port)) query = query.filter(models_v2.Port.network_id == network_id, models_v2.Port.status == const.PORT_STATUS_ACTIVE, models_v2.Port.device_owner ==