upsert to dictionary of tables?

124 views
Skip to first unread message

Angela

unread,
Jun 1, 2021, 1:49:47 AM6/1/21
to Kdb+ Personal Developers
Hi,

So it's better/faster to do upsert with the symbol "`t upsert ..." than upsert and assign "t:t upsert ...", as show here, right?

q)t0:t1:([]sym:`a`b;data:1 2)
q)\t do[50000;`t1 upsert ([]sym:`a`b;data:1 2)]
27
q)t1:t0
q)\t do[50000;t1:t1 upsert ([]sym:`a`b;data:1 2)]
1396

Now if I have a dictionary of tables, I can upsert using the "symbol" way like this:
q)t:`1`2`3!3#enlist flip `sym`data!(`$();`int$())
q)t
1| +`sym`data!(`symbol$();`int$())
2| +`sym`data!(`symbol$();`int$())
3| +`sym`data!(`symbol$();`int$())
q)`t.1 upsert ([]sym:`a`b;data:1 2)
`t.1

But if the table dictionary keys like `1 is in a function local variable (ie. x), is there a way to do upsert like with "`t.1"?

thanks

Matthew Clark

unread,
Jun 1, 2021, 3:43:11 AM6/1/21
to Kdb+ Personal Developers
Hi Angela,

It seems like you were very close to having something working there. My only suggestion surrounds using the sv keyword in order to generate the symbol name for upsert in place. I've timed this and it doesn't seem to be too impactful on the time taken.

q)\t:50000 sv[`;`t,x] upsert ([]sym:`a`b;data:1 2)
76

Here x is just equal to the symbol 1. And instead of a do loop here I am using the notation \t:X which just performs the operation X times while timing it. 

Matthew Clark

unread,
Jun 1, 2021, 7:16:33 AM6/1/21
to Kdb+ Personal Developers
A suggestion made to me can improve on this further. Instead of using the sv keyword, which takes up more time you can use the @ operator with a symbol input to perform the upsert in place. Seems to save a substantial amount of time (almost half):

q)\t:50000 sv[`;`t,x] upsert ([]sym:`a`b;data:1 2)
76
q)\t:50000 @[`t;x;,;([]sym:`a`b;data:1 2)
42

Hope this helps!



Angela

unread,
Jun 1, 2021, 8:24:33 AM6/1/21
to Kdb+ Personal Developers
Thank you! I was trying with the @ operator but wasn't quite there, and didn't know it's even possible with the sv function!

thanks

Alvi Kabir

unread,
Jun 4, 2021, 4:02:06 PM6/4/21
to Kdb+ Personal Developers
I suggest using the variadic apply '.' as it supports all depths:

t:`1`2`3!3#enlist flip `sym`data!(`$();`int$())


.[`t;enlist `1;upsert;([]sym:`a`b;data:1 2)]

`t


t

1| +`sym`data!(`a`b;1 2i)

2| +`sym`data!(`symbol$();`int$())

3| +`sym`data!(`symbol$();`int$())


t[`4]: enlist[`inner]!enlist flip `sym`data!(`$();`int$())


t

1| +`sym`data!(`a`b;1 2i)

2| +`sym`data!(`symbol$();`int$())

3| +`sym`data!(`symbol$();`int$())

4| (,`inner)!,+`sym`data!(`symbol$();`int$())


.[`t;(`4;`inner);upsert;([]sym:`a`b;data:1 2)]

`t


t

1| +`sym`data!(`a`b;1 2i)

2| +`sym`data!(`symbol$();`int$())

3| +`sym`data!(`symbol$();`int$())

4| (,`inner)!,+`sym`data!(`a`b;1 2i)

Angela Sum

unread,
Jun 5, 2021, 7:04:41 AM6/5/21
to personal...@googlegroups.com
Thanks!

--
You received this message because you are subscribed to a topic in the Google Groups "Kdb+ Personal Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/personal-kdbplus/sn3XgfMMIqI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to personal-kdbpl...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/personal-kdbplus/8d11c550-c1c1-4770-9a7c-a91828af6a5dn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages