简体   繁体   English

Spring Data JPA:非常慢的插入和选择查询

[英]Spring Data JPA: very slow insert and select queries

I have a pretty simple spring-data-jpa + Hibernate application, which stores customers data in MySql Database (the full source code is posted here ). 我有一个非常简单的spring-data-jpa + Hibernate应用程序,它将客户数据存储在MySql数据库中(完整的源代码发布在这里 )。

The problem is that it runs insert and select queries very slowly , comparing to what I have through mysql CLI: 问题是它运行插入选择查询非常慢 ,与我通过mysql CLI进行比较:

  • insert one row takes ~ 3600ms , 插入一行需要~ 3600ms
  • select all (just 2 rows) takes ~ 1700ms 选择全部(仅2行)需要~ 1700ms
  • both queries take in mysql CLI about 0.12s . 两个查询都在mysql CLI中大约0.12s

There is a similar problem discussed here , however in my case the measurements are way worse (even though I don't insert batch, it's just one simple row in DB). 有讨论过类似的问题在这里 ,但在我的情况的测量方式更糟(尽管我不插入批次,它只是一个简单的排DB)。 Is there any way to improve performance in Spring JPA/Hibernate? 有没有办法提高Spring JPA / Hibernate的性能?

Another question, is there any way to reduce size of spring-data-jpa and hibernate-entitymanager? 另一个问题是,有没有办法减少spring-data-jpa和hibernate-entitymanager的大小? I was able to exclude byte-buddy and jandex dependencies without harm to the program, but that's only couple Mbs (the shaded jar size is down from 19.6Mb to 16.6Mb)? 我能够排除byte-buddy和jandex依赖项而不会对程序造成伤害,但这只是几个Mbs(阴影jar大小从19.6Mb下降到16.6Mb)?

UPDATE As per request here is the code (the all sources are here ): 更新根据请求,这里是代码(所有来源都在这里 ):

@Entity
@AttributeAccessor("field")
@Table(name = "customer")
public class Customer implements Serializable{
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name="id",unique=true, nullable=false, insertable=true, updatable=true)
  @Type(type="long")
  private Long id;

  @Column(name = "name")
  private String name;
//+ constructors, getters/setters
}

Here is Spring application and also saving (insert) customer: 这是Spring应用程序,也是保存(插入)客户:

public class Application {
  private static ApplicationContext applicationContext;

  static CustomerRepository customerRepository;

  public static void main(String[] args) throws InterruptedException {

    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MySQLAutoconfiguration.class);

    customerRepository = ctx.getBean(CustomerRepository.class);
    runTest();
  }

private static void runTest () throws {
...
//insert
  Customer customerJohn = customerRepository.save(new Customer("John"));
//select
  Customer foundEntity = customerRepository.findOne(customerJohn.getId());
...
}

And configuration: 和配置:

@Configuration
@ComponentScan
@EnableJpaRepositories (basePackages = "com.vk.dal.repository")
@PropertySource("classpath:mysql.properties")
public class MySQLAutoconfiguration {

  @Autowired
  private Environment env;

  @Bean
  public DataSource dataSource() {
    final DriverManagerDataSource dataSource = new DriverManagerDataSource();

    dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
    dataSource.setUrl(env.getProperty("spring.datasource.url"));
    dataSource.setUsername(env.getProperty("spring.datasource.username") != null ? env.getProperty("spring.datasource.username") : "");
    dataSource.setPassword(env.getProperty("spring.datasource.password") != null ? env.getProperty("spring.datasource.password") : "");

    return dataSource;
  }

  @Bean
  public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
    final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource);
    em.setPackagesToScan("com.vk.dal.domain");
    em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
    Properties properties = additionalProperties();
    if (properties != null) {
      em.setJpaProperties(properties);
    }
    return em;
  }

  @Bean
  JpaTransactionManager transactionManager(final EntityManagerFactory entityManagerFactory) {
    final JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(entityManagerFactory);
    return transactionManager;
  }


  final Properties additionalProperties() {
    final Properties hibernateProperties = new Properties();

    hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("mysql-hibernate.hbm2ddl.auto"));
    hibernateProperties.setProperty("hibernate.dialect", env.getProperty("mysql-hibernate.dialect"));
    hibernateProperties.setProperty("hibernate.show_sql", env.getProperty("mysql-hibernate.show_sql") != null ? env.getProperty("mysql-hibernate.show_sql") : "false");

    return hibernateProperties;
  }

}

Appreciate any help. 感谢任何帮助。

I had the same results, in my case issue was in connection with the database server. 我有相同的结果,在我的情况下问题与数据库服务器有关。 The server was remote and executing query was too slow. 服务器是远程的,执行查询太慢了。 When I deployed my application to the same server, it became fast enough. 当我将我的应用程序部署到同一台服务器时,它变得足够快。 Insert object with many dependencies(one to many, many to one) now takes 190ms. 插入具有许多依赖项(一对多,多对一)的对象现在需要190毫秒。 Select this object takes 61ms. 选择此对象需要61ms。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM