vb.net代码的性能改进

时间:2015-09-15 09:23:08

标签: vb.net optimization vb.net-2010

我需要将72列的5000万条记录写入文本文件,文件大小增加到9.7gb。

我需要根据XML文件中定义的长度检查每个列长度是否需要格式化。

逐个从oracle中读取记录并检查格式并写入文本文件。

要写5个crores记录需要超过24小时。如何在下面的代码中提高性能。

Dim valString As String = Nothing
        Dim valName As String = Nothing
        Dim valLength As String = Nothing
        Dim valDataType As String = Nothing
        Dim validationsArray As ArrayList = GetValidations(Directory.GetCurrentDirectory() + "\ReportFormat.xml")
        Console.WriteLine("passed xml")
        Dim k As Integer = 1


        Try
            Console.WriteLine(System.DateTime.Now())
            Dim selectSql As String = "select * from table where
            " record_date >= To_Date('01-01-2014','DD-MM-YYYY') and record_date <= To_Date('31-12-2014','DD-MM-YYYY')"
            Dim dataTable As New DataTable
            Dim oracleAccess As New OracleConnection(System.Configuration.ConfigurationManager.AppSettings("OracleConnection"))
            Dim cmd As New OracleCommand()
            cmd.Connection = oracleAccess
            cmd.CommandType = CommandType.Text
            cmd.CommandText = selectSql
            oracleAccess.Open()
            Dim Tablecolumns As New DataTable()
            Using oracleAccess
                Using writer = New StreamWriter(Directory.GetCurrentDirectory() + "\FileName.txt")
                    Using odr As OracleDataReader = cmd.ExecuteReader()
                        Dim sbHeaderData As New StringBuilder
                        For i As Integer = 0 To odr.FieldCount - 1
                            sbHeaderData.Append(odr.GetName(i))
                            sbHeaderData.Append("|")
                        Next
                        writer.WriteLine(sbHeaderData)
                        While odr.Read()
                            Dim sbColumnData As New StringBuilder
                            Dim values(odr.FieldCount - 1) As Object
                            Dim fieldCount As Integer = odr.GetValues(values)
                            For i As Integer = 0 To fieldCount - 1
                                Dim vals As Array = validationsArray(i).ToString.ToUpper.Split("|")
                                valName = vals(0).trim
                                valDataType = vals(1).trim
                                valLength = vals(2).trim

                                Select Case valDataType
                                    Case "VARCHAR2"
                                        If values(i).ToString().Length = valLength Then
                                            sbColumnData.Append(values(i).ToString())
                                            'sbColumnData.Append("|")
                                        ElseIf values(i).ToString().Length > valLength Then
                                            sbColumnData.Append(values(i).ToString().Substring(0, valLength))
                                            'sbColumnData.Append("|")
                                        Else
                                            sbColumnData.Append(values(i).ToString().PadRight(valLength))
                                            'sbColumnData.Append("|")
                                        End If
                                    Case "NUMERIC"
                                        valLength = valLength.Substring(0, valLength.IndexOf(","))
                                        If values(i).ToString().Length = valLength Then
                                            sbColumnData.Append(values(i).ToString())
                                            'sbColumnData.Append("|")
                                        Else
                                            sbColumnData.Append(values(i).ToString().PadLeft(valLength, "0"c))
                                            'sbColumnData.Append("|")
                                        End If
                                        'sbColumnData.Append((values(i).ToString()))
                                End Select
                            Next
                            writer.WriteLine(sbColumnData)

                            k = k + 1
                            Console.WriteLine(k)
                        End While
                    End Using
                    writer.WriteLine(System.DateTime.Now())
                End Using
            End Using

            Console.WriteLine(System.DateTime.Now())
            'Dim Adpt As New OracleDataAdapter(selectSql, oracleAccess)
            'Adpt.Fill(dataTable)
            Return Tablecolumns
        Catch ex As Exception
            Console.WriteLine(System.DateTime.Now())
            Console.WriteLine("Error: " & ex.Message)
            Console.ReadLine()
            Return Nothing
        End Try

0 个答案:

没有答案