【问题标题】:DataReader running very slowDataReader 运行很慢
【发布时间】:2018-12-27 11:44:03
【问题描述】:

我目前正在处理一个包含超过 5000 万条记录的数据库,我在其中读取了一个人想要在数据库中搜索的文件等。我注意到我的数据读取器部分运行速度特别慢,而查询似乎几乎是即时的(数据库被索引)。我只是想知道有人知道为什么它似乎运行缓慢吗?

con.Open() 使用 sw 作为新的 StreamWriter("G:\USER\SEARCH-RESULTS.txt") 试试

            For Each word As String In result

                Using com As New SqlCommand("select t.SmeNbr, t.FilPth, r.MaxDate, t.DteAdd, t.LnePos from (Select SmeNbr, MAX(FilDte) as MaxDate from Test_Table where SmeNbr = @word group by SmeNbr)r inner join Test_Table t on  t.SmeNbr = r.SmeNbr and t.FilDte = R.MaxDate", con)
                    com.Parameters.AddWithValue("@word", word)
                    Using RDR = com.ExecuteReader

                        If RDR.HasRows Then
                            Do While RDR.Read
                                MyFilePath = RDR.Item("FilPth").ToString()
                                linePos = RDR.Item("LnePos").ToString()

                                Using sr As New StreamReader(MyFilePath)
                                    sr.BaseStream.Seek(4096 * (linePos - 1), SeekOrigin.Begin)
                                    FoundWords.Add(sr.ReadLine)
                                    For Each item As String In FoundWords
                                        sw.WriteLine(item)

                                    Next
                                    FoundWords.Clear()
                                End Using
                            Loop

                        Else

                            Continue For

                        End If
                    End Using



                End Using





            Next

    Catch ex As Exception
    MessageBox.Show("Couldn't process search")
    Finally
    con.Close()

    End Try
    End Using
    MsgBox("Complete!")

所以它运行完美,因为它通过查询非常快速地获取了我想要的记录和信息位,所有甚至写入新文件的结果都几乎是即时的,我使用了断点,就像我说的那样在“Using RDR = com.ExecuteReader”和“If RDR.HasRows Then”之间花费时间

任何帮助或想法将不胜感激。

【问题讨论】:

  • 如果您表示执行ExecuteReader 行的速度很慢,我不知道您所说的“查询似乎几乎是即时的”是什么意思,因为那是运行查询的行。
  • 那么执行阅读器也会执行我的整个“select t.SmeNbr, t.FilPth, r.MaxDate, t.DteAdd,”......等等吗?
  • 是的,名为ExecuteXxx 的方法是执行查询的方法。在此之前的任何事情(打开连接除外)都只是本地准备,根本不涉及服务器。
  • 是的,对不起,我当然是那么愚蠢,那么如果你能帮助我理解为什么当我在 MSSMS 中执行该查询时它几乎是即时的,但在 VB 中执行似乎需要很长时间?还有有什么方法可以加快速度吗?谢谢你顺便说一句! :)
  • @K.Madden,是的。它在 SSMS 中运行得更快的原因可能是因为您使用了文字而不是参数。如果您指定 Unicode 字符串文字(例如 N'SmeNbrValue'),您在 SSMS 中也会遇到同样的问题。

标签: sql sql-server vb.net ssms


【解决方案1】:

com.Parameters.AddWithValue("@word", word)

AddWithValue 从提供的 .NET 对象值推断参数数据类型。由于 .NET 字符串是 Unicode,因此此代码将添加一个带有实际值长度的 nvarchar(n) 参数。我从您的 cmets 中看到,实际的列数据类型是 char(13),因此最好将其明确指定为参数数据类型:

com.Parameters.Add("@word", SqlDbType.Char, 13).Value = word

AddWithValue 的含义是,由于数据类型不匹配,可能不会使用索引,并且 SQL Server 过程缓存中可能有许多相同查询的变体,它们仅在长度上有所不同。由于这些原因,我建议使用avoid AddWithValue

【讨论】:

  • 太棒了!像魅力一样工作!非常感谢。 :D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-12
相关资源
最近更新 更多