Optimizing Queries in Bosch IoT Insights
You can optimize your queries in Bosch IoT Insights to improve the performance of your project. This tutorial provides you with hints that help you understand why your query might slow down the performance of your project.
The content of the Optimizing Queries in Bosch IoT Insights tutorial is also available as a video here.
Prerequisites
You have opened Explore > Data Explorer.
You have created a query template.
You have analyzed your query as described in Analyzing a query.
To optimize the query by using an index, you have requested to add this index with the Bosch IoT Insights support team.
This chapter details the following topics:
Limiting the results of a query by repositioning the limit parameter
Limiting the results of a query by using the greater than parameter
Limiting the results of a query by repositioning the limit parameter
You can place the limit at the top of a query to have it only run through the first results depending on the value you add. In our example below, 50.000 results are returned. Adjust the value as desired.
Example query:
[
{
"$limit"
:
50000
},
{
"$match"
: {
"payload.timestamp"
: {
"$gte"
:
"${range.from}"
,
"$lte"
:
"${range.to}"
}
}
},
{
"$project"
: {
"_id"
:
0
,
"Waermepumpe_Power"
:
"$payload.Waermepumpe_Power"
,
"BalkonPV_Power"
:
"$payload.BalkonPV_Power"
}
}
]
Limiting the results of a query by using the greater than parameter
You can add the parameter gte to only receive values greater than the specified value. In our example below, only values greater than 10 shall be returned from the heat pump and the balcony PV.
Example query:
[
{
"$match"
: {
"payload.timestamp"
: {
"$gte"
:
"${range.from}"
,
"$lte"
:
"${range.to}"
}
}
},
{
"$project"
: {
"_id"
:
0
,
"Waermepumpe_Power"
:
"$payload.Waermepumpe_Power"
,
"BalkonPV_Power"
:
"$payload.BalkonPV_Power"
}
},
{
"$match"
: {
"BalkonPV_Power"
: {
"$gte"
:
10
},
"Waermepumpe_Power"
: {
"$gte"
:
10
}
}
},
{
"$limit"
:
50000
}
]
Optimizing the query by using an index
You can add indexes to your collection to improve the performance of your project. This index is visible at the top next to the statistics. If you use an index, the entire database does not need to be scanned. In our example, we used payload.timestamp as index.
Example query:
[
{
"$match"
: {
"payload.timestamp"
: {
"$gte"
:
"${range.from}"
,
"$lte"
:
"${range.to}"
}
}
},
{
"$project"
: {
"_id"
:
0
,
"Waermepumpe_Power"
:
"$payload.Waermepumpe_Power"
,
"BalkonPV_Power"
:
"$payload.BalkonPV_Power"
}
},
{
"$limit"
:
50000
}
]