nhibernate - 归档记录

发布于 2024-12-09 06:53:10 字数 1313 浏览 0 评论 0原文

这是我们的领域模型的简单视图(我们在医疗保健领域):

Account
{
  List<Registration> Registrations {...}
  DateTime CreatedDate {...}
  Type1 Property1 {...}
  Type2 Property2 {...}
  ...

 }

Registration
{
 InsuranceInformation {...}
 PatientVisit {...}
 Type1 Property1 {...}
 Type2 Property2 {...}
 ...

 } 

设置

  1. 我们使用 Nhibernate/FluentNH 来设置和配置 SessionFactory
  2. 假设我们已经设置了所有必需的表索引

用法

  1. 我们每天获得约 10,000 个新帐户
  2. 我们总共有大约 50 万个帐户。
  3. 我们有几个对这些帐户进行操作的 Linq 查询
  4. 我们所有的查询都使用 Linq,大多数查询都是使用谓词构建器模式动态构建的(我们不使用 Hql)

问题是, 随着帐户数量的增加,这些查询的执行时间也会增加。

注意:

  1. 只有在 48 小时窗口内的帐户才与 我们的查询/应用程序。但是,旧帐户需要保留 (所以无法删除)。即使这些帐户不需要 稍后分析可能会使用的应用程序 为了

解决这个性能问题:

  1. 我们正在考虑归档超过 48 小时的帐户
  2. 创建一个与主数据库具有相同模式的归档数据库
  3. 添加一个计划在每晚基本运行的 Windows 服务,该服务将“旧”帐户从数据库中移走主数据库到存档数据库
  4. Windows 服务将使用 nhiberate 从主数据库读取旧帐户并将旧帐户(再次使用 nhibernate)保存到存档数据库,然后从主数据库中删除旧帐户。目前,我们认为这项服务将一次移动一个帐户,直到所有旧帐户都移动到存档数据库。
  5. 有时,当我们确实收到从存档数据库恢复帐户的请求时,我们会反转上述步骤

问题:

  1. 这种存档方法有什么好处吗?如果没有,为什么?你能建议吗 一些替代的实现?
  2. 在复制过程中我可以使用相同的 sessionfactory 连接到主数据库和存档数据库吗?如何动态更改连接字符串?我可以有两个模拟的开放会话与两个数据库一起使用吗?
  3. 我可以使用这种方法一次复制多个帐户吗?批量复制和批量删除?

任何帮助表示感谢,感谢您的投入。

This is a simplistic view of our domain model (we are in healthcare):

Account
{
  List<Registration> Registrations {...}
  DateTime CreatedDate {...}
  Type1 Property1 {...}
  Type2 Property2 {...}
  ...

 }

Registration
{
 InsuranceInformation {...}
 PatientVisit {...}
 Type1 Property1 {...}
 Type2 Property2 {...}
 ...

 } 

Setup

  1. We use Nhibernate/FluentNH to setup and configure the SessionFactory
  2. And let's assume that we have setup all the required table indices

Usage

  1. We get ~10,000 new account a day
  2. We have about 500K accounts Total.
  3. We have several Linq queries that operate over these accounts
  4. All our queries use Linq, most queries are dynamically built using predicate builder pattern (we don't use Hql)

The problem is that,
As the number of accounts increases the execution time for these queries increases.

Note:

  1. Only accounts that are within at 48 hours window are relevant for
    our queries / application. But, Older accounts need to be preserved
    (so cannot be deleted). Even though these accounts are not needed by
    the application it may be consumed later by the analytics
    application

To solve this performance issue:

  1. we are considering archiving accounts that are older than 48hrs
  2. Creating an Archive database with the same schema as the main db
  3. Adding a windows service that is scheduled to run on a nightly basic that moves "old" accounts from the main db to the archive db
  4. The windows service will using nhiberate to read old accounts from the main db and save the old accounts(again using nhibernate) to the archive database, and then delete the old accounts from the main db. Right now, we think this service will move one account at a time until all the old accounts are moved to the archive database.
  5. Ocassionally, when we do get a request to restore an account from the archive db, we will reverse the above step

Questions:

  1. Is this archival approach any good? If not, why? can you suggest
    some alternate implementations?
  2. Can I use the same sessionfactory to connect to the main db and the archive db during the copy process? How can i change the connection string dynamically? Can I have two simulateous open sessions that work with two database
  3. Can I copy more than one account at a time using this approach? Batch Copy and batch deletes?

Any help appreciated, thank you for your input.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ㄟ。诗瑗 2024-12-16 06:53:10

我认为你的问题与数据库相关,然后是 nhibernate。拥有 500k 条记录的数据库并不算多。要优化访问,您应该考虑如何查询以及如何优化这些查询。

  1. 只查询您需要的数据
  2. 通过创建索引来优化您的表
  3. 使用 20/80 规则,找到 20% 昂贵的查询并优化代码/查询。你的编程速度将提高 80%
  4. NHibernate:优化你的映射
  5. HHibernate:如果你进行多次更新,请使用批处理大小
  6. 如果代码中难以完成某些操作,则添加存储过程

如果你的数据库增长,请聘请数据库专家来提供有关数据库优化的建议(他们可以将您的表现提高 10% 至 90%)。您首先需要他几天,然后每周/每月一次,具体取决于工作量。

I think your issue is more database related then nhibernate. A database with 500k records is not so much. To optimize access you should think about how you query and how to optimize for those queries.

  1. Query only the data you need
  2. Optimize your table by making indexes
  3. Use the 20/80 rule, find the 20% expensive queries and optimize the code/queries. You program will be 80% faster
  4. NHibernate: optimize your mappings
  5. HHibernate: use batch size if you do multiple updates
  6. Add stored procedures if something is hard to do in code

If your db grows, hire a db expert to advise on database optimization (they can improve your performance by 10% to 90%). You need him first for a few days and then once a week/month depending on how much work there is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文