Thread: table clustering brings joy

table clustering brings joy

From
Kevin Murphy
Date:
This is just an FYI for other people out there with large tables: table clustering sped up my queries from 10-100 times, which I am very happy about.  I'm posting this in case it's ever useful to anybody.  If someone reading this feels that I did something wrong, let me know.

I conducted tests on a table  with 13,982,464 rows that looks like this:

create table clustered_tagged_genes (
    pmid integer,
    mention text
);

The table is indexed on pmid and mention, and clustered on mention.  The statistics target was set to 1000 for each index, and the table was vacuum analyzed.  I haven't yet methodically compared varying the statistics target yet.

FYI, there are 1,348,398 distinct mention values and 4,162,269 distinct pmid values in the table.

For comparison, I had a non-clustered version of the same table:

create table tagged_genes (
    pmid integer,
    mention text
);

This table is indexed on pmid and mention, but not clustered.  (Again, statistics targets set to 1000, and table vacuum analyzed).

I tried two different equivalent queries on these tables:

"group-by-query"

  SELECT pmid
    FROM  {tablename}
    WHERE mention IN ({term1} [, ...])
    GROUP BY pmid
    HAVING count(distinct mention) = {num_terms};

and

"intersect-query"

  SELECT pmid FROM {table_name} WHERE mention = {term1}
[      INTERSECT
  SELECT pmid FROM {table_name} WHERE mention = {term2}
...
]


Out of curiosity, I also created a denormalized version of the table, which ganged associated pmids together into an array column type:

create table  array_tagged_genes (
    pmids integer[],
    mention text
);

The table is indexed on mention, statistics target set to 1000, and table vacuum analyzed.

For the array_tagged_genes query, I used this:

