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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:55:46+00:00 2026-05-11T18:55:46+00:00

I thought LINQ to SQL was tuned for performance? The following LINQ to SQL

  • 0

I thought LINQ to SQL was tuned for performance?

The following LINQ to SQL for counting is very poor

Dim uniqueFactors As Integer = db.LargeTable.Distinct.Count

produces:

SELECT COUNT(*) AS [value]
FROM [dbo].[LargeTable] AS [t0]
WHERE ([t0].[ID] % @p0) = @p1

How every the fastest way to count the number of records based on a primary key is

SELECT  @totalRowCount = rows
FROM    sysindexes
WHERE   id = OBJECT_ID('LargeTable')
    AND indid < 2

So the question, how can I ensure that LINQ to SQL performs a count quickly when asking for Count(*)?

  • 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-11T18:55:47+00:00Added an answer on May 11, 2026 at 6:55 pm

    Well, it depends on what you expect. If you ask for the “Distinct.Count” on a table, you’re instructing LINQ to do exactly what it does. Since that will result in a table scan, it will be slow for larger tables.

    The SELECT COUNT(*) is the only way SQL Server (and therefore LINQ) can give you an exact, up-to-date count of the rows in the table.

    Selecting a rows from sysindexes (or preferably: sys.partitions in SQL Server 2005 and up – the “sysindexes” views are being deprecated) will give you a approximate number – but that’s not guaranteed to be absolutely correct and up to date.

    So basically, what LINQ is missing, is a “UseApproximationForPerformancesSake” switch. That might be helpful at times – but then again, you can always use that little chunk of SQL and query the database yourself, if you need a speedy and only approximate response.

    Marc

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

Sidebar

Related Questions

I want to write a Linq to Sql query that does a count and
When Executing the following linq to sql statement: var stuff = from l in
After too much thought, I've decided to use Linq To SQL as a DAL
I'd like the community's take on some thoughts I've had about Linq to Sql
I have written what I thought to be a pretty solid Linq statement but
This is probably pushing the boundaries of Linq-to-Sql a bit but given how versatile
I didn't use LINQ to SQL in project, I just went through a hand-on
I'm new to linq-to-sql (and sql for that matter), and I've started to gather
Are there more extension points available in linq-to-sql than creating your own partial DataContext
Being new to Linq-to-SQL, I ran into one of it's Gotchas today and I

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.