Re: varchar as primary key

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Jeff Davis <pgsql(at)j-davis(dot)com>
Cc: Alexander Staubo <alex(at)purefiction(dot)net>, Matthew Hixson <hixson(at)poindextrose(dot)org>, Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: varchar as primary key
Date: 2007-05-04 19:42:45
Message-ID: 6171.1178307765@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Jeff Davis <pgsql(at)j-davis(dot)com> writes:
> $ ./cmp
> locale set to: en_US.UTF-8
> strcmp time elapsed: 2034183 us
> strcoll time elapsed: 2019880 us

It's hardly credible that you could do either strcmp or strcoll in 2 nsec
on any run-of-the-mill hardware. What I think is happening is that the
compiler is aware that these are side-effect-free functions and is
removing the calls entirely, or at least moving them out of the loops;
these times would be credible for loops consisting only of an increment,
test, and branch.

Integer overflow in your elapsed-time calculation is probably a risk
as well --- do the reports add up to something like the actual elapsed
time?

I tried a modified form of your program (attached) on an FC6 machine
and found that at any optimization level above -O0, that compiler
optimizes the strcmp() case into oblivion, even with code added as below
to try to make it look like a real operation. The strcoll() call without
any following test, as you had, draws a warning about "statement with
no effect" which is pretty suspicious too. With the modified program
I get

$ gcc -O1 -Wall cmptest.c
$ time ./a.out
locale set to: en_US.UTF-8
strcmp time elapsed: 0 us
strcoll time elapsed: 67756363 us

real 1m7.758s
user 1m7.746s
sys 0m0.006s

$ gcc -O0 -Wall cmptest.c
$ time ./a.out
locale set to: en_US.UTF-8
strcmp time elapsed: 4825504 us
strcoll time elapsed: 68864890 us

real 1m13.692s
user 1m13.676s
sys 0m0.010s

So as best I can tell, strcoll() is pretty dang expensive on Linux too.

regards, tom lane

Attachment Content-Type Size
unknown_filename text/plain 1.1 KB

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Scott Ribe 2007-05-04 20:34:24 Casting to varchar
Previous Message Jeff Davis 2007-05-04 19:10:57 Re: varchar as primary key