kdb - 使用 maps 和 lj 简化查询

kdb - Simplifying a query with maps and lj

我已经将几个表加载到内存中

//the real table is huge
st:([] s:`a`a`a`b`b`b;n:3 5 7 3 5 7; v:`U20`U30`U50`U22`U33`U44)

//step function
st1:`s#select first v by s,n from st

//mapping function 
f:{  (st1([] s:(),x 0;n:(),x 1))`v}

/Another table
t:([] s:`a`b`b;v:4 6 8)

//user input
MAP:([ KEY:`U20`U33`M40 ] VAL:200 330 440 )

有没有办法简化下面的?在这里,我为 lj 创建一个临时列 KEY,然后将其删除

delete KEY from (update KEY:first each f each (s,'v) from t) lj MAP

如果您可以将用户输入(目前是键控 table)转换为字典

MAP2:`U20`U33`M40!200 330 440 

MAP2 的新查询:

update VAL:MAP2@f each (s,'v) from t

其实下面的更简单也更快:

update VAL:MAP2@f ( s;v) from t

可以一行一行的做,通过建表索引其他表来避免f。这应该通过避免在最后一行

中的 each both join 和 each(s) 来对其进行矢量化并使其更快
q)update VAL:MAP[([]KEY:st1[([]s;n:v);`v]);`VAL] from t
s v VAL
-------
a 4 200
b 6 330
b 8
q)

为什么不同步您的列名以便您可以进行直接键-table 查找?

q)st:([] s:`a`a`a`b`b`b;v:3 5 7 3 5 7; KEY:`U20`U30`U50`U22`U33`U44)
q)st1:`s#select first KEY by s,v from st

q)t,'MAP st1 t
s v VAL
-------
a 4 200
b 6 330
b 8