10 Tips to Improve your LINQ to SQL Application Performance

Hey there, back again. In my first post about LINQ I tried to provide a brief(okay, bit detailed) introduction for those who want to get involved with LINQ to SQL. In that post I promised to write about a basic integration of WCF and LINQ to SQL working together, but this is not that post.

Since LINQ to SQL is a code generator and an ORM and it offers a lot of things, it is normal to be suspicious about performance of it. These are right up to a certain point as LINQ comes with its own penalties. But there are several benchmarks showing that DLINQ brings us up to %93 of the ADO.NET SQL DataReader performance if optimizations are done correctly.

Hence I summed up 10 important points for me that needs to be considered during tuning your LINQ to SQL’s data retrieval and data modifying process:

1 – Turn off ObjectTrackingEnabled Property of Data Context If Not Necessary

If you are trying only to retrieve data as read only, and not modifying anything, you don’t need object tracking. So turn it off using it like in the example below:

using (NorthwindDataContext context = new NorthwindDataContext())
{
context.ObjectTrackingEnabled = false;
}




This will allow you to turn off the unnecessary identity management of the objects – hence Data Context will not have to store them because it will be sure that there will be no change statements to generate.

2 – Do NOT Dump All Your DB Objects into One Single DataContext

DataContext represents a single unit of work, not all your database. If you have several database objects that are not connected, or they are not used at all (log tables, objects used by batch processes,etc..). These objects just unnecessarily consume space in the memory hence increasing the identity management and object tracking costs in CUD engine of the DataContext.

Instead think of separating your workspace into several DataContexts where each one represents a single unit of work associated with it. You can still configure them to use the same connection via its constructors to not to loose the benefit of connection pooling.

3 – Use CompiledQuery Wherever Needed

When creating and executing your query, there are several steps for generating the appropriate SQL from the expression, just to name some important of them:



  1. Create expression tree



  2. Convert it to SQL



  3. Run the query



  4. Retrieve the data



  5. Convert it to the objects


As you may notice, when you are using the same query over and over, hence first and second steps are just wasting time. This is where this tiny class in System.Data.Linq namespace achieves a lot. With CompiledQuery, you compile your query once and store it somewhere for later usage. This is achieved by static CompiledQuery.Compile method.

Below is a Code Snippet for an example usage:

Func<NorthwindDataContext, IEnumerable<Category>> func =
CompiledQuery.Compile<NorthwindDataContext, IEnumerable<Category>>
((NorthwindDataContext context) => context.Categories.
Where<Category>(cat => cat.Products.Count > 5));





And now, “func” is my compiled query. It will only be compiled once when it is first run. We can now store it in a static utility class as follows :

/// <summary>
/// Utility class to store compiled queries
/// </summary>
public static class QueriesUtility
{
/// <summary>
/// Gets the query that returns categories with more than five products.
/// </summary>
/// <value>The query containing categories with more than five products.</value>
public static Func<NorthwindDataContext, IEnumerable<Category>>
GetCategoriesWithMoreThanFiveProducts
{
get
{
Func<NorthwindDataContext, IEnumerable<Category>> func =
CompiledQuery.Compile<NorthwindDataContext, IEnumerable<Category>>
((NorthwindDataContext context) => context.Categories.
Where<Category>(cat => cat.Products.Count > 5));
return func;
}
}
}




And we can use this compiled query (since it is now a nothing but a strongly typed function for us) very easily as follows:

using (NorthwindDataContext context = new NorthwindDataContext())
{
QueriesUtility.GetCategoriesWithMoreThanFiveProducts(context);
}




Storing and using it in this way also reduces the cost of doing a virtual call that’s done each time you access the collection – actually it is decreased to 1 call. If you don’t call the query don’t worry about compilation too, since it will be compiled whenever the query is first executed.

4 – Filter Data Down to What You Need Using DataLoadOptions.AssociateWith

When we retrieve data with Load or LoadWith we are assuming that we want to retrieve all the associated data those are bound with the primary key (and object id). But in most cases we likely need additional filtering to this. Here is where DataLoadOptions.AssociateWith generic method comes very handy. This method takes the criteria to load the data as a parameter and applies it to the query – so you get only the data that you need.

