【发布时间】: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