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 6553625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:36:15+00:00 2026-05-25T12:36:15+00:00

Consider this SQL performance difference, where in the first, I select all 26,000 rows,

  • 0

Consider this SQL performance difference, where in the first, I select all 26,000 rows, and in the second, I just want the first 5.

SELECT tw.*
FROM entity e
JOIN entity_tag et on et.entity_id = e.id
JOIN tag t on t.tag_id = et.tag_id
JOIN tagrelatedtweets trt on trt.FK_Tag_ID = t.tag_id
JOIN tweets tw on tw.PK_Tweet_ID = trt.FK_Tweet_ID
WHERE e.id = 765131
ORDER BY tw.[timestamp]

vs

SELECT TOP (5) tw.*
FROM entity e
JOIN entity_tag et on et.entity_id = e.id
JOIN tag t on t.tag_id = et.tag_id
JOIN tagrelatedtweets trt on trt.FK_Tag_ID = t.tag_id
JOIN tweets tw on tw.PK_Tweet_ID = trt.FK_Tweet_ID
WHERE e.id = 765131
ORDER BY tw.[timestamp]

Without: CPU =    201 | Reads:    6880 | Writes:    0 | Duration:   451
With:    CPU = 302439 | Reads: 7453199 | Writes: 3169 | Duration: 74188

This just doesn’t make sense to me… Is there another way to go about this?

After Martin’s suggestion of a REBUILD STATISTICS on all tables involved, there is a small improvement, but the trick with changing the TOP amount into a parameter works the best.

Before statistics rebuild:

CPU = 302439 | Reads: 7453199 | Writes: 3169 | Duration: 74188 

After statistics rebuild:

CPU = 127734 | Reads: 4100436 | Writes: 2656 | Duration: 16880 

With parameter:

CPU =    218 | Reads:    6899 | Writes:    0 | Duration:    83 

Query with parameter:

DECLARE @TOP INT; SET @TOP=5; 
SELECT TOP (@TOP) tw.*
FROM entity e
JOIN entity_tag et on et.entity_id = e.id
JOIN tag t on t.tag_id = et.tag_id
JOIN tagrelatedtweets trt on trt.FK_Tag_ID = t.tag_id
JOIN tweets tw on tw.PK_Tweet_ID = trt.FK_Tweet_ID
WHERE e.id = 765131
ORDER BY tw.timestamp desc

One last remark for those of you using Entity Framework; if you experience this behavior, you can simulate the same parameter-based behavior as follows:

.Take(100).ToList().Take(5)

I know it isn’t pretty, but it’s the only way to trigger the correct execution plan if you are using entity framework as far as I can tell.

Thanks a lot for pointing me in the correct direction Martin!

  • 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-05-25T12:36:15+00:00Added an answer on May 25, 2026 at 12:36 pm

    Following discussion in the comments it seems that for some reason the plan SQL Server is choosing to optimize for the “First 5 Rows Only” case is sub optimal. It probably will be using non blocking operators such as nested loops joins rather than blocking ones such as hash joins.

    As you are unable to post the execution plans it is difficult to know exactly the reason why but comparing the plans for both versions of the query and looking at actual vs estimated number of rows in the actual execution plan for the problem query should set some light on the matter.

    It seems in this case that hiding the TOP 5 information from the optimiser at compile time is sufficient to give you the plan you want however!

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

Sidebar

Related Questions

Consider this T-SQL: CREATE USER my_test_user WITHOUT LOGIN; SELECT USER_NAME(), USER, SUSER_SNAME(),SYSTEM_USER, suser_name(); EXECUTE
Consider this typical disconnected scenario: load a Customer object from SQL Server using LINQ
Consider this query: SELECT F1,F2 FROM TABLE GROUP BY F1 Selecting F1 is valid,
Consider this query: WITH Scores (score) AS ( SELECT CAST(score AS INTEGER) FROM (
consider this sql CREATE VIEW [dbo].[MyView1] ([ID],[VisitDate],[StartDate] ,[EndDate],[MyCount]) WITH SCHEMABINDING AS SELECT id, VisitDate,dateadd(dd,-10,VisitDate),dateadd(dd,10,VisitDate),
Consider this table on SQL Server wordID aliasID value =========================== 0 0 'cat' 1
Consider this example table (assuming SQL Server 2005): create table product_bill_of_materials ( parent_product_id int
Please consider this sql statements Create table abc (A int, B int ) insert
Consider this table (from http://www.tizag.com/mysqlTutorial/mysqlmax.php ): Id name type price 123451 Park's Great Hits
Consider this LINQ To SQL query. It's intention is to take a string[] of

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.