The following code below associates and retrieves the categories only with continuing products:

using (NorthwindDataContext context = new NorthwindDataContext())
{
DataLoadOptions options = new DataLoadOptions();
options.AssociateWith<Category>(cat=> cat.Products.Where<Product>(prod => !prod.Discontinued));
context.LoadOptions = options;
}




5 – Turn Optimistic Concurrency Off Unless You Need It

LINQ to SQL comes with out of the box Optimistic Concurrency support with SQL timestamp columns which are mapped to Binary type. You can turn this feature on and off in both mapping file and attributes for the properties. If your application can afford running on “last update wins” basis, then doing an extra update check is just a waste.

UpdateCheck.Never is used to turn optimistic concurrency off in LINQ to SQL.

Here is an example of turning optimistic concurrency off implemented as attribute level mapping:

[Column(Storage=“_Description”, DbType=“NText”,
UpdateCheck=UpdateCheck.Never)]
public string Description
{
get
{
return this._Description;
}
set
{
if ((this._Description != value))
{
this.OnDescriptionChanging(value);
this.SendPropertyChanging();
this._Description = value;
this.SendPropertyChanged(“Description”);
this.OnDescriptionChanged();
}
}
}




6 – Constantly Monitor Queries Generated by the DataContext and Analyze the Data You Retrieve

As your query is generated on the fly, there is this possibility that you may not be aware of additional columns or extra data that is retrieved behind the scenes. Use Data Context’s Log property to be able to see what SQL are being run by the Data Context. An example is as follows:

using (NorthwindDataContext context = new NorthwindDataContext())
{
context.Log = Console.Out;
}





Using this snippet while debugging you can see the generated SQL statements in the Output Window in Visual Studio and spot performance leaks by analyzing them. Don’t forget to comment that line out for production systems as it may create a bit of an overhead. (Wouldn’t it be great if this was configurable in the config file?)

To see your DLINQ expressions in a SQL statement manner one can use SQL Query Visualizer which needs to be installed separately from Visual Studio 2008.

7 – Avoid Unnecessary Attaches to Tables in the Context


Since Object Tracking is a great mechanism, nothing comes for free. When you  Attach an object to your context, you mean that this object was disconnected for a while and now you now want to get it back in the game. DataContext then marks it as an object that potentially will change - and this is just fine when you really intent to do that.

But there might be some circumstances that aren’t very obvious, and may lead you to attach objects that arent changed. One of such cases is doing an AttachAll for collections and not checking if the object is changed or not. For a better performance, you should check that if you are attaching ONLY the objects in the collection those are changed.

I will provide a sample code for this soon.

8 – Be Careful of Entity Identity Management Overhead

During working with a non-read only context, the objects are still being tracked – so be aware that non intuitive scenarios this can cause while you proceed. Consider the following DLINQ code:

using (NorthwindDataContext context = new NorthwindDataContext())
{
var a = from c in context.Categories
select c;
}




Very plain, basic DLINQ isn’t it? That’s true; there doesn’t seem any bad thing in the above code. Now let’s see the code below:

using (NorthwindDataContext context = new NorthwindDataContext())
{
var a = from c in context.Categories
select new Category
{
CategoryID = c.CategoryID,
CategoryName = c.CategoryName,
Description = c.Description
};
}




The intuition is to expect that the second query will work slower than the first one, which is WRONG. It is actually much faster than the first one.

The reason for this is in the first query, for each row the objects need to be stored, since there is a possibility that you still can change them. But in the 2nd one, you are throwing that object away and creating a new one, which is more efficient.

9 – Retrieve Only the Number of Records You Need

When you are binding to a data grid, and doing paging – consider the easy to use methods that LINQ to SQL provides. These are mainly Take and Skip methods. The code snippet involves a method which retrieves enough products for a ListView with paging enabled:

/// <summary>
/// Gets the products page by page.
/// </summary>
/// <param name=”startingPageIndex”>Index of the starting page.</param>
/// <param name=”pageSize”>Size of the page.</param>
/// <returns>The list of products in the specified page</returns>
private IList<Product> GetProducts(int startingPageIndex, int pageSize)
{
using (NorthwindDataContext context = new NorthwindDataContext())
{
return context.Products
.Take<Product>(pageSize)
.Skip<Product>(startingPageIndex * pageSize)
.ToList<Product>();
}
}




