优化代码和SQL查询以获得更好的性能

时间:2019-03-10 21:53:01

标签: sql-server performance wcf memory-leaks query-optimization

在具有高流量的Web应用程序上,SQL Server Express一直使用大约715-720 MB的服务器内存。据我所知,Express默认具有1GB的可用内存。

问题:715-720 MB(SQL Server Express可用内存限制的70%)是否被认为是高内存使用率?

如果认为上述内存使用率很高,我想知道是否可以优化以下代码和查询以提高性能,并避免内存泄漏(如果存在)。

以下功能是返回JSON的WCF Web服务:

function getRows(db, query) {
  return new Promise((resolve, reject) => {
    db.getAllRows(query, (err, rows) => {
      if (err) reject(err);
      else resolve(rows);
    });
  });
}

const rows = await getRows(...);
for (const row of rows) {
  // ...
}

下面是返回的JSON的示例:

Public Function HomeNews() As List(Of HomeSectionsList) Implements IService.HomeNews
    Dim daQuery As SqlDataAdapter
    Dim dtSections = New DataTable
    Try
        Using myconnection As New SqlConnection(ConnectionStrings("ConnectionString").ToString)
            myconnection.Open()                
            MyCommand = New SqlCommand("SELECT TOP(8) * FROM containers", myconnection)
            daQuery = New SqlDataAdapter(MyCommand)
            daQuery.Fill(dtSections)                
        End Using

       Dim lstHomeSections As New List(Of HomeSectionsList)()

        If dtSections.Rows.Count > 0 Then
            For Each mySectionRow As DataRow In dtSections.Rows
                Dim lstArticles As New List(Of ArticleList)() 
                Using myconnection As New SqlConnection(ConnectionStrings("ConnectionString").ToString)
                    MyCommand = New SqlCommand("SELECT TOP (10) a.*, (SELECT image FROM TABLE_X WHERE id=1) AS ads_image, (SELECT link FROM TABLE_X WHERE id=1) AS ads_link FROM articles a WHERE a.containerid=" & mySectionRow.Item("id") & " ORDER BY a.publish_date DESC", myconnection)

                    lstHomeSections.Add(New HomeSectionsList With {
                                        .id = mySectionRow.Item("id"),
                                        .title = mySectionRow.Item("title"),
                                        .articles = CreateArticleList(MyCommand)})
                End Using                   
            Next
        End If
        Return lstHomeSections
    Catch ex As Exception
        Dim lstHomeSections As New List(Of HomeSectionsList)()
        Return lstHomeSections
    End Try
End Function

Public Function CreateArticleList(myCustomSQLCommand As SqlCommand) As List(Of ArticleList)
    Dim dtSelected As DataTable
    Dim daQuery As SqlDataAdapter
    Try
        Using myconnection As New SqlConnection(ConnectionStrings("ConnectionString").ToString)
            myconnection.Open()
            daQuery = New SqlDataAdapter(myCustomSQLCommand)
            dtSelected = New DataTable
            daQuery.Fill(dtSelected)
        End Using

        Dim lstArticles As New List(Of ArticleList)()
        If dtSelected.Rows.Count > 0 Then
            For Each myArticleRow As DataRow In dtSelected.Rows
                lstArticles.Add(New ArticleList() With {
                                .id = myArticleRow.Item("id"),
                                .title = myArticleRow.Item("title"),
                                .image = myArticleRow.Item("image"),
                                .ad = CreateAdsList()                                   
                    })
            Next
        End If
        Return lstArticles
    Catch ex As Exception
        Dim lstArticles As New List(Of ArticleList)()
        Return lstArticles
    End Try
End Function

Public Function CreateAdsList() As List(Of AdsList)
    Dim daQuery As SqlDataAdapter
    Dim lstAds As New List(Of AdsList)()
    Dim dtAds = New DataTable
    Try
        Using myconnection As New SqlConnection(ConnectionStrings("ConnectionString").ToString)
            myconnection.Open()
            MyCommand = New SqlCommand("SELECT a.image, a.link FROM ads a WHERE id=1", myconnection)
            daQuery = New SqlDataAdapter(MyCommand)
            daQuery.Fill(dtAds)
        End Using

        If dtAds.Rows.Count > 0 Then
            For Each myrow As DataRow In dtAds.Rows                    
                lstAds.Add(New AdsList() With {
                           .image = myrow.Item("image"),
                           .link = myrow.Item("link")
                           })
            Next
        End If
        Return lstAds
    Catch ex As Exception
        Return lstAds
    End Try
End Function
  • 这些查询是否会导致内存泄漏?
  • 这些嵌套的[ { "id": 1, "title": "Container title", "articles": [ { "id": 1, "title": "Title 1", "ad": [ { "image": "path-to-image/image.png", "link": "http://example.com" } ] }, { "id": 2, "title": "Title 2", "ad": [ { "image": "path-to-image/image.png", "link": "http://example.com" } ] }, ] } ] 语句会导致任何延迟吗?

在知道每个函数调用另一个函数的情况下,如何更好地优化这段代码?

0 个答案:

没有答案