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,216 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
ID: 16960428
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
ID: 16961588
>>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 35

Expert Comment

by:Mark Geerlings
ID: 16961843
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
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 19

Expert Comment

by:actonwang
ID: 16962223
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
 
LVL 35

Expert Comment

by:Mark Geerlings
ID: 16963166
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
ID: 16963219
to markegeer,

     >>it might not be up to date.

     did you see that?
0
 
LVL 35

Expert Comment

by:Mark Geerlings
ID: 16963431
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
ID: 16963485
heahea :)
0
 
LVL 35

Expert Comment

by:Mark Geerlings
ID: 16968338
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle DATE Column Space 11 80
Read only access to a Procedure in oracle? 4 68
use lov values 2 51
Database Design Dilemma 6 40
This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Via a live example, show how to take different types of Oracle backups using RMAN.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

832 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