SQLite搜索速度很慢

时间:2013-11-28 23:55:32

标签: vb.net sqlite

这是我从SQLite数据库中搜索的代码:

flResults.Controls.Clear()
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If
        Dim Querytxt As String = "Select * from trancebeats Where Cat LIKE '%" & txtSearch.Text & "%' OR  Title LIKE '%" & txtSearch.Text & "%' OR Artist LIKE '%" & txtSearch.Text & "%' OR Album LIKE '%" & txtSearch.Text & "%' OR Year LIKE '%" & txtSearch.Text & "%' OR Genre LIKE '%" & txtSearch.Text & "%' OR Publisher LIKE '%" & txtSearch.Text & "%' OR Site LIKE '%" & txtSearch.Text & "%' Group By Album Order By Album"
        Dim Adp As SQLiteDataAdapter = New SQLiteDataAdapter
        Dim Cmd As SQLiteCommand = New SQLiteCommand
        Dim TableSRC As New DataTable
        TableSRC.Rows.Clear()
        Dim i As Integer
    With Cmd
        .CommandText = Querytxt
        .Connection = con
    End With

    With Adp
        .SelectCommand = Cmd
        .Fill(TableSRC)
    End With

    If TableSRC.Rows.Count > 0 Then
        lblResultsFound.Text = TableSRC.Rows.Count & "  Results founds"
        For i = 0 To TableSRC.Rows.Count - 1
            Threading.Thread.Sleep(10)
            Dim pic As New PictureBox
            Dim lblArtist As New Label
            Dim lblAlbum As New Label
            Dim lblLabel As New Label
            Dim lblCat As New Label
            Dim fl As New Panel
            pic.Size = New Size(75, 75)
            pic.SizeMode = PictureBoxSizeMode.StretchImage
            pic.Cursor = Cursors.Hand
            pic.BorderStyle = BorderStyle.Fixed3D
            pic.Tag = TableSRC.Rows(i)("Cat")
            pic.Name = TableSRC.Rows(i)("Cat") & "pic"
            pic.Location = New Point(7, 7)
            Try
                If System.IO.File.Exists(TableSRC.Rows(i)("Cover")) Then
                    Dim xx As Image
                    Using str As Stream = File.OpenRead(TableSRC.Rows(i)("Cover"))
                        xx = Image.FromStream(str)
                    End Using
                    pic.Image = xx
                End If
            Catch ex As Exception

            End Try
            lblAlbum.AutoEllipsis = True
            lblArtist.AutoEllipsis = True
            lblLabel.AutoEllipsis = True
            lblCat.AutoEllipsis = True
            lblAlbum.AutoSize = False
            lblArtist.AutoSize = False
            lblLabel.AutoSize = False
            lblCat.AutoSize = False
            lblAlbum.Size = New Size(100, 15)
            lblArtist.Size = New Size(100, 15)
            lblLabel.Size = New Size(100, 15)
            lblCat.Size = New Size(100, 15)
            lblAlbum.Text = TableSRC.Rows(i)("Artist")
            lblArtist.Text = TableSRC.Rows(i)("Album")
            lblLabel.Text = TableSRC.Rows(i)("Publisher")
            lblCat.Text = TableSRC.Rows(i)("Cat")
            fl.Size = New Size(200, 95)
            fl.BackColor = System.Drawing.Color.SteelBlue
            fl.Controls.Add(pic)
            fl.Controls.Add(lblArtist)
            fl.Controls.Add(lblAlbum)
            fl.Controls.Add(lblLabel)
            fl.Controls.Add(lblCat)
            fl.Name = TableSRC.Rows(i)("Cat") & "fl"
            AddHandler pic.Click, AddressOf CatClick
            lblArtist.Location = New Point(87, 12)
            lblAlbum.Location = New Point(87, 30)
            lblLabel.Location = New Point(87, 66)
            lblCat.Location = New Point(87, 48)
            AddHandler fl.Click, AddressOf flClick
            AddHandler lblArtist.Click, AddressOf flClick1
            AddHandler lblAlbum.Click, AddressOf flClick1
            AddHandler lblLabel.Click, AddressOf flClick1
            AddHandler lblCat.Click, AddressOf flClick1

            flResults.Controls.Add(fl)
        Next
    Else
        Dim lblNothing As New Label
        lblNothing.Text = "No Results Found!!"
        lblNothing.ForeColor = System.Drawing.Color.White
        flResults.Controls.Add(lblNothing)
    End If

我有超过5000行

当我进行搜索时,结果需要时间......

我的代码错了或者我必须改变搜索方式???

当它向我显示结果时...例如它将显示100个结果....我必须等到它完成显示结果然后我才能找到我正在搜索的内容.....

2 个答案:

答案 0 :(得分:2)

所有这些通配符都需要一些时间来搜索,即使是在一个小的记录集上,因为每个都需要进行全表扫描。

加速SQL查询通常涉及索引相关列。与非索引列相比,可以比更多更快地搜索索引列。但是,在搜索字词开头使用通配符会否定您从索引编制中获得的好处。

要获得索引的好处,您可以将每个列存储两次,一次使用反向字符串,索引所有列,并搜索" ... somefield LIKE 'term%' OR somefield_reversed LIKE 'term_reversed%' ... "为了这一个查询,要添加很多复杂性但是;如果这一查询非常快,那么我只会这样做。

您还可以使用其他(可能更好的)选项 - SQLite有一个您可能想要查看的FTS(全文搜索)扩展程序:http://www.sqlite.org/fts3.html

答案 1 :(得分:0)

为什么不在查询中指定一个列,朋友?

也许在桌面上放置一个Combobox,其中包含您在桌面上的所有列,然后每当用户搜索时,程序就会运行一个查询,其表格基于用户选择的Combobox

示例:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

          Combobox1.Items.Add("Cat")
          Combobox1.Items.Add("Title")
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

     Dim Querytxt As String = "Select * from trancebeats Where " & _
     combobox1.Text & " LIKE '%" & txtSearch.Text & _
     "%' Group By Album Order By Album"

     End Sub

在这里,您可以让用户只指定程序应在哪个列中查找数据。