One of my service takes a long time to execute. Analysis has shown that nextval()
calls of a certain sequence take about 5 seconds each. Why is that, and would you have suggestions to improve this? Thank you.
Context
This is a regular Spring 4.2.4 / Hibernate 5.1.0 application, with Postgresql 9.6.3. The slow service saveAndFlushes a new entity, the graph of which implying the inserts of many linked entities (about 60k).
I have wrapped the driver with DriverSpy, so as to print SQL statements to the console. The SQL generated by Hibernate is consistent with the relationship model, and properly batched (size=100). However, there are many calls to the nextval()
of the sequence of the id of the linked entity, each of these taking about 5s.
The target table holds about 4 million records.
Detailed info
Sequence info (grabbed from pgAdmin):
- currentValue = 54225001
- increment = 1000
- minimum = 1
- maximum = 9223372036854775807
- cache = 1 (I've tried values 1000 and 10000, no visible change)
- cycled = no
Hibernate info (simplified by merging a MappedSuperClass with the child class):
@Entity
@SequenceGenerator(allocationSize = 1000, name = "generatorName", sequenceName = "sequenceName")
@Table(...)
public class XXX{
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generatorName")
private Long id;
EDIT: structure of the table (and related objects)
CREATE SEQUENCE seq_id_unit_brief
START WITH 1
INCREMENT BY 1000
NO MINVALUE
NO MAXVALUE
CACHE 1;
CREATE TABLE unit_brief (
id bigint NOT NULL,
ubf_brief_id bigint NOT NULL,
ubf_unit_id character varying(50) NOT NULL,
ubf_start_date date NOT NULL,
ubf_end_date date NOT NULL,
modified_by character varying(100) NOT NULL,
modified_time timestamp without time zone NOT NULL
);
ALTER TABLE ONLY unit_brief
ADD CONSTRAINT pk_unit_brief PRIMARY KEY (id);
CREATE INDEX idx_unit_brief_brief_id ON unit_brief USING btree (ubf_brief_id);
CREATE INDEX idx_unit_brief_end_date ON unit_brief USING btree (ubf_end_date);
CREATE INDEX idx_unit_brief_start_date ON unit_brief USING btree (ubf_start_date);
ALTER TABLE ONLY unit_brief
ADD CONSTRAINT fk_unit_brief_brief_id FOREIGN KEY (ubf_brief_id) REFERENCES brief(id);