HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

postgresql bigserial or sequence using jpa

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
postgresqlbigserialsequenceusingjpa

Problem

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?.

Solution

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.

Code Snippets

CREATE TABLE tablename (
    colname BIGSERIAL
);
CREATE SEQUENCE tablename_colname_seq;

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

Context

StackExchange Database Administrators Q#62939, answer score: 9

Revisions (0)

No revisions yet.