加载品牌 ->类别->高效的产品层次结构

发布于 2024-11-24 23:05:13 字数 495 浏览 1 评论 0原文

我有一个包含 3 个对象的系统 - BrandsCategoriesProducts。我使用 Nhibernate 作为我的 ORM。

一款产品分配给一个品牌和多个类别。对于我们正在构建的电子商务网站,我们需要在左侧显示品牌菜单。对于每个品牌,其想法是显示仅包含该品牌的产品的类别。

例如,如果我们有 3 个类别 - 露营、远足和登山,以及 3 个品牌 A、B、C。如果品牌 A 没有“攀岩”类别下的产品,则该类别不会显示。

问题是如何有效地做到这一点。对于每个品牌,需要遍历每个类别,并检查当前品牌的该类别下是否至少有一种产品。对于 200 个类别和 20 个品牌,在最坏的情况下将导致大约 2000 次数据库调用。

另一种选择是将所有产品加载到内存中,但如果有很多产品(20,000+),这种方式也会很慢。

我可以做任何想法/优化,以使其加载速度更快吗?

I have a system which has 3 objects - Brands, Categories and Products. I am using Nhibernate as my ORM.

A product is assigned to one brand, and to multiple Categories. For an ecommerce website we are building, we need to show the brands menu on the left. For each brand, the idea is to show the categories which ONLY have a product, of that brand.

For example, if we have 3 categories - Camping, Hiking and Climbing, and 3 brands A,B,C. If brand A has no products which are under the category 'Climbing', the category wouldn't show.

The problem is how to do this efficiently. For each brand, it requires to go through each category, and check if there is at least one product under that category, of the current brand. With 200 categories, and 20 brands, this would result in approximately 2000 database calls at the worst case.

The other option would be to load all products in memory, but if there are a lot of products (20,000 +), it would be slow also this way.

Any ideas / optimizations I could do, to make it load much faster?

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

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

发布评论

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

评论(1

你对谁都笑 2024-12-01 23:05:13

您可以使用 QueryOver 获取每个品牌的类别以及该类别中的产品数量。效果如下(我目前无法访问我的参考文献,因此这可能不是 100% 准确,但您将能够理解我的意思):

QueryOver<Brand>()
   .JoinQueryOver(b => b.Categories)
   .Select(c => c.CategoryId) //or whatever else you want to select
    .SelectCount(c=> c.Products);

另一个选择可以是定义一个公式 property 用于返回关联产品数量的 Category 类与该类别。

you can use QueryOver to get, for each brand- its categories and the count of products in that category. something to the effect of (I currently don't have access to my references, so this may not be 100% accurate, but you'll be able to get my meaning):

QueryOver<Brand>()
   .JoinQueryOver(b => b.Categories)
   .Select(c => c.CategoryId) //or whatever else you want to select
    .SelectCount(c=> c.Products);

another option can be to define a formula property for the Category class that would return the count of products associated with that Category.

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