0

I want to use more threads division read a sqlite table, how to achieve it? For example: a table has 1000 rows, I want to use 2 threads read the table, every thread only read 500 rows. How to achieve it? Could anyone help me? Thank you very much!

This is my single thread code:

string conn = @"Data Source=" + AppDomain.CurrentDomain.BaseDirectory + dbName + ";Version=3;";

SQLiteConnection sqliteConn = new SQLiteConnection(conn);
sqliteConn.Open();

string selectSql = "select fieldName1,fieldName2 from TableName";  
SQLiteCommand command = new SQLiteCommand(selectSql, sqliteConn);

SQLiteDataReader reader = command.ExecuteReader();

Dictionary<string, string> dic = new Dictionary<string, string>();


while (reader.Read())
{   
    if (reader["fieldName1"] != null && reader["fieldName1"].ToString().Trim().Length > 0)
    {

        dic.Add(reader["fieldName1"].ToString(), reader["fieldName2"].ToString()); // I need to storage the two field to dictionary<string,string>
    }   
}

reader.close();

my issue is : the tableName rows is larger, use single thread read the table is slower, how to use multithreading read the table? Could anyone help me? Thanks!

Superman
  • 285
  • 5
  • 17
  • There is [LIMIT/OFFSET](http://stackoverflow.com/q/2728999/1997232) to run 2 queries (first read 0-500 rows, second 501-999). But the more interesting question why do you want to do it? Do you expect it to be faster? It will be slower (due to concurrent access to the same table, which will lead to not sequential file reading = performance issue). – Sinatr Mar 15 '17 at 09:09
  • yeah, I want to read the table faster, because the table rows are large. Do you have a better way? – Superman Mar 16 '17 at 01:14
  • It'd help if you provided a sample of the code that you're already using, that way folks who want to answer the question have a template to modify. – Nat Mar 20 '17 at 04:08
  • @Nat, I do not know how to write the code, could you help me? – Superman Mar 20 '17 at 05:26
  • @Superman I don't usually work with SQLite and I don't know your setup, so I don't think it'd be too easy to help there. If you do figure out how to write the basic example, I could help show you how to multi-thread it. – Nat Mar 20 '17 at 05:36
  • ok,thank you very much! – Superman Mar 21 '17 at 01:41

0 Answers0