title: Spring Boot JPARepository performance on save()
body: <p>I have an issue where my spring boot application performance is very slow when inserting data.</p>
  
  <p>I am extracting a large subset of data from one database and inserting the data into another database. </p>
  
  <p>The following is my entity. </p>
  
  <pre><code>@Entity
  @Table(name = "element")
  public class VXMLElementHistorical {
  
  @Id
  @Column(name = "elementid")   
  private long elementid;
  
  @Column(name = "elementname")
  private String elementname; 
  
  Getter/Setter methods...    
  </code></pre>
  
  <p>I have configured a JPA repository</p>
  
  <pre><code>public interface ElementRepository extends JpaRepository&lt;Element, Long&gt; {
  
  }
  </code></pre>
  
  <p>and call the save() method with my object  </p>
  
  <pre><code>@Transactional 
  public void processData(List&lt;sElement&gt; hostElements) 
  throws DataAccessException { 
  
  List&lt;Element&gt; elements = new ArrayList&lt;Element&gt;();    
  
  for (int i = 0; i &lt; hostElements.size(); i++) {
          Element element = new Element();
          element.setElementid(hostElements.get(i).getElementid());
          element.setElementname(hostElements.get(i).getElementname());
          elements.add(element);
      }
  
     try{
     elementRepository.save(elements);{
     //catch etc...
  
  }
  </code></pre>
  
  <p>What is happening is that for each item, it is taking between 6 and 12 seconds to perform an insert. I have turned on hibernate trace logging and statistics and what is happening when I call the save function is that hibernate performs two queries, a select and an insert. The select query is taking 99% of the overall time.  </p>
  
  <p>I have ran the select query direct on the database and the result returns in nanoseconds. Which leads me to believe it is not an indexing issue however I am no DBA. </p>
  
  <p>I have created a load test in my dev environment, and with similar load sizes,  the over all process time is no where near as long as in my prod environment.  </p>
  
  <p>Any suggestions? </p>