# Sorting on disk

**URL:** <https://forum.kx.com/t/sorting-on-disk/8493>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [November 17, 2013, 4:51pm UTC](https://forum.kx.com/t/sorting-on-disk/8493 "2013-11-17T16:51:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![leonbaum2](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@leonbaum2](https://forum.kx.com/u/leonbaum2)\
**Post date:** [November 17, 2013, 4:51pm UTC](https://forum.kx.com/t/sorting-on-disk/8493/1 "2013-11-17T16:51:00Z")

</div>

Hi,Say I have a large single file or splayed table (not partitioned) thatwill not fit in memory. Is there any way to update the table to besorted by a given column without needing to have the entire table inmemory?Thanks,Leon

---

<div class="post-metadata">

**Author:** ![wp1](https://avatars.discourse-cdn.com/v4/letter/w/db5fbb/32.png) [@wp1](https://forum.kx.com/u/wp1)\
**Post date:** [November 17, 2013, 4:57pm UTC](https://forum.kx.com/t/sorting-on-disk/8493/2 "2013-11-17T16:57:00Z")

</div>

yes:// flat tableq)t:(a:2 1 2;b:2 2 3)q)save`t`:tq)@[`:.;`t;{`a xasc x}]`:.q)get `:ta b—1 22 22 3q)// splayedq)t:(a:2 1 2;b:2 2 3)q)`a xasc `:t/`:t/q)get`:t/a b—1 22 3q)t:(a:2 1 2;b:2 2 3)q)`:t/ set t`:t/q)`a xasc `:t/`:t/q)get`:t/a b—1 22 22 3q)On Sun, Nov 17, 2013 at 4:51 PM, Leon Baum wrote:\>\> Hi,\>\> Say I have a large single file or splayed table (not partitioned) that\> will not fit in memory. Is there any way to update the table to be\> sorted by a given column without needing to have the entire table in\> memory?\>\>\> Thanks,\> Leon\>\> –\>

Submitted via Google Groups

---

<div class="post-metadata">

**Author:** ![leonbaum2](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@leonbaum2](https://forum.kx.com/u/leonbaum2)\
**Post date:** [November 17, 2013, 6:33pm UTC](https://forum.kx.com/t/sorting-on-disk/8493/3 "2013-11-17T18:33:00Z")

</div>

Thanks, those examples do result in the tables being updated on disk,but the sorting still appears to use as much memory as loading theentire table. I suppose that’s the only efficient way to do it.Leonwp writes:\> yes:\>\> // flat table\> q)t:(a:2 1 2;b:2 2 3)\> q)save`t\> `:t\> q)@[`:.;`t;{`a xasc x}]\> `:.\> q)get `:t\> a b\> —\> 1 2\> 2 2\> 2 3\> q)\>\> // splayed\> q)t:(a:2 1 2;b:2 2 3)\> q)`a xasc `:t/\> `:t/\> q)get`:t/\> a b\> —\> 1 2\> 2 3\> q)t:(a:2 1 2;b:2 2 3)\> q)`:t/ set t\> `:t/\> q)`a xasc `:t/\> `:t/\> q)get`:t/\> a b\> —\> 1 2\> 2 2\> 2 3\> q)\>\>\> On Sun, Nov 17, 2013 at 4:51 PM, Leon Baum wrote:\>\>\>\> Hi,\>\>\>\> Say I have a large single file or splayed table (not partitioned) that\>\> will not fit in memory. Is there any way to update the table to be\>\> sorted by a given column without needing to have the entire table in\>\> memory?\>\>\>\>\>\> Thanks,\>\> Leon\>\>\>\> –\>\>

Submitted via Google Groups

---

<div class="post-metadata">

**Author:** ![charlie1](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@charlie1](https://forum.kx.com/u/charlie1)\
**Post date:** [November 17, 2013, 6:46pm UTC](https://forum.kx.com/t/sorting-on-disk/8493/4 "2013-11-17T18:46:00Z")

</div>

`a xasc `:t/

calculates the index permuation, and then applies it column by column.

You may not notice a difference in mem usage unless you have \>2 columns.
