KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ help understanding a function

  • help understanding a function

    Posted by fav_pa on May 29, 2024 at 9:07 pm

    hello everyone!
    i am not sure to really understand the operations order in this function .

    // tabPath — path where to store the table

    // pvar — variable to sort and index on for fast querying

    // table — name of the table to save

    :@[;pvar;`p#] pvar xasc (` sv (tabPath;`;table;`)) set .Q.en[tabPath] get table;



    i am under the impression that because of left to right evaluation, it is saved before it is ordered. can someone explain ?
    thank you!!


    unterrainer_ale replied 1 month, 2 weeks ago 3 Members · 4 Replies
  • 4 Replies
  • fav_pa

    May 29, 2024 at 9:27 pm

    and also would this not make more sense: (` sv (tabPath;`;table;`)) set @[; pvar;`p#] pvar xasc .Q.en[tabPath] get table

  • unterrainer_ale

    May 30, 2024 at 8:09 am

    Hey fav_pa,

    I believe there are a few more mistakes in the code you posted, can you share the source where you have it from?

    First, the part to create the path to save down is missing some code

    q) ` sv (`$":path/toTable";`tableName;`)

    Then you also miss the `p in


    Other than that, the second code you posted makes sense

    `(` sv (tabPath;; table; )) set @[; pvar;`p#] pvar xasc .Q.en[tabPath] get table


  • rocuinneagain

    May 30, 2024 at 9:12 am

    Yes with right to left evaluation it is being saved then sorted.

    The reason this may have been done is that sorting in memory will temporarily use extra memory to perform the operation.


    Sorting on disk is very memory efficient as it does not bring the full table in to memory.

    The trade off is it is slower as each column gets written twice.

    q)tab:flip (`$/:.Q.an)!{1000000?10000.0}each .Q.an
    q)\ts `a xasc `:tab/ set tab
    1824 25168800
    q)\ts `:tab/ set `a xasc tab
    1165 536874192
    21.33094 //Sorting in memory uses 21x memory than sorting on disk
    • fav_pa

      May 30, 2024 at 9:29 am

      Extremely interesting, thank you very much for your answer!

Log in to reply.