4

I'm a java developer working with jpa, before to define the indexes in my entities i would like to known which one is faster o better to use?.

2
  • Did you test/benchmark it yourself? What did you come up with? Commented Apr 10, 2014 at 16:07
  • 2
    No, i haven't tested it, in fact i don`t know how to prepare a test for this kind of situation and read the results.
    – OJVM
    Commented Apr 10, 2014 at 18:49

1 Answer 1

9

bigserial is just shorthand for creating a sequence. So if you include the time it takes to type, bigserial has better "performance" :)

CREATE TABLE tablename (
    colname BIGSERIAL
);

Equals

CREATE SEQUENCE tablename_colname_seq;

CREATE TABLE tablename (
    colname biginteger DEFAULT nextval('tablename_colname_seq') NOT NULL
);

Also, if you these are your primary keys, then they are already indexed, so you don't need to index them (foreign key columns aren't indexed automatically though, FYI).

For JPA, you should use @GeneratedValue(strategy=GenerationType.IDENTITY) for PostgreSQL serial/bigserial columns.

1
  • Tank you. I haven't realized the sequence that is created when the serial or bigserial is used. I'll use the @GeneratedValue(strategy=GenerationType.IDENTITY) so my persistence layer can work either mysql or postgresql
    – OJVM
    Commented Apr 10, 2014 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.