KX Community

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

Home Forums kdb+ What is the role played by key columns in a keyed table [ query join/performance ] ? Re: What is the role played by key columns in a keyed table [ query join/performance ] ?

  • pcarroll

    Member
    May 24, 2022 at 12:00 am

    Just wanted to codify some of the above:

    q)tab:([]sym:-50000?`6;px:50000?10) 
    q)ktab:`sym xkey tab // wanted to show the last record to convince that if kdb // has to search through entire list, no speed gain is evident 
    q)-1#tab 
    sym px 
    --------- 
    obafmn 6 
    
    q)-1#ktab 
    sym   | px 
    ------| -- 
    obafmn| 6 
    
    q)\ts do[100000;select from tab where sym=`obafmn] 
    2034 66240 
    
    q)\ts do[100000;select from ktab where sym=`obafmn] 
    2051 66240 // below no speed gain, but memory usage is reduced 
    
    q)\ts do[100000;ktab`obafmn] 
    2079 960 // however this does mean any other *valid* request would be faster 
    
    q)rand key ktab 
    sym| kfpfmd 
    
    q)\ts do[100000;ktab`kfpfmd] 
    1169 960 // showing point above with grouped attribute applied to keyed column 
    q)gktab:`sym xkey update `g#sym from tab 
    q)\ts do[100000;select from gktab where sym=`obafmn] 
    89 1808 // final point I wanted to make showing duplicate keys 
    q)`a`a!(1;1) 
    a| 1 
    a| 1

    Thanks  and  for contributing here.