CHunky Universe of Vigourous Astonishing SHarepoint :)

Set ObjectTrackingEnabled = false when reading

In LINQ 2 SP we work with a data context. By default a data context has the property ObjectTrackingEnabled = true. This property is crucial for all other operations in CRUD except Read. I performed a mini experiment. I created 20 000 items in my task list. Every seventh item contains “pärla”. Allright, here is what I found out:

2857 / 20 000 items
ObjectTrackingEnabled = true 07s.405ms
ObjectTrackingEnabled = false 01s.232ms

Sharepoint as a developer platformWith ObjectTrackingEnabled it takes 6 times more time to read the list items: whole 7,5 seconds. It would be a catastrophy if you would run this code to retrieve some items in your webpart which runs synchronously in the main thread.

The best SharePoint book from a developer perspective (SharePoint 2010 as a developer platform) explains ObjectTrackingEnabled:

Track Changes
The LINQ to SharePoint provider checks changes made in the database against its current state. This is
the default behavior. If you access the lists in a read-only manner, the tracking can be suppressed to
optimize performance:
ctx.ObjectTrackingEnabled = false;

Here is the code which I ran to get the results:

Console.WriteLine("Oppgaver, 20 000 items");
var sw = new Stopwatch();

Console.WriteLine("Retrieving items containing pärla");
Console.WriteLine("**** ObjectTrackingEnabled=true (default) ****");

using (var ctx = new OppgaverDataContext("http://takana/"))
	var tasks = ctx.Oppgaver.Where(t=> t.Title.Contains("pärla")).ToList();
	Console.WriteLine("\tcount: " + tasks.Count());


var elapsed = String.Format("{0:00}s:{1:000}ms", sw.Elapsed.Seconds, sw.Elapsed.Milliseconds);
Console.WriteLine("\ttime:\t" + elapsed);

sw = new Stopwatch();
Console.WriteLine("**** ObjectTrackingEnabled=false ****");

using (var ctx = new OppgaverDataContext("http://takana/") { ObjectTrackingEnabled = false })
	var tasks = ctx.Oppgaver.Where(t => t.Title.Contains("pärla")).ToList();
	Console.WriteLine("\tcount: " + tasks.Count());

elapsed = String.Format("{0:00}s:{1:000}ms", sw.Elapsed.Seconds, sw.Elapsed.Milliseconds);
Console.WriteLine("\ttime:\t" + elapsed);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Daniel Chronlund Cloud Tech Blog

News, tips and thoughts for Microsoft cloud fans

Вула Чăвашла

VulaCV - Чăвашла вулаттаракан сайт

Discovering SharePoint

And going crazy doing it

Bram de Jager - Architect, Speaker, Author

Microsoft 365, SharePoint and Azure

SharePoint Dragons

Nikander & Margriet on SharePoint

Mai Omar Desouki

PFE @ Microsoft

Cameron Dwyer

Office 365, SharePoint, Azure, OnePlace Solutions & Life's Other Little Wonders


Me and My doings!

Share SharePoint Points !

By Mohit Vashishtha

Jimmy Janlén "Den Scrummande Konsulten"

Erfarenheter, synpunkter och raljerande om Scrum från Jimmy Janlén

Aryan Nava

DevOps, Cloud and Blockchain Consultant


SharePoint for everyone


Ryan Dennis is a SharePoint Solution Architect with a passion for SharePoint and PowerShell

SharePoint 2020

The Vision for a Future of Clarity

Aharoni in Unicode

Treacle tarts for great justice

... And All That JS

JavaScript, Web Apps and SharePoint


Mostly what I know and share about...

%d bloggers like this: