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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:59:22+00:00 2026-05-11T01:59:22+00:00

I understand the best way to count the number of rows in an SQL

  • 0

I understand the best way to count the number of rows in an SQL table is count(*) (or equivalently count(PrimaryKey)).

  1. Is this O(1)?
  2. If not, why not?

Why not just implement a counter and return it for this specific query? Is it because this query is not a common use case?

If the answers vary according to SQL engine, I’d like to hear the differences – but in any case, I’m interested in the actual implementation in production SQL engines.

  • 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. 2026-05-11T01:59:22+00:00Added an answer on May 11, 2026 at 1:59 am

    In some RDBM’s this is O(1) (most notably MySQL), put AFAIK it is generally frowned upon and considered an ‘ugly performance hack’. The reasons is that if you have transactions (which every real RDBM should have), the total number of rows in the table might or might not be equal to the total number you can see from the current transaction. This is why the server needs to check which rows are actually visible to your transaction, making it more O(n) than O(1).

    If you want to optimize the process of getting the number of rows and are satisfied with an approximate result, most RDBM’s have special ‘info’ tables which hold information about the tables, including the approximate number of rows (again, it is not the exact number of rows because of the transactions).

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

Sidebar

Related Questions

I'm trying to understand the best way to implement Python's ConfigParser so that elements
I understand that the best way to develop a free and paid version of
I would like to understand whats the best way to handle exceptions in Multicast
From what I understand, the best way to deal with dates in the Zend
I've been trying to understate the best way to write an MS SQL 2005
From what I have understood, it is not the best way to open a
Been trying my best to understand this correctly. What is the difference between an
Im wondering if someone can help me understand the best way for me to
I'm trying to understand the best way to handle SOA on heroku, i've got
I'm trying to understand the best way to map the fields in an Edit

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.