From a4f442940551a8a38e26e132c7efa766226daa15 Mon Sep 17 00:00:00 2001 From: ZhiQiang Fan Date: Sat, 24 Oct 2015 09:30:53 -0600 Subject: [PATCH] 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 --- ceilometer/storage/impl_sqlalchemy.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/ceilometer/storage/impl_sqlalchemy.py b/ceilometer/storage/impl_sqlalchemy.py index b9a846ea..c0b812a8 100644 --- a/ceilometer/storage/impl_sqlalchemy.py +++ b/ceilometer/storage/impl_sqlalchemy.py @@ -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