# In Elasticsearch SQL using group by year(date-field) is slow, should I create a standalone year-of-date field when importing?

**URL:** https://discuss.elastic.co/t/in-elasticsearch-sql-using-group-by-year-date-field-is-slow-should-i-create-a-standalone-year-of-date-field-when-importing/196480
**Category:** Elasticsearch
**Created:** [August 23, 2019, 9:24am UTC](https://discuss.elastic.co/t/in-elasticsearch-sql-using-group-by-year-date-field-is-slow-should-i-create-a-standalone-year-of-date-field-when-importing/196480 "2019-08-23T09:24:55Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Jack\_Wu](https://avatars.discourse-cdn.com/v4/letter/j/b3f665/32.png) [@Jack\_Wu](https://discuss.elastic.co/u/Jack_Wu)
#### Post date: [August 23, 2019, 9:24am UTC](https://discuss.elastic.co/t/in-elasticsearch-sql-using-group-by-year-date-field-is-slow-should-i-create-a-standalone-year-of-date-field-when-importing/196480/1 "2019-08-23T09:24:55Z")

</div>

Hi all, the "year" function in group by is slow.  
Here is the example:

"select year(docdate), sum(docamt) FROM indexA GROUP BY year(docdate)"  
is much slower than  
"select docdate\_year, sum(docamt) FROM indexA GROUP BY docdate\_year"

Should I create another "year field", "month field", "day field" for speed?  
Are there any other approach to speed the date-function in the group by clause?

---

<div class="post-metadata">

### Author: ![Andrei\_Stefan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrei_stefan/32/47533_2.png) [@Andrei\_Stefan](https://discuss.elastic.co/u/Andrei_Stefan)
#### Post date: [August 26, 2019, 10:07am UTC](https://discuss.elastic.co/t/in-elasticsearch-sql-using-group-by-year-date-field-is-slow-should-i-create-a-standalone-year-of-date-field-when-importing/196480/2 "2019-08-26T10:07:52Z")

</div>

Creating separate fields for year, month, day will definitely be faster than the `YEAR` SQL function.

---

<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: [September 23, 2019, 10:21am UTC](https://discuss.elastic.co/t/in-elasticsearch-sql-using-group-by-year-date-field-is-slow-should-i-create-a-standalone-year-of-date-field-when-importing/196480/3 "2019-09-23T10:21:33Z")

</div>

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