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

Ask A Question

Stats

  • Questions 120k
  • Answers 120k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can treat the bit as a string ('0', '1')… May 12, 2026 at 12:17 am
  • Editorial Team
    Editorial Team added an answer use the tutorials at w3schools.com. They indicate how to work… May 12, 2026 at 12:17 am
  • Editorial Team
    Editorial Team added an answer You cannot set the Format property using SQL but you… May 12, 2026 at 12:17 am

Related Questions

I have a table in the database that I'm retrieving using LINQ to SQL,
I thought I would rewrite this question (same iteration). The original was how to
I created a .MDF database in my WPF application. I then generated LINQ-to-SQL classes
I am trying to start a new MVC project with tests and I thought

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.