avoid unnecessary inner join in get_resources() for SQL backend

To get distinct resource ids, we do a query on resource table which
inner join sample table, and apply filters on it.

Note that when sql_expire_samples_only is enabled, there will be
some resources without any sample, in such case we must use inner
join to avoid wrong result, no matter if there is a timestamp filter
or not.

But that option is disabled by default, so when there is no timestamp
filters, the inner join is unnecessary, we should avoid it to save
some RAM/CPU cost.

Change-Id: If85dbea15d42d42c6b0be7402c06f258e278b2eb
Closes-Bug: #1509677
This commit is contained in:
ZhiQiang Fan 2015-10-24 09:30:53 -06:00
parent e8ddd5e814
commit a4f4429405
1 changed files with 10 additions and 3 deletions

View File

@ -431,9 +431,16 @@ class Connection(base.Connection):
session = self._engine_facade.get_session()
# get list of resource_ids
res_q = session.query(distinct(models.Resource.resource_id)).join(
models.Sample,
models.Sample.resource_id == models.Resource.internal_id)
has_timestamp = start_timestamp or end_timestamp
# NOTE: When sql_expire_samples_only is enabled, there will be some
# resources without any sample, in such case we should use inner
# join on sample table to avoid wrong result.
if cfg.CONF.sql_expire_samples_only or has_timestamp:
res_q = session.query(distinct(models.Resource.resource_id)).join(
models.Sample,
models.Sample.resource_id == models.Resource.internal_id)
else:
res_q = session.query(distinct(models.Resource.resource_id))
res_q = make_query_from_filter(session, res_q, s_filter,
require_meter=False)
res_q = res_q.limit(limit) if limit else res_q