Gremlin 参数化查询

先给出用Python 请求支持Gremlin查询的图数据库时使用参数化查询和不使用参数化查询的实现方式。

  • 参数化查询

start_time = time.time()
request_body = {
    "Gremlin": '''g.V().has('Category','code', category)
                ''',
    "bindings": {"category":"aircondition"},
    "language": "Gremlin-groovy",
    "aliases": {
        "graph": 'home',
        "g": "__g_{0}".format('home')
        }
    }
r = requests.post('http://127.0.0.1:8183/Gremlin', data=json.dumps(request_body),
                   headers={"Content-Type": "application/json"})
print(f"time cost is {time.time()-start_time}")
print(r.json())
  • 非参数化查询

start_time = time.time()
request_body = {
    "Gremlin": '''g.V().has('Category','code', 'aircondition')
                ''',
    "bindings": {},
    "language": "Gremlin-groovy",
    "aliases": {
        "graph": 'home',
        "g": "__g_{0}".format('home')
        }
    }
r = requests.post('http://127.0.0.1:8183/Gremlin', data=json.dumps(request_body),
                   headers={"Content-Type": "application/json"})
print(f"time cost is {time.time()-start_time}")
print(r.json())

对比两种请求方式,参数化请求与不使用参数化请求的写法相比,需在Gremlin语句中定义变量, 每次请求时,将语句中的变量赋值放在了bindings中,binding是一个变量字典。

Gremlin server文档中强调了Parameterized request(参数化请求) 对于提高查询性能很关键,因为避免了重复的Gremlin脚本编译,可以加快查询时间。考虑到Gremlin server会缓存其收到的所有请求,因此参数化请求也可以减少Gremlin server的资源占用。所以对于一个线上服务,需要尽可能的将所有的Gremlin查询都使用参数化请求方式来实现,不然每次查询请求都会进行脚本编译和缓存,这样就会面临Gremlin server内存占用越来越多的风险。

Parameterized request are considered the most efficient way to send Gremlin to the server as they can be cached, which will boost performance and reduce resources required on the server.
The bindings argument is a Map of variables where the keys become available as variables in the Gremlin script. Note that parameterization of requests is critical to performance, as repeated script compilation can be avoided on each request.
Use script parameterization. Period. Gremlin Server caches all scripts that are passed to it. The cache is keyed based on the a hash of the script. Therefore g.V(1) and g.V(2) will be recognized as two separate scripts in the cache. If that script is parameterized to g.V(x) where x is passed as a parameter from the client, there will be no additional compilation cost for future requests on that script. Compilation of a script should be considered "expensive" and avoided when possible.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值