Search code examples
handlekdb+q-lang

kdb/q: Query multiple handles with hopen


I would like to be able to query several handles at once, where the tables have the same format like:

handles: 8000,8001,8003 tables: foo

Want to do something like:

x:hopen `8000`8001`8003
x select from foo col1,col2

So i get rows from each foo table on each handle.

Is there a way to achieve this?

Thank you


Solution

  • Use 'each' to hopen each handle

      q)h:hopen each 8000 8001 8002
      q)h
      476 480 484i
    

    Use apply each-left to send the same query to each server

      q)r:h@\:"select col1,col2 from foo"
      q)r
      +`col1`col2!(1 2;2 3)
      +`col1`col2!(1 2;2 3)
      +`col1`col2!(1 2;2 3)
    

    Then you'll have to raze the result:

     q)show res:raze r
    col1 col2
    --------- 
     1    2
     2    3
     1    2
     2    3
     1    2
     2    3