【问题标题】:.NET - Efficient collection of database entities?.NET - 数据库实体的有效集合?
【发布时间】:2010-01-15 15:46:44
【问题描述】:

我在 3d 派对 ORM 框架中有一个 Page 类和一个 PageCollection 类。我可以根据参数(pageid、parentid、url 等)填充 PageCollection(SQL 查询)。但是我需要多次访问 ASP.NET MVC 网站(站点地图、身份验证)的数据,所以我选择加载所有页面 1 次并引用该(全局)集合。

GlobalClass.Pages //is PageCollection containing all pages

我现在已经创建了基于前面提到的参数(pageid、parentid、url 等)返回临时子集合或单个实体的函数。

GlobalClass.Pages.GetByPageId(id) //returns single page entity
GlobalClass.Pages.GetByParentId(parentid) //returns subcollection

但是网站变得很慢。

这里怎么走?

  • 缓存子集合 (GetByParent())?
  • 为集合创建内部哈希查找表
  • 还有什么...

Namespace BLL
Public Class PageCollection
    Inherits CustomCollectionBase
    
    Public Sub New()

    End Sub

    
    Public Sub LoadByParent(ByVal PagParent As Integer)
        If PagParent = 0 Then
            Me.whereAdd('parent IS NULL')
        Else
            Me.whereAdd('parent = ' & PagParent.ToString())
        End If

        Me.Load(Me.data)
    End Sub

    Public Function GetBySiteMapNode(ByVal node As SiteMapNode) As BLL.Page
        Return Me.GetByUrl(node.Url)
    End Function

    Public Function GetById(ByVal id As Integer) As BLL.Page
        For Each p In Me
            If p.PagAutoKey = id Then Return p
        Next
        Return Nothing
    End Function

    Public Function GetByUrl(ByVal url As String) As BLL.Page
        For Each p In Me
            If p.Url = url Then Return p
        Next
        Return Nothing
    End Function

    Public Function GetByParent(ByVal parent As Integer) As BLL.PageCollection
        Dim pc As New PageCollection
        For Each p In Me
            If p.PagParent = parent Then pc.Add(p)
        Next
        Return pc
    End Function
End Class
End Namespace

【问题讨论】:

  • 是的,一些非常重要的信息没有给出:)
  • 慢到底是什么? GetBy 方法是否比您预期的要慢?如果 GetBy 方法命中数据库而不是全局集合,它们会更快吗?

标签: .net asp.net-mvc vb.net collections performance


【解决方案1】:

我要做的第一件事是确定您的网站运行缓慢的原因。如果我每次假设性能不佳的原因都有一美元,结果却是其他原因......

在整个代码中设置跟踪点并找出您将时间花在哪里。您当前的方法可能很好,但在一些实现细节上已经挂了。只修复您知道已损坏的部分。

【讨论】:

  • 有趣的是,我正在查询每个页面实体中的角色。也许这需要先做一些工作:)
  • 我开始使用 linq,帮助很大。谢谢
猜你喜欢
  • 2023-03-13
  • 2016-02-24
  • 1970-01-01
  • 1970-01-01
  • 2021-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-18
相关资源
最近更新 更多