Improve dimension filtering performance

Much faster to connect the tables on a different ID

Change-Id: I141710287f77918389ee6634b5a072f002219471
This commit is contained in:
Ryan Brandt 2016-08-26 14:02:25 -06:00
parent e98d29bd0e
commit 8e679ff720
2 changed files with 20 additions and 11 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014,2016 Hewlett Packard Enterprise Development Company, L.P. * (C) Copyright 2014,2016 Hewlett Packard Enterprise Development LP
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at * in compliance with the License. You may obtain a copy of the License at
@ -92,8 +92,11 @@ final class MetricQueries {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(" and ").append(tableToJoinName).append( sb.append(" and ").append(tableToJoinName).append(
".dimension_set_id in ( " ".id in ( "
+ "SELECT dimension_set_id FROM MonMetrics.Dimensions WHERE ("); + "SELECT defDimsSub2.id FROM MonMetrics.Dimensions AS dimSub " +
"JOIN MonMetrics.DefinitionDimensions AS defDimsSub2 " +
"ON defDimsSub2.dimension_set_id = dimSub.dimension_set_id" +
" WHERE (");
int i = 0; int i = 0;
for (Iterator<Map.Entry<String, String>> it = dimensions.entrySet().iterator(); it.hasNext(); i++) { for (Iterator<Map.Entry<String, String>> it = dimensions.entrySet().iterator(); it.hasNext(); i++) {
@ -128,7 +131,7 @@ final class MetricQueries {
} }
} }
sb.append(") GROUP BY dimension_set_id HAVING count(*) = ").append(dimensions.size()).append(") "); sb.append(") GROUP BY defDimsSub2.id,dimSub.dimension_set_id HAVING count(*) = ").append(dimensions.size()).append(") ");
return sb.toString(); return sb.toString();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014,2016 Hewlett Packard Enterprise Development Company, L.P. * (C) Copyright 2014,2016 Hewlett Packard Enterprise Development LP
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at * in compliance with the License. You may obtain a copy of the License at
@ -28,9 +28,11 @@ public class MetricQueriesTest {
public void metricQueriesBuildDimensionAndClauseTest1() { public void metricQueriesBuildDimensionAndClauseTest1() {
String expectedResult = String expectedResult =
" and defdims.dimension_set_id in ( SELECT dimension_set_id FROM MonMetrics.Dimensions WHERE" " and defdims.id in ( SELECT defDimsSub2.id FROM MonMetrics.Dimensions AS dimSub "
+ "JOIN MonMetrics.DefinitionDimensions AS defDimsSub2 "
+ "ON defDimsSub2.dimension_set_id = dimSub.dimension_set_id WHERE"
+ " ((name = :dname0 and value = :dvalue0) or (name = :dname1 and value = :dvalue1))" + " ((name = :dname0 and value = :dvalue0) or (name = :dname1 and value = :dvalue1))"
+ " GROUP BY dimension_set_id HAVING count(*) = 2) "; + " GROUP BY defDimsSub2.id,dimSub.dimension_set_id HAVING count(*) = 2) ";
Map<String, String> dimsMap = new HashMap<>(); Map<String, String> dimsMap = new HashMap<>();
dimsMap.put("foo", "bar"); dimsMap.put("foo", "bar");
@ -54,9 +56,11 @@ public class MetricQueriesTest {
public void metricQueriesBuildDimensionAndClauseTest4() { public void metricQueriesBuildDimensionAndClauseTest4() {
String expectedResult = String expectedResult =
" and defdims.dimension_set_id in ( SELECT dimension_set_id FROM MonMetrics.Dimensions WHERE" " and defdims.id in ( SELECT defDimsSub2.id FROM MonMetrics.Dimensions AS dimSub "
+ "JOIN MonMetrics.DefinitionDimensions AS defDimsSub2 "
+ "ON defDimsSub2.dimension_set_id = dimSub.dimension_set_id WHERE"
+ " ((name = :dname0 and ( value = :dvalue0_0 or value = :dvalue0_1)))" + " ((name = :dname0 and ( value = :dvalue0_0 or value = :dvalue0_1)))"
+ " GROUP BY dimension_set_id HAVING count(*) = 1) "; + " GROUP BY defDimsSub2.id,dimSub.dimension_set_id HAVING count(*) = 1) ";
Map<String, String> dimsMap = new HashMap<>(); Map<String, String> dimsMap = new HashMap<>();
dimsMap.put("foo", "bar|baz"); dimsMap.put("foo", "bar|baz");
@ -67,10 +71,12 @@ public class MetricQueriesTest {
public void metricQueriesBuildDimensionAndClauseTest5() { public void metricQueriesBuildDimensionAndClauseTest5() {
String expectedResult = String expectedResult =
" and defdims.dimension_set_id in ( SELECT dimension_set_id FROM MonMetrics.Dimensions WHERE" " and defdims.id in ( SELECT defDimsSub2.id FROM MonMetrics.Dimensions AS dimSub "
+ "JOIN MonMetrics.DefinitionDimensions AS defDimsSub2 "
+ "ON defDimsSub2.dimension_set_id = dimSub.dimension_set_id WHERE"
+ " ((name = :dname0 and ( value = :dvalue0_0 or value = :dvalue0_1))" + " ((name = :dname0 and ( value = :dvalue0_0 or value = :dvalue0_1))"
+ " or (name = :dname1 and ( value = :dvalue1_0 or value = :dvalue1_1)))" + " or (name = :dname1 and ( value = :dvalue1_0 or value = :dvalue1_1)))"
+ " GROUP BY dimension_set_id HAVING count(*) = 2) "; + " GROUP BY defDimsSub2.id,dimSub.dimension_set_id HAVING count(*) = 2) ";
Map<String, String> dimsMap = new HashMap<>(); Map<String, String> dimsMap = new HashMap<>();
dimsMap.put("foo", "bar|baz"); dimsMap.put("foo", "bar|baz");