dbTalk Databases Forums  

any way to speed up count(*)?

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss any way to speed up count(*)? in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
mh@pixar.com
 
Posts: n/a

Default any way to speed up count(*)? - 11-24-2009 , 06:22 PM






I've got an application being put together with cake/php. It's
pretty nice, but their data pager does this:

SELECT COUNT(*) AS COUNT
FROM foo f
LEFT JOIN bar b
ON (f.asset_group_id = b.asset_group_id)
WHERE 1 = 1

Any way possible to speed this up?

Many TIA!
Mark

--
Mark Harrison
Pixar Animation Studios

Reply With Quote
  #2  
Old   
Charles Hooper
 
Posts: n/a

Default Re: any way to speed up count(*)? - 11-25-2009 , 08:47 AM






On Nov 24, 7:22*pm, m... (AT) pixar (DOT) com wrote:
Quote:
I've got an application being put together with cake/php. *It's
pretty nice, but their data pager does this:

* * SELECT COUNT(*) AS COUNT
* * FROM foo f
* * LEFT JOIN bar b
* * ON (f.asset_group_id = b.asset_group_id)
* * WHERE 1 * * * * * * * * * *= 1

Any way possible to speed this up?

Many TIA!
Mark

--
Mark Harrison
Pixar Animation Studios
You probably need to determine what the SQL statement is doing to see
if it is possible to speed up the execution. Helpful methods:
* Generate a 10046 trace at level 8 for the SQL statement - execute a
simple SQL statement, such as SELECT SYSDATE FROM DUAL; after the SQL
statement to increase the chances of the STAT lines (row source
execution plan) being written to the trace file. Review the trace
file manually, or use TKPROF.
* Add a /*+ GATHER_PLAN_STATISTICS */ hint to the SQL statement, and
use DBMS_XPLAN with 'ALLSTATS LAST' specified as the third
parameter.

If it were a single table, an index on a column with a not NULL
constraint (such as a primary key column) could be used to hopefully
speed up a COUNT(*) operation, but that might not work in your case
due to the table join.

What Oracle release are you using? 10.2.0.4?

Charles Hooper
Co-author of "Expert Oracle Practices: Oracle Database Administration
from the Oak Table"
IT Manager/Oracle DBA
K&M Machine-Fabricating, Inc.

Reply With Quote
  #3  
Old   
Mark D Powell
 
Posts: n/a

Default Re: any way to speed up count(*)? - 11-25-2009 , 10:19 AM



On Nov 24, 7:22*pm, m... (AT) pixar (DOT) com wrote:
Quote:
I've got an application being put together with cake/php. *It's
pretty nice, but their data pager does this:

* * SELECT COUNT(*) AS COUNT
* * FROM foo f
* * LEFT JOIN bar b
* * ON (f.asset_group_id = b.asset_group_id)
* * WHERE 1 * * * * * * * * * *= 1

Any way possible to speed this up?

Many TIA!
Mark

--
Mark Harrison
Pixar Animation Studios
Mark, you tune a select count like you tune any other SQL statement.
Start with the explain plan. Here is an article on how Oracle
performs select count.

Is there a good way of counting the number of rows in a table ?
http://www.jlcomp.demon.co.uk/faq/count_rows.html

The best optimization is not to perform the count. That is, if you
are going to fetch the data anyway just go ahead and fetch it if
possible. If you just need to know if a row exists but do not truely
need to know how many you can use either where rownum = 1 or an exists
subquery to cut the counting off when a row is found. That is, you
get a count of 1 for a hit and zero where there are not hits (rows
matching query criteria).

In you your example why do you have where 1 = 1 ? Kind of unnecessary
isn't it.

HTH -- Mark D Powell --

Reply With Quote
  #4  
Old   
Robert Klemme
 
Posts: n/a

Default Re: any way to speed up count(*)? - 11-25-2009 , 11:58 AM



On 25.11.2009 17:19, Mark D Powell wrote:
Quote:
On Nov 24, 7:22 pm, m... (AT) pixar (DOT) com wrote:
I've got an application being put together with cake/php. It's
pretty nice, but their data pager does this:

SELECT COUNT(*) AS COUNT
FROM foo f
LEFT JOIN bar b
ON (f.asset_group_id = b.asset_group_id)
WHERE 1 = 1

Any way possible to speed this up?

Many TIA!
Mark

--
Mark Harrison
Pixar Animation Studios

Mark, you tune a select count like you tune any other SQL statement.
Start with the explain plan. Here is an article on how Oracle
performs select count.

Is there a good way of counting the number of rows in a table ?
http://www.jlcomp.demon.co.uk/faq/count_rows.html

The best optimization is not to perform the count. That is, if you
are going to fetch the data anyway just go ahead and fetch it if
possible. If you just need to know if a row exists but do not truely
need to know how many you can use either where rownum = 1 or an exists
subquery to cut the counting off when a row is found. That is, you
get a count of 1 for a hit and zero where there are not hits (rows
matching query criteria).

In you your example why do you have where 1 = 1 ? Kind of unnecessary
isn't it.
For the count the left join might be unnecessary as well. This would be
the case if there would be at most one b.asset_group_id per
f.asset_group_id. Am I missing something?

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Reply With Quote
  #5  
