使用 Fluent NHibernate 检索大型数据集

发布于 2024-12-02 15:40:46 字数 132 浏览 3 评论 0原文

我正在构建一个解决方案,从数据库中检索大量数据(5k 到 10k 记录)。我们现有的数据访问层使用 Fluent NHibernate,但我“害怕”通过水化表示数据库实体的对象模型会产生大量开销。

我可以简单地检索 ADO 数据集吗?

I'm building a solution where I'm retrieving large amounts of data from the database(5k to 10k records). Our existing data access layer uses Fluent NHibernate but I'm "scared" that I will incur a large amount of overhead by hydrating object models that represent the database entities.

Can I retrieve simply an ADO dataset?

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

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

发布评论

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

评论(3

仙女山的月亮 2024-12-09 15:40:46

是的,您应该关心它的性能。您可以查看使用 NHibernate 的 IStatelessSession 功能。然而,这可能不会给您带来您想要的性能。虽然我从 2.1.2GA 开始就没有使用过 NH,但我发现在批量操作方面它们不太可能大幅提高 NH 的性能。坦率地说,在批量操作方面,NH(以及大多数 ORM)很糟糕。

问:我可以只检索 ADO 数据集吗?

当然可以。仅仅因为您使用 NHibernate 并不意味着您不能新建 ADO.NET 连接并直接访问数据库。

尽管我讨厌数据表和数据集,但这是您可能需要考虑使用它们的极少数情况之一,而不是增加映射/创建与 10K 行数据关联的对象的开销。

Yes you should be concerned about the performance of this. You can look at using the IStatelessSession functionality of NHibernate. However this probably won't give you the performance you are looking for. While I haven't used NH since 2.1.2GA, I would find it unlikely that they've substantially improved the performance of NH when it comes to bulk operations. To put it bluntly, NH just sucks (and most ORMs in general for that matter) when it comes to bulk operations.

Q: Can I retrieve simply an ADO dataset?

Of course you can. Just because you're using NHibernate doesn't mean you can't new up an ADO.NET connection and hit the database in the raw.

As much as I loathe data tables and data sets, this one of the rare cases you might want to consider using them instead of adding the overhead of mapping / creating the objects associated with your 10K rows of data.

_畞蕅 2024-12-09 15:40:46

根据您需要的性能,有几种选择。使用 sqldatareader 是最好的选择,因为它是几乎每个 .NET ORM 实现的基础。除了速度最快之外,如果您不需要在查询后保存所有记录的列表,它还可以占用更少的内存。

至于您对性能的担忧,5k-10k 记录并不算高。我之前已经从 nhibernate 中提取了超过一百万行,但显然记录并不大,而且只是针对单个案例。如果您在高流量网站上执行此操作,那么当遇到瓶颈时,您当然必须提高效率。如果您正在考虑数据集,我建议您尝试 Massive 因为它仍然比DataSet/Table 更方便。

Depending on how much performance you need, there are a few options. Nothing will ever beat using a sqldatareader, as that's what's underneath just about every .NET ORM implementation. In addition to being the fastest, it can take a lot less memory if you don't need to save a list of all the records after the query.

Now as for your performance worries, 5k-10k records isn't that high. I've pulled upwards of a million rows out of nhibernate before, but obviously the records weren't huge and it was for a single case. If you're doing this on a high traffic website then of course you will have to be more efficient if you're hitting bottlenecks. If you're thinking about datasets, I'd suggest instead trying Massive because it should still be more efficient than DataSet/Table and more convenient.

不气馁 2024-12-09 15:40:46

您可以使用“标量查询”,它实际上是返回 object[] 列表(每行一个 object[])的本机 SQL 查询:

sess.CreateSQLQuery("SELECT * FROM CATS")
 .AddScalar("ID", NHibernateUtil.Int64)
 .AddScalar("NAME", NHibernateUtil.String)
 .AddScalar("BIRTHDATE", NHibernateUtil.Date)

NHibernate 文档中的示例: http://nhibernate.info/doc/nh/en/index.html#d0e10794

You can use "Scalar queries", which is in fact native SQL query returning a list of object[] (one object[] per row):

sess.CreateSQLQuery("SELECT * FROM CATS")
 .AddScalar("ID", NHibernateUtil.Int64)
 .AddScalar("NAME", NHibernateUtil.String)
 .AddScalar("BIRTHDATE", NHibernateUtil.Date)

Example from NHibernate documentation: http://nhibernate.info/doc/nh/en/index.html#d0e10794

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