Neo4j的性能优化实践说明

huangapple go评论56阅读模式
英文:

clarification on Neo4j's performance good practice

问题

以下是翻译好的部分:

文档中提到:
> 在所有查询中都要指定目标数据库,可以使用Driver.execute_query()中的database_参数,也可以在创建新会话时使用database参数。如果没有提供数据库,驱动程序必须向服务器发送额外的请求以确定默认数据库是什么。对于单个查询,开销很小,但在数百个查询中会变得显著。

并提供了2个示例:

# 好的做法
driver.execute_query("<QUERY>", database_="<DB NAME>")
driver.session(database="<DB NAME>")

# 不好的做法
driver.execute_query("<QUERY>")
driver.session()

这是否意味着以下做法:

# 好的做法
driver.execute_query("<QUERY>", database_="<DB NAME>")
driver.session()

和这个

# 好的做法
driver.execute_query("<QUERY>")
driver.session(database="<DB NAME>")

是好的做法,还是我应该在execute_query() **和 ** session()中都指定数据库名称?

英文:

The docs state:
> Specify the target database on all queries, either with the database_ parameter in Driver.execute_query() or with the database parameter when creating new sessions. If no database is provided, the driver has to send an extra request to the server to figure out what the default database is. The overhead is minimal for a single query, but becomes significant over hundreds of queries.

and gives 2 examples:

# Good practice
driver.execute_query("<QUERY>", database_="<DB NAME>")
driver.session(database="<DB NAME>")

# Bad practice
driver.execute_query("<QUERY>")
driver.session()

Does that mean that

# Good practice
driver.execute_query("<QUERY>", database_="<DB NAME>")
driver.session()

and this

# Good practice
driver.execute_query("<QUERY>")
driver.session(database="<DB NAME>")

are good practices or should I specify the database name in execute_query() and session()?

答案1

得分: 2

The documentation could be a bit clearer.

driver.execute_querydriver.session 实际上代表了两种不同的方式来执行查询。您应该使用其中一个调用,而不是同时使用它们。

文档中更清晰的做法可能是这样说:

# 良好实践
driver.execute_query("<查询>", database_="<数据库名称>")
  或
driver.session(database="<数据库名称>")

# 不良实践
driver.execute_query("<查询>")
  或
driver.session()
英文:

The documentation could be a bit clearer.

driver.execute_query and driver.session actually represent 2 different ways to execute queries. You would use one call or the other, but not together.

It would have been clearer for the documentation to say something like:

# Good practice
driver.execute_query("<QUERY>", database_="<DB NAME>")
  OR
driver.session(database="<DB NAME>")

# Bad practice
driver.execute_query("<QUERY>")
  OR
driver.session()

答案2

得分: 1

每当执行查询(使用 driver.execute_query 或任何会话/事务API),如果您事先知道目标数据库,建议指定它是个好主意。

为什么呢?因为Neo4j引入了“主数据库”概念,允许不同用户拥有默认数据库。

如果您不指定要目标的数据库,驱动程序首先必须与服务器通信,以确定给定用户的主数据库是什么。

由于主数据库可以随时更改,驱动程序每个会话只缓存该信息一次。

因此,每次使用会话API时,您应该在会话配置级别指定目标数据库,以避免触发解析过程。

由于 driver.execute_query 是对会话的可重试事务API的一个门面(在这种情况下,驱动程序为您管理会话),您还需要在那里指定数据库,如果您希望受管理的会话正确配置。

注意:有正在进行的内部讨论,以改进驱动程序处理“主数据库解析”问题的方式,因此请关注可能在下一个版本中的改进!

英文:

Whenever you execute a query (with driver.execute_query or with any session/transaction API), specifying the target database if you know it in advance, is a good idea.

Why? Because Neo4j introduced the "home database" concept, which allows different users to have default databases.

If you do not not specify what database you target, the driver will first have to communicate with the server to figure out what the home database is for the given user.

Since the home database can be changed at any time, the driver only caches that information once per session.

Therefore, you should specify the target database at the session configuration level every time you use a session API, to avoid that resolution process to kick in.

Since driver.execute_query is a facade on session's retryable transaction APIs (the driver manages the session for you in this case), you also need to specify the database there, if you want the managed session to be properly configured.

Note: there are ongoing internal discussions to improve the way drivers handle the "home database resolution" issue, so stay tuned for potential improvements in the next releases!

huangapple
  • 本文由 发表于 2023年6月15日 16:55:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76480799.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定