Reputation: 341
I am trying to understand PLINQ. So for that,I am querying database which has 102915 products in it.
But shockingly I am seeing that PLINQ takes 18 secs where normal query takes only 4 secs. To understand, I have read this article, PLINQ Performs Worse Than Usual LINQ. But still, I couldn't understand why this took so many secs.
To remove over head, I removed .order(m.sku)
in PLINQ, but still it gives same result. Here, is a code for LINQ and PLINQ version.
PLINQ Version
shootersEntities model = new shootersEntities();
var IsOnline = cBOnline.Checked;
var IsDeleted = cBDeleted.Checked;
Stopwatch s = new Stopwatch();
s.Start();
var p = from m in model.products.AsParallel()
where ((m.productOnline == IsOnline) || (m.deleted == IsDeleted))
select new { m.sku, m.productCode, m.quantity };
var list = p.ToList();
s.Stop();
MessageBox.Show((s.ElapsedMilliseconds / 1000).ToString());
dataGridView1.DataSource = list;
LINQ Version
shootersEntities model = new shootersEntities();
var IsOnline = cBOnline.Checked;
var IsDeleted = cBDeleted.Checked;
Stopwatch s = new Stopwatch();
s.Start();
var p = from m in model.products
where ((m.productOnline == IsOnline) || (m.deleted == IsDeleted))
select new { m.sku, m.productCode, m.quantity };
var list = p.ToList();
s.Stop();
MessageBox.Show((s.ElapsedMilliseconds / 1000).ToString());
dataGridView1.DataSource = list;
Upvotes: 0
Views: 241
Reputation: 2048
You are filtering after your AsParallel. So you are reading all the rows spinning threads and filtering.
You can confirm this with a sql profile.
Upvotes: 0
Reputation: 34391
Probably because the LINQ version translates the query to SQL (which may or may not be run in parallell on the server) while the PLINQ version needs to retrieve everything from the database and then run filtering and sorting itself.
Upvotes: 3