10 – Don’t Misuse CompiledQuery

I can hear you saying “What? Are you kiddin’ me? How can such a class like this be misused?”

Well, as it applies to all optimization LINQ to SQL is no exception:

“Premature optimization is root all of evil” – Donald Knuth

If you are using CompiledQuery make sure that you are using it more than once as it is more costly than normal querying for the first time. But why?

That’s because the resulting function coming as a CompiledQuery is an object, having the SQL statement and the delegate to apply it. It is not compiled like the way regular expressions are compiled. And your delegate has the ability to replace the variables (or parameters) in the resulting query.

That’s the end folks, I hope you’ll enjoy these tips while programming with LINQ to SQL. Any comments or questions via sidarok at sidarok dot com or here to this post are welcome.

 

more : sidarok.com

 



Technorati Tags: ,
| Continue Reading..

Yahoo search to 'battle spyware'

Yahoo is introducing new technology to its search engine which will warn users if they are about to click on a website that hosts viruses, spyware and spam.

SearchScan uses security firm McAfee's SiteAdvisor technology to warn users about "potentially risky sites".

The service, which is switched on by default, produces an on-screen alert.

"Our goal is to protect users by allowing them to make a more informed decision about the sites they visit," said Yahoo's Priyank Garg.

Rival firm Google introduced similar technology in 2006.

Yahoo's service will warn users about three types of risk:

  • Browser exploits: Sites that can harm a user's computer or install malware simply by visiting the site. Any such sites or pages included in McAfee's data will be removed from search results automatically.
  • Dangerous downloads: SearchScan will display warnings next to search results for sites that offer potentially dangerous software, such as viruses, spyware or adware.
  • Unsolicited e-mail: SearchScan will alert users to scanned sites that send unsolicited e-mails or inappropriately share e-mail addresses with third parties.

Viruses, spyware and adware programs are often "hidden" inside innocuous-looking programs such as screensavers and toolbars.

Industry analysts IDC estimate that 67% of all computers have some form of spyware installed without a user's knowledge.

source : news.bbc.co.uk

 

Technorati Tags: ,,
| Continue Reading..

Ruby faces off against PHP, Java

Ruby experts compared the scripting languages of the rival PHP (Hypertext Preprocessor) language and Java during a panel session at the CommunityOne conference in San Francisco on Monday.

After an audience member asked why he should choose Ruby over PHP, panelists cited benefits such as test-driven development.

"There's a culture that's sprung up around Ruby," and the companion Ruby on Rails framework that is different from PHP, said Charles Nutter, a Sun engineer who has worked on the JRuby project. JRuby provides a version of Ruby to run on the Java Virtual Machine.

"You are pretty much ostracized from the Ruby world unless you’re doing test-driven development," and writing good tests, Nutter said.

With PHP, everyone has his or her own way of doing applications, he said. Testing and tried-and-true design patterns are not part of PHP culture, Nutter said.

PHP, said moderator Mark Driver, an analyst at Gartner, is "ridiculously easy to learn." PHP and ColdFusion enable developers to do an "amazing" amount of work, he said.

"But you do tend to hit the limit when your architectural complexity goes beyond a certain space," Driver said.

The panel also pondered whether Ruby could compete with the Java language. Driver said developers he has spoken with have used words like "elegant" and "beautiful" to describe Ruby. These, Driver said, were "things I hadn't heard in the Java world, quite frankly."

Panelist Thomas Enebo, a JRuby core developer at Sun, said Ruby was created to be fun. "If something is fun to you, it is going to end up being productive as a byproduct," Enebo said.

Java, said panelist David Koontz, president of Happy Camper Studios, is a higher level system language whereas Ruby is an application language. A system language is faster when an application needs to be fast, he said.

Unlike Java Ruby does not have a formal design body for the language, an audience member said. Java is overseen by the Java Community Process (JCP). He wondered if Ruby would need a formal standardization process.

Ruby has lured people who want to create their own language features, said panelist Sarah Mei, an independent developer. But Ruby has been attracting more people who want a formal process and that is probably the way it will go, Mei said.

