简体   繁体   English

在首次更新时优化GraphDiff性能(后续更新很快)

[英]Optimize GraphDiff performance on first update (subsequent updates are quick)

I am working on a project with a very complex entity. 我正在与一个非常复杂的实体进行项目。 The object graph contains approximately 30 objects in a tree with two levels (there are children of children). 对象图在树中包含大约30个对象,分为两个级别(有孩子的孩子)。

The entity is managed in a browser, and sent as Json to a Web API endpoint. 该实体在浏览器中进行管理,并作为Json发送到Web API端点。 The endpoint serializes the JSON into the complex entity, then the repository uses GraphDiff to save the entity. 端点将JSON序列化为复杂实体,然后存储库使用GraphDiff保存该实体。

On first save, the UpdateGraph call takes around 12 seconds, on subsequent calls on the same entity, takes milliseconds. 首次保存时,UpdateGraph调用大约需要12秒,而在同一实体上的后续调用则需要毫秒。

I assume that the graph is cached per entity i the EF DbContext. 我假设该图是在EF DbContext中按实体缓存的。

Is there any way to optimize the first call? 有什么方法可以优化首次通话? We have only a few scenarios when we are using GraphDiff, maybe is there a way to prepare the graphs used in teh applicaton. 当我们使用GraphDiff时,只有几种情况,也许有一种方法可以准备在应用程序中使用的图形。

Thank you for your help. 谢谢您的帮助。

I solved it by cloning the GraphDiff source code from GitHub and modifying it. 我通过从GitHub克隆GraphDiff源代码并对其进行修改来解决它。

GraphDiff detects dynamically the keys of the entities, and because the way how the dynamic query expressions are generated to retrieve the already persisted objects, the expression are recompiled by EF every time when a new entity (another primary key) is used for the query. GraphDiff动态地检测实体的键,并且由于生成动态查询表达式以检索已经存在的对象的方式,因此每次将新实体(另一个主键)用于查询时,EF都会重新编译该表达式。 So the compiled queries are not cached only if the entity keys were used before. 因此,仅当以前使用实体键时,才对缓存的查询进行缓存。 These query compilations took a lot of time in my case (10s). 在我的案例中,这些查询编译花费了很多时间(10秒钟)。

The source code was modified to allow the injection of the already persisted entity. 修改了源代码,以允许注入已经持久的实体。 This means that instead of letting GrahpDiff to retrieve the entity from the DB, it was retrieved in an optimal way and was injected for merging into GraphDiff. 这意味着,不是让GrahpDiff从数据库中检索实体,而是以最佳方式检索了该实体,并将其注入以合并到GraphDiff中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM