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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:34:10+00:00 2026-06-10T05:34:10+00:00

We assume that there is no primary key defined for a table T. In

  • 0

We assume that there is no primary key defined for a table T. In that case, how does one count all the rows in T quickly/efficiently for these databases – Oracle 11g, MySql, Mssql ?

It seems that count(*) and count(column_name) can be slow and inaccurate respectively. The following seems to be the fastest and most reliable way to do it-

select count(rowid) from MySchema.TableInMySchema;

Can you tell me if the above statement also has any shortcomings ? If it is good, then do we have similar statements for mysql and mssql ?

Thanks in advance.

Source –
http://www.thewellroundedgeek.com/2007/09/most-people-use-oracle-count-function.html

  • 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-10T05:34:12+00:00Added an answer on June 10, 2026 at 5:34 am

    count(column_name) is not inaccurate, it’s simply something completely different than count(*).

    The SQL standard defines count(column_name) as equivalent to count(*) where column_name IS NOT NULL. To the result is bound to be different if column_name is nullable.

    In Oracle (and possibly other DBMS as well), count(*) will use an available index on a not null column to count the rows (e.g. PK index). So it will be just as fas

    Additionally there is nothing similar to the rowid in SQL Server or MySQL (in PostgreSQL it would be ctid).

    Do use count(*). It’s the best option to get the row count. Let the DBMS do any optimization in the background if adequate indexes are available.

    Edit

    A quick demo on how Oracle automatically uses an index if available and how that reduces the amount of work done by the database:

    The setup of the test table:

    create table foo (id integer not null, c1 varchar(2000), c2 varchar(2000));
    insert into foo (id, c1, c2)
    select lvl, c1, c1 from 
    (
      select level as lvl, dbms_random.string('A', 2000) as c1
      from dual 
      connect by level < 10000
    );
    

    That generates 10000 rows with each row filling up some space in order to make sure the table has a realistic size.

    Now in SQL*Plus I run the following:

    SQL> set autotrace traceonly explain statistics;
    SQL> select count(*) from foo;
    
    
    Execution Plan
    ----------------------------------------------------------
    Plan hash value: 1342139204
    
    -------------------------------------------------------------------
    | Id  | Operation          | Name | Rows  | Cost (%CPU)| Time     |
    -------------------------------------------------------------------
    |   0 | SELECT STATEMENT   |      |     1 |  2740   (1)| 00:00:33 |
    |   1 |  SORT AGGREGATE    |      |     1 |            |          |
    |   2 |   TABLE ACCESS FULL| FOO  |  9999 |  2740   (1)| 00:00:33 |
    -------------------------------------------------------------------
    
    
    Statistics
    ----------------------------------------------------------
            181  recursive calls
              0  db block gets
          10130  consistent gets
              0  physical reads
              0  redo size
            430  bytes sent via SQL*Net to client
            420  bytes received via SQL*Net from client
              2  SQL*Net roundtrips to/from client
              5  sorts (memory)
              0  sorts (disk)
              1  rows processed
    
    SQL>
    

    As you can see a full table scan is done on the table which requires 10130 “IO Operations” (I know that that is not the right term, but for the sake of the demo it should be a good enough explanation for someone never seen this before)

    Now I create an index on that column and run the count(*) again:

    SQL> create index i1 on foo (id);
    
    Index created.
    
    SQL> select count(*) from foo;
    
    
    Execution Plan
    ----------------------------------------------------------
    Plan hash value: 129980005
    
    ----------------------------------------------------------------------
    | Id  | Operation             | Name | Rows  | Cost (%CPU)| Time     |
    ----------------------------------------------------------------------
    |   0 | SELECT STATEMENT      |      |     1 |     7   (0)| 00:00:01 |
    |   1 |  SORT AGGREGATE       |      |     1 |            |          |
    |   2 |   INDEX FAST FULL SCAN| I1   |  9999 |     7   (0)| 00:00:01 |
    ----------------------------------------------------------------------
    
    
    Statistics
    ----------------------------------------------------------
              1  recursive calls
              0  db block gets
             27  consistent gets
             21  physical reads
              0  redo size
            430  bytes sent via SQL*Net to client
            420  bytes received via SQL*Net from client
              2  SQL*Net roundtrips to/from client
              0  sorts (memory)
              0  sorts (disk)
              1  rows processed
    
    SQL>
    

    As you can see Oracle did use the index on the (not null!) column and the amount of IO went drastically down (from 10130 to 27 – not something I’d call “grossly ineffecient“).

    The “physical reads” stem from the fact that the index was just created and was not yet in the cache.

    I would expect other DBMS to apply the same optimizations.

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

Sidebar

Related Questions

Assume that I have a table user_count defined as follows: id primary key, auto
Assume that category_id is an index key (not primary key) of table books .
i have questions about choosing a table's primary key. assume that we have 3
We all know that most applications out there assume class names to follow the
Let's assume there is a SQL Server 2008 table like below, that holds 10
Assume the following in MySQL: CREATE TABLE users ( id integer auto_increment primary key,
Assume that there's no problem with my header file and some included library. I'm
The idea is that there is a primary implementation of COM interface, which needs
Say I have a table with a primary key a_id and foreign key b_id
I've noticed that SO and other sites use the auto-incrementing primary key of the

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.