Nutter cited a project that spawned out of the Rubinius virtual machine effort that would create specification tests for Ruby. "That's really probably going to start helping us define what Ruby is today," and help determine what happens tomorrow, he said.

Also, Ruby implementers have gotten together to determine features planned for Ruby via the IRC effort, Nutter said.

Asked about efforts to run Ruby on multi-core systems, Nutter said JRuby is native-threaded, supporting multiple threads. Efforts are afoot for the standard implementation of Ruby to support this as well, he said. Fine-grained locks will be needed around C structures in Ruby, he said.

Panelelists also cited NetBeans and CodeGear's 3rdRail as useful tools for Ruby and Ruby on Rails.

Paul Krill is editor at large at InfoWorld.

source : infoworld.com

Technorati Tags: ,,
| Continue Reading..

Windows PowerShell V2 CTP2

The second Community Technology Preview of Windows PowerShell V2 is now available for download. Designed as the successor of Windows PowerShell 1.0, Windows PowerShell V2 CTP2 also marks a consistent evolution in comparison with the initial Community Technology Preview release.

As such, it delivers new features, along with extended usability. In this manner, Microsoft offers a more mature command line shell and scripting language that enables an increased level of control over Windows environments, leading to a jump in productivity. The CTP will integrate seamlessly with Windows Server 2003 SP2; Windows Server 2008; Windows Vista SP1; Windows XP SP2.

"This release adds a plethora of new features. PowerShell remoting now allows a one-to-one interactive experience. Thought about partitioning and organizing PowerShell scripts? Use modules to create self-contained and reusable units. This release introduces transactions support in PowerShell engine and APIs along with an update to the Registry provider to support them. We introduced eventing support in Powershell engine for listening, forwarding and acting on management and system events. Support for multiple parameter sets in script cmdlets bring them to par with C# cmdlets," said Hemant Mahawar, Program Manager Windows PowerShell.

But at the same time, Microsoft informed that Windows PowerShell V2 CTP2 is not meant for integration into productivity environments. The Community Technology Preview label means that Windows PowerShell V2 is not even in Beta stage, but just a pre-release build. V2 CTP2 does provide support for 1.0 cmdlets and scripts, but this release of PowerShell has not been tested to the same extent of the final version and is still far from reaching the finish line.

"For the adventurous folks.... application developers can host PowerShell in IIS to support multiple remote PowerShell sessions in a single process. These are just a few of the new features we have packaged in this CTP2 release. Additionally this CTP2 includes some simple updates... like new parameters to several existing cmdlets," Mahawar added.


Download:
Windows PowerShell V2 CTP2 XP SP2 x86
Windows PowerShell V2 CTP2 Vista x86
Windows PowerShell V2 CTP2 2K3 SP1 x86
Windows PowerShell V2 CTP2 XP SP2 x64
Windows PowerShell V2 CTP2 Vista x64

Technorati Tags: ,

| Continue Reading..

Mac OS 10.5.2 = Vista made in Apple?

I’ve heard many different complaints about Mac OS 10.5.2 since it was released, from my own Skype/podcasting problems to issues with just about everything else, including: printing, third-party wireless routers, software freezing and problems with Time Machine.

The Apple discussion boards are littered with stories about all kinds of crawlers in 10.5.2, and while hardly an epidemic, they’re not exactly isolated incidents either.

Chuck Freedman from the PowerPage Podcast shares his problems with 10.5.2 on his MacBook Pro – he thinks it’s the buggiest software update in years. So much so, that he coined a new term for 10.5.2 – “Apple Vista.” The picture below isn’t a screen grab of the latest maze game, it’s Chuck’s MBP screen after a spectacular 10.5.2 crash.

Read more about Chuck’s tale of woe and check out six more gallery images after the jump.
Late in 2007 Apple began releasing its new operating system, Mac OS 10.5 (a.k.a. Leopard). With new hardware announcements in early January 2008 Apple began shipping this new OS with all new computers they sold, and wow, what an OS. There were tons of new features to knock your socks off. From then on, all Macs purchased came with Leopard pre-installed – life was supposed to be good.

