# How to write a efficient ES query for the below SQL query?

**URL:** https://discuss.elastic.co/t/how-to-write-a-efficient-es-query-for-the-below-sql-query/215599
**Category:** Elasticsearch
**Created:** [January 19, 2020, 5:53am UTC](https://discuss.elastic.co/t/how-to-write-a-efficient-es-query-for-the-below-sql-query/215599 "2020-01-19T05:53:45Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![DeeeFOX](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/deeefox/32/49566_2.png) [@DeeeFOX](https://discuss.elastic.co/u/DeeeFOX)
#### Post date: [January 19, 2020, 5:53am UTC](https://discuss.elastic.co/t/how-to-write-a-efficient-es-query-for-the-below-sql-query/215599/1 "2020-01-19T05:53:46Z")

</div>

```
select tag1, tag2, sum(sum_val)
    select tag-uid, sum(value) as sum_val
    from data_table
    group by tag-uid
join tag_table using(tag-uid)
group by tag1, tag2
```

---

<div class="post-metadata">

### Author: ![aravindputrevu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aravindputrevu/32/24816_2.png) [@aravindputrevu](https://discuss.elastic.co/u/aravindputrevu)
#### Post date: [January 19, 2020, 6:47am UTC](https://discuss.elastic.co/t/how-to-write-a-efficient-es-query-for-the-below-sql-query/215599/2 "2020-01-19T06:47:05Z")

</div>

If you have your documents ingested appropriately like mentioned [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/_mapping_concepts_across_sql_and_elasticsearch.html), you could use the [translate](https://www.elastic.co/guide/en/elasticsearch/reference/current/sql-translate.html) endpoint of Elasticsearch SQL and convert the SQL query to equivalent Query DSL.

--  
Aravind

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [January 19, 2020, 6:50am UTC](https://discuss.elastic.co/t/how-to-write-a-efficient-es-query-for-the-below-sql-query/215599/3 "2020-01-19T06:50:08Z")

</div>

That seems to be a join which Elasticsearch does not support. You are therefore likely to restructure your data when indexing. If you describe the use case and what you are trying to achieve at a higher level someone may be able to help you.

---

<div class="post-metadata">

### Author: ![DeeeFOX](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/deeefox/32/49566_2.png) [@DeeeFOX](https://discuss.elastic.co/u/DeeeFOX)
#### Post date: [January 19, 2020, 1:54pm UTC](https://discuss.elastic.co/t/how-to-write-a-efficient-es-query-for-the-below-sql-query/215599/4 "2020-01-19T13:54:50Z")

</div>

we use es as tsdb, and data we store is look like:

```
{
	"metric": "metric_name",
	"@timestamp": "2020-01-01T01:01:01",
	"t": {
		"key1": "val1",
		"key2": "val2"
	},
	"f": {
		"name1": 1.2,
		"name2": 2.3
	}
}

```

simple aggregation is ok (but some aggs are still very slow) like:

> select t.key2, sum(f.name1) from tsdata where t.key1='val1' group by t.key2

but i have no idea that if we can handle a more complex aggregation like "join" does in mysql

---

<div class="post-metadata">

### Author: ![DeeeFOX](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/deeefox/32/49566_2.png) [@DeeeFOX](https://discuss.elastic.co/u/DeeeFOX)
#### Post date: [January 19, 2020, 2:08pm UTC](https://discuss.elastic.co/t/how-to-write-a-efficient-es-query-for-the-below-sql-query/215599/5 "2020-01-19T14:08:48Z")

</div>

oh, thanks for your replying!!

we use es as tsdb described in:

[my other answer](https://discuss.elastic.co/t/how-to-write-a-efficient-es-query-for-the-below-sql-query/215599/4)

data look like:

```
{
	"metric": "metric_name",
	"@timestamp": "2020-01-01T01:01:01",
	"t": {
		"key1": "val1",
		"key2": "val2"
	},
	"f": {
		"name1": 1.2,
		"name2": 2.3
	}
}

```

so, if we want to have more tsdb-use funcs like:

> 1、"topk( metric\_name.f.name1 ) by t.key1 in\_every '10m'"  
> 2、"inc-rate( metric\_name.f.name1 ) in\_every '5m'"

that es aggs can not directly support

my question is:

can i enrich the function of es like udf do in hive ?  
or, it's to say that how to create a user defined function in es?

---

<div class="post-metadata">

### Author: ![DeeeFOX](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/deeefox/32/49566_2.png) [@DeeeFOX](https://discuss.elastic.co/u/DeeeFOX)
#### Post date: [January 20, 2020, 4:53am UTC](https://discuss.elastic.co/t/how-to-write-a-efficient-es-query-for-the-below-sql-query/215599/6 "2020-01-20T04:53:09Z")

</div>

Any ideas ?

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [February 17, 2020, 5:04am UTC](https://discuss.elastic.co/t/how-to-write-a-efficient-es-query-for-the-below-sql-query/215599/7 "2020-02-17T05:04:28Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
