C# c-优化SQLite Select语句

C# c-优化SQLite Select语句,c#,sqlite,select,C#,Sqlite,Select,我在数据库中使用System.Data.SQLite,我的select语句非常慢。查询大约5000行数据大约需要3-5分钟。以下是我正在使用的代码: string connectionString; connectionString = string.Format(@"Data Source={0}", documentsFolder + ";Version=3;New=False;Compress=True;"); //Open a new SQ

我在数据库中使用System.Data.SQLite,我的select语句非常慢。查询大约5000行数据大约需要3-5分钟。以下是我正在使用的代码:

        string connectionString;
        connectionString = string.Format(@"Data Source={0}", documentsFolder + ";Version=3;New=False;Compress=True;");
        //Open a new SQLite Connection
        SQLiteConnection conn = new SQLiteConnection(connectionString);
        conn.Open();

        SQLiteCommand cmd = new SQLiteCommand();
        cmd.Connection = conn;
        cmd.CommandText = "Select * From urls";
        //Assign the data from urls to dr
        SQLiteDataReader dr = cmd.ExecuteReader();

        SQLiteCommand com = new SQLiteCommand();
        com.CommandText = "Select * From visits";
        SQLiteDataReader visit = com.ExecuteReader();

        List<int> dbID2 = new List<int>();
        while (visit.Read())
        {
            dbID2.Add(int.Parse(visit[1].ToString()));
        }
        //Read from dr
        while (dr.Read())
        {
            string url = dr[1].ToString();
            string title = dr[2].ToString();
            long visitlong = Int64.Parse(dr[5].ToString());
            string browser = "Chrome";
            int dbID = int.Parse(dr[0].ToString());
            bool exists = dbID2.Any(item => item == dbID);
            int frequency = int.Parse(dr["visit_count"].ToString());

            bool containsBoth = url.Contains("file:///");

            if (exists)
            {
                if (containsBoth == false)
                {
                    var form = Form.ActiveForm as TestURLGUI2.Form1;

                    URLs.Add(new URL(url, title, browser, visited, frequency));
                    Console.WriteLine(String.Format("{0} {1}", title, browser));
                }
            }

        }
        //Close the connection
        conn.Close();
这是另一个需要很长时间的例子:

IEnumerable<URL> ExtractUserHistory(string folder, bool display)
{
    // Get User history info
    DataTable historyDT = ExtractFromTable("moz_places", folder);

    // Get visit Time/Data info
    DataTable visitsDT = ExtractFromTable("moz_historyvisits",
                                           folder);



    // Loop each history entry
    foreach (DataRow row in historyDT.Rows)
    {
        // Select entry Date from visits
        var entryDate = (from dates in visitsDT.AsEnumerable()
                         where dates["place_id"].ToString() == row["id"].ToString()
                         select dates).LastOrDefault();
        // If history entry has date
        if (entryDate != null)
        {
            // Obtain URL and Title strings
            string url = row["Url"].ToString();
            string title = row["title"].ToString();
            int frequency = int.Parse(row["visit_count"].ToString());
            string visit_type;

            //Add a URL to list URLs
            URLs.Add(new URL(url, title, browser, visited, frequency));

            // Add entry to list
            //    URLs.Add(u);
            if (title != "")
            {
                Console.WriteLine(String.Format("{0} {1}", title, browser));
            }
        }
    }

    return URLs;
}



DataTable ExtractFromTable(string table, string folder)
{
    SQLiteConnection sql_con;
    SQLiteCommand sql_cmd;
    SQLiteDataAdapter DB;
    DataTable DT = new DataTable();

    // FireFox database file
    string dbPath = folder + "\\places.sqlite";

    // If file exists
    if (File.Exists(dbPath))
    {
        // Data connection
        sql_con = new SQLiteConnection("Data Source=" + dbPath +
                            ";Version=3;New=False;Compress=True;");

        // Open the Connection
        sql_con.Open();
        sql_cmd = sql_con.CreateCommand();

        // Select Query
        string CommandText = "select * from " + table;

        // Populate Data Table
        DB = new SQLiteDataAdapter(CommandText, sql_con);
        DB.Fill(DT);

        // Clean up
        sql_con.Close();
    }
    return DT;
}

现在,如何优化这些选项以使它们更快?

< P>除了将更多的数据聚合作为连接加入SQL之外,还可以考虑让SQLSeDATaReADER提供数据类型,而不是总是解析值。

例如,您有以下行:

long visitlong = Int64.Parse(dr[5].ToString());
dr[5]是一个Sqlite值,首先将其转换为字符串,然后将其解析为长字符串。这些解析操作需要时间。为什么不改为:

long visitlong = dr.GetInt64(5);
或:

尽可能检查并使用它们,而不是解析值

编辑:


请注意,这需要将数据存储为正确的类型。如果数据库中的所有内容都存储为字符串,则某些解析将不可避免。

请确保最近运行了SQL命令ANALYZE{db | table | index}


我最近遇到了一种情况,查询在VisualStudio中快速运行1分钟调试。事实证明,由于我在Navicat SQLite v3.7中进行了数据库设计,因此统计数据与Visual Studio v3.8中System.Data.SQLite使用的数据不同。运行分析;在Visual Studio的整个数据库文件中,更新了v3.8使用的[sqlite_statX]表。此后,这两个地方的速度都是一样的。

您考虑过让数据库为您进行连接吗?谢谢,但我发现我的问题是,当我通过调试运行它时,速度非常慢,但当我在没有调试的情况下运行它时,它会在几秒钟内完成。
long visitlong = dr.GetInt64(dr.GetOrdinal("columnName"));