# Optimize Kdb group queries ('by' clause) for a speedy execution

**URL:** <https://forum.kx.com/t/optimize-kdb-group-queries-by-clause-for-a-speedy-execution/11901>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [April 27, 2019, 7:34am UTC](https://forum.kx.com/t/optimize-kdb-group-queries-by-clause-for-a-speedy-execution/11901 "2019-04-27T07:34:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vivek\_shende](https://avatars.discourse-cdn.com/v4/letter/v/5fc32e/32.png) [@vivek\_shende](https://forum.kx.com/u/vivek_shende)\
**Post date:** [April 27, 2019, 7:34am UTC](https://forum.kx.com/t/optimize-kdb-group-queries-by-clause-for-a-speedy-execution/11901/1 "2019-04-27T07:34:00Z")

</div>

Hello,

I need to optimize Kdb group queries to reduce their total execution time. I am executing a group query using ‘by’ clause over 4 columns. The group query itself runs over a subquery.&nbsp;

The subquery returns around 6 million records and it runs in approximately 700 ms. The group query that runs on top of it reduces these records to around 3-4k, but it takes almost 4000 ms (4 seconds) to run which is beyond the acceptable limit of the application. I need to optimize it so that the total query execution time is less than 1.5 seconds. Is there any performant alternative to ‘by’ clause? Is there something I can do with the grouped columns that will improve the execution time?

---

<div class="post-metadata">

**Author:** ![jwbuitenhuis](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@jwbuitenhuis](https://forum.kx.com/u/jwbuitenhuis)\
**Post date:** [April 27, 2019, 11:03am UTC](https://forum.kx.com/t/optimize-kdb-group-queries-by-clause-for-a-speedy-execution/11901/2 "2019-04-27T11:03:00Z")

</div>

Have a reproducible example? It’s probably related to the types you’re grouping on. The below example reduces to 12K rows but performs faster:

q)n:6000000;t:(sym:n?`1;date:.z.d+n#1 2 3;mkt:n?`1;typ:n?`1;price:n?2.0)

q)count select by date,sym,mkt,typ from t

12288

q)\t select by date,sym,mkt,typ from t

261
