c# - 优化SQLite Select语句

时间:2012-07-25 23:30:10

标签: 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 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;
}

现在,我如何优化它们以便它们更快?

2 个答案:

答案 0 :(得分:1)

除了将更多的数据聚合作为连接移动到SQL之外,您还可以考虑让SQLiteDataReader提供数据类型,而不是始终解析值。

例如,您有以下行:

long visitlong = Int64.Parse(dr[5].ToString());

dr[5]是一个Sqlite值,您首先将其转换为字符串,然后将其解析为long。这些解析操作需要时间。为什么不这样做:

long visitlong = dr.GetInt64(5);

或者:

long visitlong = dr.GetInt64(dr.GetOrdinal("columnName"));

查看the various methods that SqliteDataReader offers并尽可能使用它们,而不是解析值。

修改

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

答案 1 :(得分:0)

确保您最近运行SQL命令“ANALYZE {db | table | index};”。

我最近在我的ER软件(Navicat)中遇到了查询运行速度快(<1秒)的情况,即:没有调试,但在Visual Studio中调试非常慢(> 1分钟)。事实证明,因为我在Navicat(SQLite v3.7)中进行了数据库设计,所以统计信息与Visual Studio(v3.8)中System.Data.SQLite使用的统计信息不同。运行“ANALYZE;”在Visual Studio的整个数据库文件中更新了v3.8使用的[sqlite_statX]表。之后两个地方的速度都是一样的。