基于securityId生成价格区间的kdb测试表:优化方案问询
优化KDB测试表中marketPrice列的生成方案
需求说明
创建KDB测试表时,需根据securityId列的值生成对应区间的marketPrice列,规则如下:
- 当
securityId=a时,marketPrice`取值范围为0-100 - 当
securityId=b时,marketPrice`取值范围为100-200 - 当
securityId=c时,marketPrice`取值范围为1000-1100
当前实现使用了多次update语句调整marketPrice,希望优化为**无需使用update(直接在表创建阶段生成)或仅使用一次update**的方案。
当前实现代码
n:100; tradeDate: asc n?2025.04.01 + til 10; securityId: `g#n?`a`b`c; quantityAvailable: n?1000; marketPrice: n?100.; data:([] tradeDate: tradeDate; securityId: securityId; quantityAvailable: quantityAvailable; marketPrice:marketPrice ); update marketPrice: 100+marketPrice from `data where securityId=`b; update marketPrice: 1000+marketPrice from `data where securityId=`c;
优化方案
方案1:表创建阶段直接生成(无update语句)
利用字典映射不同securityId对应的基准值,结合随机数直接生成符合区间要求的marketPrice,无需后续调整:
n:100; tradeDate: asc n?2025.04.01 + til 10; securityId: `g#n?`a`b`c; quantityAvailable: n?1000; // 定义各securityId对应的基准价格 priceBase: `a`b`c!0 100 1000; // 生成对应区间的marketPrice:基准值 + 0-100的随机小数 marketPrice: priceBase[securityId] + n?100.; data:([] tradeDate: tradeDate; securityId: securityId; quantityAvailable: quantityAvailable; marketPrice:marketPrice );
说明:通过字典priceBase快速匹配每个securityId的基准值,再加上0-100的随机数,直接生成符合区间要求的价格,避免了多次表更新操作,效率更高。
方案2:仅使用一次update语句调整
如果已经存在基础表,可借助字典映射实现单次update完成价格调整:
n:100; tradeDate: asc n?2025.04.01 + til 10; securityId: `g#n?`a`b`c; quantityAvailable: n?1000; marketPrice: n?100.; data:([] tradeDate: tradeDate; securityId: securityId; quantityAvailable: quantityAvailable; marketPrice:marketPrice ); // 单次update完成所有调整 update marketPrice: marketPrice + (`a`b`c!0 100 1000)[securityId] from `data;
说明:通过字典映射每个securityId需要增加的价格偏移量,一次性对全表marketPrice进行调整,替代多次分条件的update操作。
内容的提问来源于stack exchange,提问作者Utsav
相关产品推荐
相关产品推荐

