Use explicit inner join for networks in port query

This improves the performance of the database when fetching a list of ports
for a project user. This change creates an inner join with the networks
belonging to the ports.

Previous SQL query:
SELECT ports ...
FROM network, ports ...
WHERE ports.project_id = <project>
OR ports.network_id = networks.id
AND networks.project_id = <project>

Current SQL query:
SELECT ports ...
FROM ports
INNER JOIN networks ON networks.id = ports.network_id
WHERE ports.project_id = <project>
OR networks.project_id = <project>

Closes-Bug: #2016704
Change-Id: I9c49a307956ecfbf8bd2e866cefb21a212c38bd6
This commit is contained in:
Ihtisham ul Haq 2023-04-17 12:23:30 +02:00
parent cda5107e65
commit f23d7af8d7
1 changed files with 9 additions and 2 deletions

View File

@ -110,11 +110,18 @@ def _update_subnetpool_dict(orig_pool, new_pool):
return updated
def _port_query_hook(context, original_model, query):
# Apply the port query only in non-admin and non-advsvc context
if ndb_utils.model_query_scope_is_project(context, original_model):
query = query.join(models_v2.Network,
models_v2.Network.id == models_v2.Port.network_id)
return query
def _port_filter_hook(context, original_model, conditions):
# Apply the port filter only in non-admin and non-advsvc context
if ndb_utils.model_query_scope_is_project(context, original_model):
conditions |= and_(
models_v2.Port.network_id == models_v2.Network.id,
models_v2.Network.project_id == context.project_id)
return conditions
@ -150,7 +157,7 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
model_query.register_hook(
models_v2.Port,
"port",
query_hook=None,
query_hook=_port_query_hook,
filter_hook=_port_filter_hook,
result_filters=None)
return super(NeutronDbPluginV2, cls).__new__(cls, *args, **kwargs)