Old   
joel garry
 
Posts: n/a

Default Re: any way to speed up count(*)? - 11-25-2009 , 12:27 PM



On Nov 25, 8:19*am, Mark D Powell <Mark.Powe... (AT) hp (DOT) com> wrote:
Quote:
On Nov 24, 7:22*pm, m... (AT) pixar (DOT) com wrote:


In you your example why do you have where 1 = 1 ? *Kind of unnecessary
isn't it.
I have an example pinned up on my cube of an OCI generated code that
has 48 1=1 statements in it. It would be even more unnecessary for me
to try to get rid of them. :-)

I'm sure there must be more somewhere in this kind of code, that one
just happened to catch my eye in EM one day. Doesn't seem to bother
the optimizer at all.

jg
--
@home.com is bogus.
Death of wikipedia, news at 11. http://news.cnet.com/8301-1023_3-10403467-93.html

Reply With Quote
  #6  
Old   
Shakespeare
 
Posts: n/a

Default Re: any way to speed up count(*)? - 11-25-2009 , 03:04 PM



joel garry schreef:
Quote:
On Nov 25, 8:19 am, Mark D Powell <Mark.Powe... (AT) hp (DOT) com> wrote:
On Nov 24, 7:22 pm, m... (AT) pixar (DOT) com wrote:


In you your example why do you have where 1 = 1 ? Kind of unnecessary
isn't it.

I have an example pinned up on my cube of an OCI generated code that
has 48 1=1 statements in it. It would be even more unnecessary for me
to try to get rid of them. :-)

I'm sure there must be more somewhere in this kind of code, that one
just happened to catch my eye in EM one day. Doesn't seem to bother
the optimizer at all.

jg
--
@home.com is bogus.
Death of wikipedia, news at 11. http://news.cnet.com/8301-1023_3-10403467-93.html

Most times, these queries are generated by some tool that needs a where
clause anyway, and 'AND's or 'OR's the user specified clauses to it, and
they put in the 1=1 for when a user does not enter any condition..

Shakespeare
(What's in = What's in)

Reply With Quote
  #7  
Old   
Thomas Kellerer
 
Posts: n/a

Default Re: any way to speed up count(*)? - 11-25-2009 , 03:29 PM



mh (AT) pixar (DOT) com wrote on 25.11.2009 01:22:
Quote:
I've got an application being put together with cake/php. It's
pretty nice, but their data pager does this:

SELECT COUNT(*) AS COUNT
FROM foo f
LEFT JOIN bar b
ON (f.asset_group_id = b.asset_group_id)
WHERE 1 = 1

Any way possible to speed this up?
If the statement is really that simple than you might get away with a materialized view:

CREATE MATERIALZED VIEW v_foo
ENABLE QUERY REWRITE
SELECT COUNT(*) AS table_count
FROM foo f
LEFT JOIN bar b
ON (f.asset_group_id = b.asset_group_id);

Oracle will see that it can use the view to satisfy your select and all it needs to do is return the single row from the MV.

You need to make sure the view is up-to-date though.

The problem is most probably that you won't be able to declare it as "refresh fast on commit" but it might be worth trying.

Thomas

Reply With Quote
  #8  
Old   
Volker Borchert
 
Posts: n/a

Default Re: any way to speed up count(*)? - 11-26-2009 , 01:22 PM



Shakespeare wrote:
Quote:
Most times, these queries are generated by some tool that needs a where
clause anyway, and 'AND's or 'OR's the user specified clauses to it, and
they put in the 1=1 for when a user does not enter any condition..
Surprisingly, the optimizer sometimes seems to do a bad job about such
constant clauses. As in "where 1 = 2" resulting in a full table scan...

--

"I'm a doctor, not a mechanic." Dr Leonard McCoy <mccoy (AT) ncc1701 (DOT) starfleet.fed>
"I'm a mechanic, not a doctor." Volker Borchert <v_borchert (AT) despammed (DOT) com>

Reply With Quote
  #9  
Old   
Shakespeare
 
Posts: n/a

Default Re: any way to speed up count(*)? - 11-26-2009 , 02:20 PM



Volker Borchert schreef:
Quote:
Shakespeare wrote:
Most times, these queries are generated by some tool that needs a where
clause anyway, and 'AND's or 'OR's the user specified clauses to it, and
they put in the 1=1 for when a user does not enter any condition..

Surprisingly, the optimizer sometimes seems to do a bad job about such
constant clauses. As in "where 1 = 2" resulting in a full table scan...

Yep. You never know if somewhere/sometime in your query 1 will become
equal to 2..... Better safe than sorry.

Shakespeare

Reply With Quote
  #10  
Old   
Gerard H. Pille
 
Posts: n/a

Default Re: any way to speed up count(*)? - 11-26-2009 , 02:32 PM



Volker Borchert wrote:
Quote:
Shakespeare wrote:
Most times, these queries are generated by some tool that needs a where
clause anyway, and 'AND's or 'OR's the user specified clauses to it, and
they put in the 1=1 for when a user does not enter any condition..

Surprisingly, the optimizer sometimes seems to do a bad job about such
constant clauses. As in "where 1 = 2" resulting in a full table scan...

You're kidding, right?

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.