From bb93805e209a1a92a04ad5ef9d2ff49fa9c8ad8f Mon Sep 17 00:00:00 2001 From: Jon Ziebell Date: Sun, 21 Feb 2021 21:42:16 -0500 Subject: [PATCH] Optimized get_metrics query a bit Forced certain where clauses to take advantages of indexes. --- api/thermostat.php | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/api/thermostat.php b/api/thermostat.php index e5d1492..0cddbfe 100644 --- a/api/thermostat.php +++ b/api/thermostat.php @@ -291,13 +291,30 @@ class thermostat extends cora\crud { ); } - $keys_custom = [ - 'property_structure_type', + $keys_required_in_query = [ 'property_age', 'property_square_feet', 'property_stories' ]; - foreach($keys_custom as $key) { + foreach($keys_required_in_query as $key) { + if(isset($attributes[$key]) === true) { + $where[] = $this->database->column_equals_value_where( + $key, + $attributes[$key] + ); + } else { + // Fill these in for query performance. + $where[] = $this->database->column_equals_value_where( + $key, + ['operator' => '>', 'value' => 0] + ); + } + } + + $keys_optional_in_query = [ + 'property_structure_type', + ]; + foreach($keys_optional_in_query as $key) { if(isset($attributes[$key]) === true) { $where[] = $this->database->column_equals_value_where( $key, @@ -378,6 +395,10 @@ class thermostat extends cora\crud { $where[] = '`address_longitude` between ' . $between_a[0] . ' and ' . $between_a[1]; } } + } else { + // Fill these in for query performance. + $where[] = '`address_longitude` between -180 and 180'; + $where[] = '`address_latitude` between -90 and 90'; } // Should match their position in the thermostat profile exactly.