Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of Elastic Time Range Queries against Out of Range Indexes

Tags:

It is common to have elastic indices with dates, in particular from something like logstash.

So for example, you have indices like foo-2016.05.01, foo-2016.05.02, etc...

When doing a time range query for data. What is the cost of querying indexes that I already know won't have data for that time range?

So for example if time range query only asks for data from 2016.05.02 but I also include the foo-2016.05.01 index in my query.

Is that basically a quick one-op per index where the index knows it has no data in that date range, or will doing this be costly to performance? I'm hoping not only to know the yes/no answer, but to understand why it behaves the way it does.

like image 347
Kyle Brandt Avatar asked May 19 '16 17:05

Kyle Brandt


2 Answers

Short version: it's probably going to be expensive. The cost will be n where n is the number of distinct field values for the date data. If all entries in the index had an identical date field value, it'd be a cheap query of 1 check (and would be pointless since it'd be a binary "all or nothing" response at that point). Of course, the reality is usually that every single doc has a unique date field value (which is incrementing such as in a log), depending on how granular the date is (assuming here that the time is included to seconds or milliseconds). Elasticsearch will check each aggregated, unique date field value of the included indices to try and find documents that match on the field by satisfying the predicates of the range query. This is the nature of the inverted index (indexing documents by their fields).

An easy way to improve performance is to change the Range Query to a Range Filter which caches results and improves performance for requests beyond the first one. Of course, this is only valuable if you're repeating the same range filter over time (the cache is read more than it is written), and if the range is not part of scoring the documents (that is to say those in range are not more valuable that those not in range when returning a set of both - also known as "boosting").

Another way to improve performance is by convention. If you query by day, store each day in its own rolling index and then do pre-search logic to select the indexes to query. This eliminates the need for the filter or query entirely.

like image 200
Haney Avatar answered Sep 28 '22 04:09

Haney


Elasticsearch doesn't care about the index name (that includes the date) and it doesn't automagically exclude that index from your range query. It will query all the shards (a copy - be it replica or primary) of all the indices specified in the query. Period.

Kibana, on the other hand, knows based on the time range selected to query specific indices only.

If you know your range will not make sense on some indices, then exclude those from the query before creating the query.

A common approach for logging usecase, in case the current day is most frequently queried is to create an alias. Give it a significant name - like today - that will always point to today's index. Also, common with time based indices is the retention period. For these two tasks - managing the aliases and deleting the "expired" indices - you can use Curator.

In case the most times you care about the current day, use that alias and thus you get rid of the days before today.

In case not, then filter the indices to be queried based on the range before deciding on which indices to run the query.

like image 32
Andrei Stefan Avatar answered Sep 28 '22 04:09

Andrei Stefan