"array-query":

  SELECT * FROM array_explode((select pmids FROM array_tagged_genes WHERE mention  = {term1}
[      INTERSECT
  SELECT * FROM array_explode((select pmids FROM array_tagged_genes WHERE mention  = {term2}
...
]

where array_explode is a plpgsql function to unpack the pmids from their array and return them on separate rows.

The following test results were created by a perl program that dragged a file larger than memory through memory, restarted the postgresql server, ran explain analyze on the next query and averaged the results across three trials of each query.  (Every individual trial hopefully performed a query without the benefit of any operating system or postgresql caching.)  The computer was an otherwise unloaded dual 1.8GHz PowerMac G5 running Mac OS X 10.4.2 with 1GB RAM, running off a SATA drive (7200 rpm, 9ms seek, if you care).


RankQuery type/tableSearch termsRowsMsecsMsecs/rowRelative time
1intersect-query/clusteredmycn47949.840.101
2array-query/arraymycn47979.180.171.59
3group-by-query/clusteredmycn479163.430.343.28
4group-by-query/nonclusteredmycn4792238.794.6744.92







1group-by-query/clusteredmycn,trka20109.085.451
2intersect-query/clusteredmycn,trka20138.646.931.27
3array-query/arraymycn,trka20264.2513.212.42
4group-by-query/nonclusteredmycn,trka2010076.79503.8492.38







1intersect-query/clusteredlcat134168.110.051
2group-by-query/clusteredlcat134186.610.061.27
3array-query/arraylcat1341222.20.173.26
4group-by-query/nonclusteredlcat13417325.775.46107.55







1intersect-query/clusteredeps1514861.050.411
2group-by-query/clusteredeps1514867.750.461.11
3array-query/arrayeps1514880.450.541.32
4group-by-query/nonclusteredeps15148929.846.2815.23







1intersect-query/clusteredbrca12822105.740.041
2group-by-query/clusteredbrca12822161.950.061.53
3array-query/arraybrca12822198.720.071.88
4group-by-query/nonclusteredbrca1282212113.84.29114.56







1intersect-query/clusteredeps15,tax4,irs41102.9102.901
2group-by-query/clusteredeps15,tax4,irs41112.58112.581.09
3array-query/arrayeps15,tax4,irs41124.78124.781.21
4group-by-query/nonclusteredeps15,tax4,irs411024.091024.099.95

-Kevin Murphy

Re: table clustering brings joy

From
Greg Stark
Date:
Kevin Murphy <murphy@genome.chop.edu> writes:

> This is just an FYI for other people out there with large tables: table
> clustering sped up my queries from 10-100 times, which I am very happy about.
> I'm posting this in case it's ever useful to anybody.  If someone reading this
> feels that I did something wrong, let me know.

One thing you should realize is that cluster effectively removes all dead
tuples from the table. If you had lots of dead tuples that could have been
slowing things down.

Vacuum only marks dead tuples for reuse. If you're not running vacuum often
enough or you've done big batch updates then you may have accumulated lots of
dead tuples and then your vacuum analyze doesn't actually remove them from the
table.

Moreover, if the tables are undergoing updates or deletes then you should
expect to always have some steady state level of dead tuples in the table. Any
tests conducted immediately after a "vacuum full" or "cluster" won't include
that factor.

All that said clustering is indeed often quite effective. Especially if it
makes an index scan efficient enough to win over sequential scans you can see
some huge effects. It's most useful for tables that aren't undergoing lots of
updates and don't need to be reclustered often.

--
greg

Re: table clustering brings joy

From
Kevin Murphy
Date:
Greg Stark wrote:

>All that said clustering is indeed often quite effective. Especially if it
>makes an index scan efficient enough to win over sequential scans you can see
>some huge effects. It's most useful for tables that aren't undergoing lots of
>updates and don't need to be reclustered often.
>
>
Which is my situation; most tables are read-only.  Thanks for the comments.

-Kevin

Re: table clustering brings joy

From
Junaili Lie
Date:
Quick questions:
For big tables with frequent insert, no update, and frequent read
(using indexes), will clustering help?
what should be done on such table other than regular analyze?
comments are appreciated.


On 8/16/05, Kevin Murphy <murphy@genome.chop.edu> wrote:
> Greg Stark wrote:
>
> >All that said clustering is indeed often quite effective. Especially if it
> >makes an index scan efficient enough to win over sequential scans you can see
> >some huge effects. It's most useful for tables that aren't undergoing lots of
> >updates and don't need to be reclustered often.
> >
> >
> Which is my situation; most tables are read-only.  Thanks for the comments.
>
> -Kevin
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: Don't 'kill -9' the postmaster
>

Re: table clustering brings joy

From
Greg Stark
Date:
Junaili Lie <junaili@gmail.com> writes:

> Quick questions:
> For big tables with frequent insert, no update, and frequent read
> (using indexes), will clustering help?
> what should be done on such table other than regular analyze?
> comments are appreciated.

If you never have any deletes or updates then you don't really need to vacuum
the table regularly. (You still need to vacuum it before transaction id
wraparound but that's a pretty long time.)

So clustering won't help you by removing dead tuples and compacting the table.

But it can still help by ordering the records in the same order as your index.
The more the record order is correlated with the index the more effective the
index is and the larger the result set that can use that index productively.

That will only help if you're often retrieving moderately large result sets by
one particular index. If you normally only retrieve one record at a time or
from lots of different indexes then it probably won't really make much
difference.

New records won't be inserted in order though so periodically you'll want to
recluster the table to maintain the order.

--
greg

Re: table clustering brings joy

From
Ron Mayer
Date:
Greg Stark wrote:

> clustering...
> That will only help if you're often retrieving moderately large result sets by
> one particular index. If you normally only retrieve one record at a time or
> from lots of different indexes then it probably won't really make much
> difference.

It'll also help for columns whose values are related in some way.

For example, clustering a table of addresses based on "zip code"
will help lookups based on city or county or state (presumably
because all the disk pages for a given city will be grouped
together within the disk pages for the zip codes within the city).