TopN queries

TopN - 图1info

Apache Druid supports two query languages: Druid SQL and native queries. This document describes a query type in the native language. For information about when Druid SQL will use this query type, refer to the SQL documentation.

Apache Druid TopN queries return a sorted set of results for the values in a given dimension according to some criteria. Conceptually, they can be thought of as an approximate GroupByQuery over a single dimension with an Ordering spec. TopNs are much faster and resource efficient than GroupBys for this use case. These types of queries take a topN query object and return an array of JSON objects where each object represents a value asked for by the topN query.

TopNs are approximate in that each data process will rank their top K results and only return those top K results to the Broker. K, by default in Druid, is max(1000, threshold). In practice, this means that if you ask for the top 1000 items ordered, the correctness of the first ~900 items will be 100%, and the ordering of the results after that is not guaranteed. TopNs can be made more accurate by increasing the threshold.

A topN query object looks like:

  1. {
  2. "queryType": "topN",
  3. "dataSource": "sample_data",
  4. "dimension": "sample_dim",
  5. "threshold": 5,
  6. "metric": "count",
  7. "granularity": "all",
  8. "filter": {
  9. "type": "and",
  10. "fields": [
  11. {
  12. "type": "selector",
  13. "dimension": "dim1",
  14. "value": "some_value"
  15. },
  16. {
  17. "type": "selector",
  18. "dimension": "dim2",
  19. "value": "some_other_val"
  20. }
  21. ]
  22. },
  23. "aggregations": [
  24. {
  25. "type": "longSum",
  26. "name": "count",
  27. "fieldName": "count"
  28. },
  29. {
  30. "type": "doubleSum",
  31. "name": "some_metric",
  32. "fieldName": "some_metric"
  33. }
  34. ],
  35. "postAggregations": [
  36. {
  37. "type": "arithmetic",
  38. "name": "average",
  39. "fn": "/",
  40. "fields": [
  41. {
  42. "type": "fieldAccess",
  43. "name": "some_metric",
  44. "fieldName": "some_metric"
  45. },
  46. {
  47. "type": "fieldAccess",
  48. "name": "count",
  49. "fieldName": "count"
  50. }
  51. ]
  52. }
  53. ],
  54. "intervals": [
  55. "2013-08-31T00:00:00.000/2013-09-03T00:00:00.000"
  56. ]
  57. }

There are 11 parts to a topN query.

propertydescriptionrequired?
queryTypeThis String should always be “topN”; this is the first thing Druid looks at to figure out how to interpret the queryyes
dataSourceA String or Object defining the data source to query, very similar to a table in a relational database. See DataSource for more information.yes
intervalsA JSON Object representing ISO-8601 Intervals. This defines the time ranges to run the query over.yes
granularityDefines the granularity to bucket query results. See Granularitiesyes
filterSee Filtersno
virtualColumnsA JSON list of virtual columns. You can reference a virtual column as the grouping dimension or as an input in aggregations or postAggregations.no (default none)
aggregationsSee Aggregationsfor numeric metricSpec, aggregations or postAggregations should be specified. Otherwise no.
postAggregationsSee Post Aggregationsfor numeric metricSpec, aggregations or postAggregations should be specified. Otherwise no.
dimensionA String or JSON object defining the dimension that you want the top taken for. For more info, see DimensionSpecsyes
thresholdAn integer defining the N in the topN (i.e. how many results you want in the top list)yes
metricA String or JSON object specifying the metric to sort by for the top list. For more info, see TopNMetricSpec.yes
contextSee Contextno

Please note the context JSON object is also available for topN queries and should be used with the same caution as the timeseries case. The format of the results would look like so:

  1. [
  2. {
  3. "timestamp": "2013-08-31T00:00:00.000Z",
  4. "result": [
  5. {
  6. "dim1": "dim1_val",
  7. "count": 111,
  8. "some_metrics": 10669,
  9. "average": 96.11711711711712
  10. },
  11. {
  12. "dim1": "another_dim1_val",
  13. "count": 88,
  14. "some_metrics": 28344,
  15. "average": 322.09090909090907
  16. },
  17. {
  18. "dim1": "dim1_val3",
  19. "count": 70,
  20. "some_metrics": 871,
  21. "average": 12.442857142857143
  22. },
  23. {
  24. "dim1": "dim1_val4",
  25. "count": 62,
  26. "some_metrics": 815,
  27. "average": 13.14516129032258
  28. },
  29. {
  30. "dim1": "dim1_val5",
  31. "count": 60,
  32. "some_metrics": 2787,
  33. "average": 46.45
  34. }
  35. ]
  36. }
  37. ]

Behavior on multi-value dimensions

topN queries can group on multi-value dimensions. When grouping on a multi-value dimension, all values from matching rows will be used to generate one group per value. It’s possible for a query to return more groups than there are rows. For example, a topN on the dimension tags with filter "t1" AND "t3" would match only row1, and generate a result with three groups: t1, t2, and t3. If you only need to include values that match your filter, you can use a filtered dimensionSpec. This can also improve performance.

See Multi-value dimensions for more details.

Aliasing

The current TopN algorithm is an approximate algorithm. The top 1000 local results from each segment are returned for merging to determine the global topN. As such, the topN algorithm is approximate in both rank and results. Approximate results ONLY APPLY WHEN THERE ARE MORE THAN 1000 DIM VALUES. A topN over a dimension with fewer than 1000 unique dimension values can be considered accurate in rank and accurate in aggregates.

The threshold can be modified from its default 1000 via the server parameter druid.query.topN.minTopNThreshold, which needs a restart of the servers to take effect, or via minTopNThreshold in the query context, which takes effect per query.

If you are wanting the top 100 of a high cardinality, uniformly distributed dimension ordered by some low-cardinality, uniformly distributed dimension, you are potentially going to get aggregates back that are missing data.

To put it another way, the best use cases for topN are when you can have confidence that the overall results are uniformly in the top. For example, if a particular site ID is in the top 10 for some metric for every hour of every day, then it will probably be accurate in the topN over multiple days. But if a site is barely in the top 1000 for any given hour, but over the whole query granularity is in the top 500 (example: a site which gets highly uniform traffic co-mingling in the dataset with sites with highly periodic data), then a top500 query may not have that particular site at the exact rank, and may not be accurate for that particular site’s aggregates.

Before continuing in this section, please consider if you really need exact results. Getting exact results is a very resource intensive process. For the vast majority of “useful” data results, an approximate topN algorithm supplies plenty of accuracy.

Users wishing to get an exact rank and exact aggregates topN over a dimension with greater than 1000 unique values should issue a groupBy query and sort the results themselves. This is very computationally expensive for high-cardinality dimensions.

Users who can tolerate approximate rank topN over a dimension with greater than 1000 unique values, but require exact aggregates can issue two queries. One to get the approximate topN dimension values, and another topN with dimension selection filters which only use the topN results of the first.

Example First query

  1. {
  2. "aggregations": [
  3. {
  4. "fieldName": "L_QUANTITY_longSum",
  5. "name": "L_QUANTITY_",
  6. "type": "longSum"
  7. }
  8. ],
  9. "dataSource": "tpch_year",
  10. "dimension":"l_orderkey",
  11. "granularity": "all",
  12. "intervals": [
  13. "1900-01-09T00:00:00.000Z/2992-01-10T00:00:00.000Z"
  14. ],
  15. "metric": "L_QUANTITY_",
  16. "queryType": "topN",
  17. "threshold": 2
  18. }

Example second query

  1. {
  2. "aggregations": [
  3. {
  4. "fieldName": "L_TAX_doubleSum",
  5. "name": "L_TAX_",
  6. "type": "doubleSum"
  7. },
  8. {
  9. "fieldName": "L_DISCOUNT_doubleSum",
  10. "name": "L_DISCOUNT_",
  11. "type": "doubleSum"
  12. },
  13. {
  14. "fieldName": "L_EXTENDEDPRICE_doubleSum",
  15. "name": "L_EXTENDEDPRICE_",
  16. "type": "doubleSum"
  17. },
  18. {
  19. "fieldName": "L_QUANTITY_longSum",
  20. "name": "L_QUANTITY_",
  21. "type": "longSum"
  22. },
  23. {
  24. "name": "count",
  25. "type": "count"
  26. }
  27. ],
  28. "dataSource": "tpch_year",
  29. "dimension":"l_orderkey",
  30. "filter": {
  31. "fields": [
  32. {
  33. "dimension": "l_orderkey",
  34. "type": "selector",
  35. "value": "103136"
  36. },
  37. {
  38. "dimension": "l_orderkey",
  39. "type": "selector",
  40. "value": "1648672"
  41. }
  42. ],
  43. "type": "or"
  44. },
  45. "granularity": "all",
  46. "intervals": [
  47. "1900-01-09T00:00:00.000Z/2992-01-10T00:00:00.000Z"
  48. ],
  49. "metric": "L_QUANTITY_",
  50. "queryType": "topN",
  51. "threshold": 2
  52. }