During the early days of 10.5 Apple found a few bugs and tweeks and released an update to Leopard called 10.5.1, and again, all was well. I continued to operate my MacBook Pro running this new OS with near flawless behavior. I could go days (sometimes weeks) without rebooting my machine. I could run a dozen applications simultaneously and all was good.

In February Apple began shipping their Time Capsule backup appliance which allowed me to have a wireless network and shared network attached storage – something I’d been waiting for. Time Capsule allowed Apple’s new Time Machine software to automatically back up my data to a shared network drive and restore it easily. A great concept, but it still needs some bugs worked out (that’s a topic for another article.)

In order to get my machine to see, utilize and understand the Time Capsule, I had to upgrade my Mac to 10.5.2. Without hesitation, I did. From that day forward I’ve been running the worst operating system I’ve run on a Mac in years. My machine now runs slower and if I run more than a few applications at a time, it barely operates. It takes nearly six minutes to wake from sleep, and when it does finally wake, the mouse bounces around uncontrollably and requires a hard shut down to fix.

Again, time on my side, Apple released a keyboard firmware update to fix some issues. I applied the update, but didn’t see any fixes. Shortly thereafter Apple released EFI firmware update 1.5 - well things can’t be any worse (so I thought), so I applied it. Boy was I wrong. See the screen shots. Only days later Apple released EFI Firmware 1.5.1 which fixed more, but at this point my machine was dead. I’ve sent it in to Apple repair to get it fixed, and I’m waiting for its return.

If I wanted to run on a buggy crappy operating system, I’d pickup a PC running Microsoft Vista. I don’t expect this from my Mac. Mac OS 10.4.11 (Tiger) ran great whereas Apple Vista just appears to be immature and needed more time to cook.

Also, I know that Apple wants to push sales of Leopard but I don’t like being forced to upgrade because of some new piece of hardware is released. Rather than force 10.5.2 down my throat because I spent more money on a Time Capsule, why not just give me a driver or application to install. The OS should be hardware independent.

In the meantime, my Apple Vista machine is in for repair, and I’m writing this on my trusty Tiger machine.

source: blogs.zdnet.com

 

Technorati Tags: ,
| Continue Reading..

Yahoo Stock Plunges After Microsoft Backs Off

After Microsoft withdrew its $33-a-share offer for Yahoo on Saturday, the question was how far Yahoo’s stock would drop. On Monday morning, the markets gave an initial answer: plenty.

Shortly after trading opened, Yahoo’s shares were off about 20 percent, at $23, down from the close of $28.67 on Friday — a day when the companies were said to be escalating their talks on a merger.

Microsoft’s own shares edged up slightly, to $30, from their Friday close of $29.24.

How long Yahoo’s stock will stay down will largely depend on the company’s next moves, which could include a partnership with its chief competitor, Google.

People close to Yahoo said that the chief executive, Jerry Yang, and his team, who told Microsoft they would not sell for less than $37 a share, greeted Microsoft’s decision as a victory. High-fives were exchanged Saturday afternoon when they learned Microsoft was backing down.

Yet some Yahoo shareholders, large and small, have indicated that they favored a deal at around $34 to $35 a share. Even those who were holding out for a higher price said a merger with Microsoft made strategic sense.

“I don’t believe that Jerry Yang as a founder, as someone who is emotionally attached to the company, was really looking out for my interest as a shareholder,” said Darren Chervitz, co-manager of the Jacob Internet Fund, which owns about 150,000 shares of Yahoo. “I don’t think anything Yahoo puts out there is going to be comparable with what Microsoft was offering.”

The entire board backed Mr. Yang’s desire to reject Microsoft’s offer, said a person involved in the negotiations who was not authorized to speak publicly about the matter. But unhappiness with Mr. Yang could spread through the company’s ranks.

“If the stock drops as far as I think it will, a lot of employees are going to be angry and many key employees could leave,” said a Yahoo executive, who asked to remain anonymous to avoid upsetting his superiors.

Yahoo defended its decision, saying it had always thought Microsoft’s offer, worth about $47.5 billion, undervalued the company. Officially, the company has shed little light on what it might do next. “We remain focused on maximizing shareholder value and pursuing strategic opportunities that position Yahoo for success and leadership in its markets,” Roy Bostock, Yahoo’s chairman, said in a statement issued late Saturday.

But people close to the company suggested that Yahoo’s most likely lifeline could come from an unlikely source: Google.

The two companies recently conducted a two-week test in which Google delivered ads on a small portion of Yahoo searches. The test, which both companies described as successful, was intended to show how much more Yahoo could earn by outsourcing some of its search ads to Google, whose technology and large base of advertisers allow it to extract more revenue on average for every search.

Now Google and Yahoo will have to decide whether to move from test to broader partnership. Talks around a deal were active Friday, even as Yahoo and Microsoft were engaged in a last-ditch effort to come to an agreement, said a person familiar with the discussions.

For Yahoo, the idea of letting Google run some of its search ads is not new. Some shareholders and even some Yahoo executives have long favored it. By the company’s own reckoning, Google earns about 60 percent to 70 percent more on average for every search than Yahoo.

But before Microsoft made its offer, Mr. Yang and his team had repeatedly rejected the idea, saying search advertising was an essential part of the company’s long-term strategy. Instead, the company spent millions in improving its own search advertising system, called Panama, telling investors it was the right choice.

In a letter sent Saturday to Mr. Yang, Steven A. Ballmer, Microsoft’s chief executive, used precisely those arguments to emphasize why a search advertising partnership with Google was a bad idea. He also noted that it was one reason Microsoft decided to walk away from its offer, rather than initiate a proxy fight.

A partnership with Google “would fundamentally undermine Yahoo’s own strategy and long-term viability by encouraging advertisers to use Google as opposed to your Panama paid search system,” Mr. Ballmer said. “This would also fragment your search advertising and display advertising strategies and the ecosystem surrounding them.”

more : nytimes.com

Technorati Tags: ,
| Continue Reading..

Download Ashampoo Burning Studio 2008 Full Version + Serial Key

Ashampoo Burning Studio 2008 is express version of full featured Ashampoo Burning Studio 7. Ashampoo Burning Studio 2008 has version number 7.01, and lacks the features of burn movie and video, including Video DVD, Video CD (VCD) or Super Video CD (S-VCD) and newly added Video DVD, and ability to backup and restore files and folders, including support for multi-disc file backup and restore on CD/DVD/Blu-ray with compression and password protection.

However, Ashampoo Burning Studio 2008 is still a powerful DVD and Blu-Ray authoring and burning software that features ability to burn files and folders to CD/DVD/Blu-ray and add files and folders to existing discs, rip music from audio CDs to MP3, WMA and WAV files, create audio CDs from WAV, MP3, FLAC, WMA and Ogg Vorbis files, create MP3 or WMA CDs, DVDs and Blu-ray discs or just copy music to CD, DVD or Blu-ray, copy CDs, DVDs and Blu-ray discs, create and burn disc images for CD, DVD and Blu-ray, erase any rewritable media, create bootable discs from boot images, and save project files. Ashampoo Burning Studio 2008 can support storing of disc images larger than 2GB on FAT32 file systems, have interactive troubleshooting assistant for solving hardware and media problems, has integrated support for over 1,700 CD, DVD and Blu-ray burner, can add additional files while copying discs, make modified copies of bootable discs.

Computeractive is giving away Ashampoo Burning Studio 2008 free full license key which enables unlimited and no time limit use. Ashampoo Burning Studio 2008 is a special edition that exclusive to Computeractive magazine reader, and cannot be downloaded from Ashampoo website. So, users have to use the direct download links from VNU.

Download ashampoo_burningstudio2008_vnu_gb.exe

Once installed, on first use dialog, the program will ask user for a few options which one is whether want to obtain the free full license key to Ashampoo Burning Studio 2008. Click on the button to launch your web browser to open web page that allows you to register for a free full version license key or registration key. Within program, the option can be accessed from “Internet” drop down menu, or simply browse to this Ashampoo registration page. Free license serial number registration is two-step process. Enter email and submit, and then click on the confirmation link in the email to receive the free serial code.

Technorati Tags: ,
| Continue Reading..

Enter your email address:

Delivered by FeedBurner

Followers