Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8622873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:09:21+00:00 2026-06-12T07:09:21+00:00

Does the placement of the columns to join matter for performance when executing a

  • 0

Does the placement of the columns to join matter for performance when executing a LINQ statement?

For example, which of the following queries would run the quickest, and why?

A)

 var query = from o in entities.orders
                join i in entities.order_items
                on o.OrderId equals i.OrderId
                where o.AddedSalesOrder == 0
                select new
                       {
                           i.ShippingFirstName,
                           i.ShippingLastName,
                           i.Sku,
                           i.Quantity,
                           i.ItemPrice,
                           o.TotalShippingCost,
                           o.OrderId,
                           o.OrderCreateDate
                        };

B)

   var query = from o in entities.orders
                join i in entities.order_items
                on o.OrderId equals i.OrderId
                where o.AddedSalesOrder == 0
                select new
                       {                          
                           o.TotalShippingCost,
                           o.OrderId,
                           o.OrderCreateDate,
                           i.ShippingFirstName,
                           i.ShippingLastName,
                           i.Sku,
                           i.Quantity,
                           i.ItemPrice
                        };

C)

var query = from o in entities.orders
                join i in entities.order_items
                on o.OrderId equals i.OrderId
                where o.AddedSalesOrder == 0
                select new
                       {                        
                           o.OrderCreateDate,
                           i.ShippingFirstName,
                           i.ShippingLastName,
                           o.TotalShippingCost,
                           o.OrderId,                          
                           i.Sku,
                           i.Quantity,
                           i.ItemPrice
                        };

I am expecting query B to be the most efficient due to the placement of the columns for the join resulting in cleaner SQL code being generated but I may be wrong.

If it matters, the queries are being run on a SQL server 2008r2 database.

–Edit–

For what its worth, I ran a quick (and surely non-absolute) benchmark via C# to see how performance behaved on each scenario. My findings are below:

a) 297.61 millisecond avg over 100000 iterations
b) 245.90 millisecond avg over 100000 iterations
c) 304.16 millisecond avg over 100000 iterations

The code I used to test this is as follows:

var sw = new Stopwatch();
List<long> totalTime = new List<long>();
for (int u = 0; u < 100000; u++)
{
     sw.Start();
     var entities = new Entities();
     var query = from o in entities.orders
                 join i in entities.order_items
                 on o.OrderId equals i.OrderId
                 where o.AddedSalesOrder == 1
                 select new
                        {
                            i.ShippingFirstName,
                            i.ShippingLastName,
                            i.Sku,
                            i.Quantity,
                            i.ItemPrice,
                            o.TotalShippingCost,
                            o.OrderId,
                            o.OrderCreateDate
                        };
     var qc = query.Count();
     sw.Stop();
     totalTime.Add(sw.ElapsedMilliseconds);
     sw.Reset();
}
Console.WriteLine("Average time in Milliseconds: {0}", totalTime.Average());

It appears that the ordering of the joined columns may impact the speed of execution – or as was pointed out, my database may be inefficient 🙂

At any rate, I wanted to post the findings for any who find this interesting.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:09:23+00:00Added an answer on June 12, 2026 at 7:09 am

    In SQL, the order of joins and columns usually does not matter: provided you have a good SQL optimizer, and you have good statistics on your database, then the database engine will restructure your query for maximum performance.

    In general, that is not true for LINQ: unlike SQL, the statements are not reordered for execution, but instead lazily performed in the same order they are typed. If you’re grabbing spatially separated data, or picking a bad merge order, your execution speed will suffer.

    The good news is that you should be safe. For LINQ to SQL or LINQ to Entities, while the SQL generated will (usually) be in roughly the same order as you typed it, you’ll still be hitting the SQL database’s optimization engine. In this case, the order of joins and column names generally will not matter.

    As always, bad statistics or a poor database optimizer can still bite you. In this case, rather than ask on StackOverflow, your best bet is going to be to check what query plans are actually getting used by breaking out SQL Profiler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does the placement of a function have an effect on the performance of closures
Possible Duplicate: C++'s “placement new” in the below code what does Line 3 represents,
Does COUNT(*) have any significant impact for MySQL performance if query already has GROUP
Does the following psuedo code accomplish my goal of cleaning up after myself when
My intended program flow would look like the following if it were possible: typedef
I would like the following functionality: I have a datagrid, and when I go
I have an array for which the following test returns true: 1 ~~ @a
Does anyone know of how I can determine when the ContextMenu get its placement
Does anyone know if there is a way to generate different code in the
Does anyone have a translate function for x/y positions after rotation in javascript? for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.