Hardik
Hardik

Reputation: 259

Multithread + C# + Performance + SQLite

I have Windows Form application.
(C# with visual studio 2010, Framework : 4.0)

Database: .db (file database), connection object through SQLite

Thread th = new Thread(() => database(node, xNodetcname, xNodesqlquery1, xNodeHint, e, inXml));
th.Name = thread;
th.Start();

Above code create each thread and processing parallel on database() function.
Each thread having one SQL Query which fetching data from database.

While I not use Multithreading the performance is better but when I use Multithreading the performance is down.

Example:

Without Multithreading 3 query processing time = 1.5 minutes
With Multithreading 3 query processing time = 1.9 minutes.

My aim is to reduce the processing time of query.

Upvotes: 0

Views: 974

Answers (2)

Mario Stoilov
Mario Stoilov

Reputation: 3447

There are a few things you have to take into consideration here:

  1. Is your db connection in Multi-Threading mode(As described in this document)?
  2. Is the database engine suitable for multithreading (hint, SQLite is not, see @TomTom answer)
  3. Are you using a thread pool(you are not) or are you initializing a new thread every time (which is rather slow)

Upvotes: 2

TomTom
TomTom

Reputation: 62111

Then generally stay away from threads.

Some basic edutation: In most cases database performance is limited by IO, not CPU. IO can partialyl be mitigated by using a lot of memory as buffers, hence large database servers have TONS of memory.

You run a small leightweight databasde. It likely is not running on a database server level hardware or a SSD - so it has a IO problem. Perforamcne will be limited by IO.

Now multiple threads make sure that the IO side (hard disc) is inefficieent, especialyl because a winform app is not running normally on a high end IO subsystem.

Ergo: if you want a faster query then:

  • Optimize the query. MIssing index?
  • Get proper hardware and / or upgrade to a heaver setup. A SSD is a great help here.

Do not use multi threading - try to solve it on SQL level but accept that this may not be possible. THere is a reason companies still actually use real database servers to handle large data amounts. And SQLite may not be a good option - no chance saying it is or not as you totally ignore that side in your information.

Upvotes: 6

Related Questions