Commit 374cf2a14c311d9bab6f852e2bc083af24c066ec

Authored by Andrii Shvaika
1 parent 8c0ae686

Performance improvement

... ... @@ -261,8 +261,6 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository {
261 261 .collect(Collectors.toList());
262 262 List<EntityKeyMapping> entityFieldsFiltersMapping = filterMapping.stream().filter(mapping -> !mapping.isLatest())
263 263 .collect(Collectors.toList());
264   - List<EntityKeyMapping> latestFiltersMapping = filterMapping.stream().filter(EntityKeyMapping::isLatest)
265   - .collect(Collectors.toList());
266 264
267 265 List<EntityKeyMapping> allLatestMappings = mappings.stream().filter(EntityKeyMapping::isLatest)
268 266 .collect(Collectors.toList());
... ... @@ -271,7 +269,6 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository {
271 269 String entityWhereClause = DefaultEntityQueryRepository.this.buildEntityWhere(ctx, query.getEntityFilter(), entityFieldsFiltersMapping);
272 270 String latestJoinsCnt = EntityKeyMapping.buildLatestJoins(ctx, query.getEntityFilter(), entityType, allLatestMappings, true);
273 271 String latestJoinsData = EntityKeyMapping.buildLatestJoins(ctx, query.getEntityFilter(), entityType, allLatestMappings, false);
274   - String whereClause = DefaultEntityQueryRepository.this.buildWhere(ctx, latestFiltersMapping, query.getEntityFilter().getType());
275 272 String textSearchQuery = DefaultEntityQueryRepository.this.buildTextSearchQuery(ctx, selectionMapping, pageLink.getTextSearch());
276 273 String entityFieldsSelection = EntityKeyMapping.buildSelections(entityFieldsSelectionMapping, query.getEntityFilter().getType(), entityType);
277 274 String entityTypeStr;
... ... @@ -291,22 +288,20 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository {
291 288 topSelection = topSelection + ", " + latestSelection;
292 289 }
293 290
294   - String fromClauseCount = String.format("from (select %s from (select %s from %s e where %s) entities %s %s) result %s",
  291 + String fromClauseCount = String.format("from (select %s from (select %s from %s e where %s) entities %s ) result %s",
295 292 "entities.*",
296 293 entityFieldsSelection,
297 294 addEntityTableQuery(ctx, query.getEntityFilter()),
298 295 entityWhereClause,
299 296 latestJoinsCnt,
300   - whereClause,
301 297 textSearchQuery);
302 298
303   - String fromClauseData = String.format("from (select %s from (select %s from %s e where %s) entities %s %s) result %s",
  299 + String fromClauseData = String.format("from (select %s from (select %s from %s e where %s) entities %s ) result %s",
304 300 topSelection,
305 301 entityFieldsSelection,
306 302 addEntityTableQuery(ctx, query.getEntityFilter()),
307 303 entityWhereClause,
308 304 latestJoinsData,
309   - whereClause,
310 305 textSearchQuery);
311 306
312 307 if (!StringUtils.isEmpty(pageLink.getTextSearch())) {
... ... @@ -547,15 +542,6 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository {
547 542 return from;
548 543 }
549 544
550   - private String buildWhere(QueryContext ctx, List<EntityKeyMapping> latestFiltersMapping, EntityFilterType filterType) {
551   - String latestFilters = EntityKeyMapping.buildQuery(ctx, latestFiltersMapping, filterType);
552   - if (!StringUtils.isEmpty(latestFilters)) {
553   - return String.format("where %s", latestFilters);
554   - } else {
555   - return "";
556   - }
557   - }
558   -
559 545 private String buildTextSearchQuery(QueryContext ctx, List<EntityKeyMapping> selectionMapping, String searchText) {
560 546 if (!StringUtils.isEmpty(searchText) && !selectionMapping.isEmpty()) {
561 547 String lowerSearchText = searchText.toLowerCase() + "%";
... ...
... ... @@ -16,6 +16,7 @@
16 16 package org.thingsboard.server.dao.sql.query;
17 17
18 18 import lombok.Data;
  19 +import org.springframework.util.StringUtils;
19 20 import org.thingsboard.server.common.data.DataConstants;
20 21 import org.thingsboard.server.common.data.EntityType;
21 22 import org.thingsboard.server.common.data.query.BooleanFilterPredicate;
... ... @@ -224,7 +225,7 @@ public class EntityKeyMapping {
224 225 return keyFilters.stream().map(keyFilter ->
225 226 this.buildKeyQuery(ctx, keyAlias, keyFilter, filterType));
226 227 } else {
227   - return null;
  228 + return Stream.empty();
228 229 }
229 230 }
230 231
... ... @@ -236,15 +237,22 @@ public class EntityKeyMapping {
236 237 entityTypeStr = "'" + entityType.name() + "'";
237 238 }
238 239 ctx.addStringParameter(alias + "_key_id", entityKey.getKey());
  240 + String filterQuery = toQueries(ctx, entityFilter.getType()).filter(Objects::nonNull).collect(
  241 + Collectors.joining(" and "));
  242 + if (StringUtils.isEmpty(filterQuery)) {
  243 + filterQuery = "";
  244 + } else {
  245 + filterQuery = " AND (" + filterQuery + ")";
  246 + }
239 247 if (entityKey.getType().equals(EntityKeyType.TIME_SERIES)) {
240 248 String join = hasFilter() ? "left join" : "left outer join";
241   - return String.format("%s ts_kv_latest %s ON %s.entity_id=entities.id AND %s.key = (select key_id from ts_kv_dictionary where key = :%s_key_id)",
242   - join, alias, alias, alias, alias);
  249 + return String.format("%s ts_kv_latest %s ON %s.entity_id=entities.id AND %s.key = (select key_id from ts_kv_dictionary where key = :%s_key_id) %s",
  250 + join, alias, alias, alias, alias, filterQuery);
243 251 } else {
244 252 String query;
245 253 if (!entityKey.getType().equals(EntityKeyType.ATTRIBUTE)) {
246 254 String join = hasFilter() ? "left join" : "left outer join";
247   - query = String.format("%s attribute_kv %s ON %s.entity_id=entities.id AND %s.entity_type=%s AND %s.attribute_key=:%s_key_id",
  255 + query = String.format("%s attribute_kv %s ON %s.entity_id=entities.id AND %s.entity_type=%s AND %s.attribute_key=:%s_key_id ",
248 256 join, alias, alias, alias, entityTypeStr, alias, alias);
249 257 String scope;
250 258 if (entityKey.getType().equals(EntityKeyType.CLIENT_ATTRIBUTE)) {
... ... @@ -254,12 +262,12 @@ public class EntityKeyMapping {
254 262 } else {
255 263 scope = DataConstants.SERVER_SCOPE;
256 264 }
257   - query = String.format("%s AND %s.attribute_type='%s'", query, alias, scope);
  265 + query = String.format("%s AND %s.attribute_type='%s' %s", query, alias, scope, filterQuery);
258 266 } else {
259 267 String join = hasFilter() ? "join LATERAL" : "left join LATERAL";
260   - query = String.format("%s (select * from attribute_kv %s WHERE %s.entity_id=entities.id AND %s.entity_type=%s AND %s.attribute_key=:%s_key_id " +
  268 + query = String.format("%s (select * from attribute_kv %s WHERE %s.entity_id=entities.id AND %s.entity_type=%s AND %s.attribute_key=:%s_key_id %s " +
261 269 "ORDER BY %s.last_update_ts DESC limit 1) as %s ON true",
262   - join, alias, alias, alias, entityTypeStr, alias, alias, alias, alias);
  270 + join, alias, alias, alias, entityTypeStr, alias, alias, filterQuery, alias, alias);
263 271 }
264 272 return query;
265 273 }
... ...