Re: Count(*)ing nested tables
- From: "Jim Kennedy" <kennedy-downwithspammersfamily@xxxxxxxxx>
- Date: Tue, 26 Jul 2005 07:04:57 -0700
"John Currier" <md4curvs@xxxxxxxxx> wrote in message
news:1122386424.873791.220540@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> > When you mention * in the select-list of a table with nest tables, it
> > tries to expand all the fields in nested table(s) too, which results in
> > a messy multi-dimensional result set for each row, and seems to be
> > ambiguous in the context of COUNT().
> >
> > I don't have any direct experience with nested tables, but I'm supposing
> > that you could try "SELECT COUNT(1) FROM WHATEVER". That should be a
> > non-NULL value for each row of the non-nested table, without attempting
> > to reference any of the nested tables within it. The value of 1 is
> > arbitrary; any other non-NULL constant value would work equally well,
> > since we're only counting them.
> >
> > You could probably also do COUNT(pk) where pk is the name of a non-NULL
> > column in the table. But for when you don't know the name of a non-NULL
> > column (if any exists), stick with COUNT(1).
>
> Interesting. Your comments are making me think about how to optimize
> that select.
>
> So, what's the most efficient way to get the number of rows in an
> arbitrary table, given that I know the primary key and index details?
>
> Thanks,
> John
> http://schemaspy.sourceforge.net
>
Benchmark it. It will differ by RDBMS. In all likelihood using the primary
key in the count would probably be most efficient. Usually there is an
index on the primary key and the optimizer might use the index to get the
row count.
Jim
.
- Follow-Ups:
- Re: Count(*)ing nested tables
- From: John Currier
- Re: Count(*)ing nested tables
- References:
- Count(*)ing nested tables
- From: John Currier
- Re: Count(*)ing nested tables
- From: Bill Karwin
- Re: Count(*)ing nested tables
- From: John Currier
- Count(*)ing nested tables
- Prev by Date: Re: Count(*)ing nested tables
- Next by Date: Re: Count(*)ing nested tables
- Previous by thread: Re: Count(*)ing nested tables
- Next by thread: Re: Count(*)ing nested tables
- Index(es):
Relevant Pages
|