Solved

How to improve the select query performance for to find the count in a table in oracle

Posted on 2006-06-22
9
1,195 Views
Last Modified: 2008-01-09
i have a huge table which consists nearly 2 million recors in oracle.
when i fire a query for to find the number for record .. it is taking very long time.

Query is : select count(*) from schema.tablename;

what should i do to improve the Performance.


Database : oracle 9.X , windows Machine.

0
Comment
Question by:ayadav1186
  • 4
  • 4
9 Comments
 
LVL 3

Accepted Solution

by:
JacekMycha earned 200 total points
Comment Utility
Buid a bitmap index on low cardinality colum (few distinct values). Bitmap indexes have entries for null values as well, so you can pick any column. Few distinct values will keep the index small. For example, a 'GENDER' column with 3 values: M, F and NULL would be perfect.
You can also build a b-tree index on a NOT NULL  colum.
Analyze table, analyze index... You can also build index using CREATE INDEX statement with COMPUTE STATISTICS clause.

You can lookup data dictionary (SELECT TABLE_NAME, NUM_ROWS FROM ALL_TABLES ...)  to see number of rows when the table was last time analyzed. This is enough accurate for space estimates, but not good for application code.

JacekMycha
0
 
LVL 19

Expert Comment

by:actonwang
Comment Utility
>>select count(*) from schema.tablename;

(1) do you have index on this table? if you have index, the query will likely to access only index to get better performance instead of full table scan on table data.

(2) This query usually will use FTS ( full table scan). FTS will scan till HWM. In order to let FTS scan less blocks, you could do is to
      1.  reorgnize your table to lower blocks used thus HWM.
      2. You could use "parallel query" to improve the query performance.


acton      
     
       
0
 
LVL 34

Expert Comment

by:Mark Geerlings
Comment Utility
Why do you want to do a "select count(*) from schema.tablename" on a large table?  Do you need an exact count that is up-to-date?  Or, could you analyze the table at regular intervals (like at night or on the weekend) and just do:
select num_rows from all_tables where owner = '[schema]' and table_name = '[table_name]'

or:
select table_name, num_rows from all_tables
where owner = '[schema]'
0
 
LVL 19

Expert Comment

by:actonwang
Comment Utility
to markgeer:

num_rows  is popluated only after you do statistics analysis. Also,  It might not be up to date.

Therefore I don't think you can use it to substitue for  "select count(*) ..."
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 34

Expert Comment

by:Mark Geerlings
Comment Utility
To actonwang:

Did you notice that I only mentioned using num_rows after saying: "...analyze the table at regular intervals...".
0
 
LVL 19

Expert Comment

by:actonwang
Comment Utility
to markegeer,

     >>it might not be up to date.

     did you see that?
0
 
LVL 34

Expert Comment

by:Mark Geerlings
Comment Utility
Right, the value for "num_rows" will only be as up-to-date as the last time that the table was analyzed.

(Also, I had asked the questioner if he/she needs an up-to-date count and we are still waiting for an answer to that.)
0
 
LVL 19

Expert Comment

by:actonwang
Comment Utility
heahea :)
0
 
LVL 34

Expert Comment

by:Mark Geerlings
Comment Utility
If you have a reporting database (a data warehouse) then a bit-mapped index may be a good option.  *DO NOT* create a bit-mapped index though on a table used in a transaction-processing system by multiple users!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now