Re: Database tuning

From: Antonio Fiol Bonnín <fiol(at)w3ping(dot)com>
To: K Old <kevsurf4(at)hotmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Database tuning
Date: 2001-12-21 17:41:41
Message-ID: 3C237455.80504@w3ping.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello,

The problem is not the database, but your query. You are asking your
poor Pentium II to sort "your" Bible in a random order!! And then you
will only retrieve the first entry.

If you actually want to get a uniformly chosen random verse from The
Bible, and you actually CARE about the quality of your randomness you
can use your id field. Do I understand well if I read "each verse has an
unique ID" ?

You can index that table by your id, and then SELECT ... WHERE
bible.book = books.id AND bible.id=XXX;

XXX is a random value ranking from 0 (or 1) to the maximum id value. You
generate it outside the database.

You can also try (but I am not certain at all that these may work):

SELECT ... WHERE bible.book = books.id AND bible.id=(MAX*random());

But you still need to know MAX.

Both approaches need the certainty that every different ID has an
associated verse.

You can even store the max on some other table, if you do not want to
care about it in your application, and then issue:

SELECT ... WHERE bible.book = books.id AND bible.id=(select
maxval*random() from counttable);

I hope that helps.

Anyway, before trying any query on a big database, an EXPLAIN should be
helpful/meaningful.

If you see SEQ SCAN on a very long table, that is certainly BAD.

Standard question: Did you VACUUM ANALYZE? If not, do it.

Good luck!

Antonio

K Old wrote:

> Hello all,
>
> I have a PostgreSQL database that is storing The Bible. It has 31,103
> records in it and I have a PHP page executing this query:
> SELECT
> books.book_name, bible.chapter, bible.verse, bible.versetext
> FROM asv_bible bible, book_bible books WHERE bible.book = books.id
> ORDER BY random() LIMIT 1
>
> The database schema is:
>
> /* --------------------------------------------------------
> Sequences
> -------------------------------------------------------- */
> CREATE SEQUENCE "book_bible_seq" start 1 increment 1 maxvalue
> 2147483647 minvalue 1 cache 1;
>
> /* --------------------------------------------------------
> Table structure for table "asv_bible"
> -------------------------------------------------------- */
> CREATE TABLE "asv_bible" (
> "id" int8 NOT NULL,
> "book" int8,
> "chapter" int8,
> "verse" int8,
> "versetext" text,
> CONSTRAINT "asv_bible_pkey" PRIMARY KEY ("id")
> );
>
>
>
> /* --------------------------------------------------------
> Table structure for table "book_bible"
> -------------------------------------------------------- */
> CREATE TABLE "book_bible" (
> "id" int4 DEFAULT nextval('book_bible_seq'::text) NOT NULL,
> "book_name" varchar(20),
> CONSTRAINT "book_bible_pkey" PRIMARY KEY ("id")
> );
>
> Right now it takes 9 seconds to return the results. I don't think
> that it has anything to do with the language executing it, as I have
> run the same query in via Perl and had the same luck.
>
> I was wondering if anyone could offer any help with lowering the time
> it takes to run?
>
> Am I optimizing the database correctly?
>
> My hardware is a Pentium II 400 with 128MB of RAM.
>
> Or if this is the normal runtime for a database of this size, I'd just
> like confirmation.
>
> Thanks,
> Kevin
>
>
> _________________________________________________________________
> Send and receive Hotmail on your mobile device: http://mobile.msn.com
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
>
> .
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Gregory Wood 2001-12-21 18:41:33 Re: Can't use subselect in check constraint
Previous Message Ian Harding 2001-12-21 17:19:51 Re: How Many Years have Passed?