Thank you very much,
if I have SAL field (number), I get SAL total as 'SELECT SUM(SAL) TOTAL from...', however this SQL command return the result very fast but 'SELECT count(*) from...' return the result very slow?
Main Topics
Browse All TopicsHi,
I want to get RecordCount from a table. If I use 'Select count(*) from ...', it's very slowly (my table has about some millions records).
Do you know how to get it faster?
Thanks in advance,
VinhDH
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I disagree with Urim's first answer. There are at least two ways to get faster counts in many cases.
The first way is by forcing Oracle to count index entries rather than a full table scan. You need to add a where clause referencing a not null, indexed column.
For example, in a customer table, if you have a numeric "cust_no" column as you primary key, use this statement:
select count(cust_no) "Qty"
from customer
where cust_no > 0;
The second way will only get you an approximate count and only if you analyze your tables regulary, but it will be very fast (especially if you add a where clause for a particular table). Try this:
select table_name, num_rows
from user_tables;
markgeer, the answer I was answering on was to get the number of records in a tables faster than count(*).
If count(*) is slow, with the best explain plan how can you bring me the number of records from the table with better response time ? (you need to run your query with different explain plan, and if you can improve the explain plan you can improve the count(*).)
The second way you suggest is good if there are statsitics on the table, is not the select you put on a prod environment.
Select count(*) usually does not give the best possible explain plan, it usually results in a full-table scan. A count of indexed values can result in only an index scan, usually a much smaller number of blocks.
I see no reason for not analyzing tables regularly (at least weekly if not daily) in most production environments, especially if the larger tables are just estimated. If those statistics are available, then I don't know why this kind of select would not be used in a production environment.
markgeer,
for your opinon if there is unique index on COL1 then select count(*) will do full table scan, while select count(COL1) won't ?
For quering the dictonary to the number of rows, not always it a good solution, for example, I want to know how much records are now! and not yesterday or week before.
Business Accounts
Answer for Membership
by: urimPosted on 1999-08-26 at 02:22:08ID: 1088743
NO you can't.