简体   繁体   中英

Spring JPA Hibernate : slow SELECT query

I encounter an optimisation problem and I can't figure out why my query is so slow.

Here my entity :

@Entity
@Table(name = "CLIENT")
public class Client {

private static final long serialVersionUID = 1L;
@Id
@Column(name = "CLIENT_ID")
@SequenceGenerator(name = "ID_GENERATOR", sequenceName = "CLIENT_S", allocationSize = 1, initialValue = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ID_GENERATOR")
private Long id;

@Column(name="LOGIN")
private String login;

@Column(name="PASSWORD")
private String password;

And the DAO

@NoRepositoryBean
public interface ClientDao extends JpaRepository<Client, Long>, JpaSpecificationExecutor<Client> {
    Client findByPasswordAndLogin(@Param("login") String customerLogin,@Param("password") String customerHashedPassword);
}

When the method findByPasswordAndLogin is executed, it takes about 200ms to be completed (seen both through Junit tests and with JProfiler).

Here the Hibernate query : Hibernate: select clientx0_.CLIENT_ID as CLIENT_ID1_4_, clientx0_.LOGIN as LOGIN9_4_, clientx0_.PASSWORD as PASSWORD10_4_, clientx0_.STATUT as STATUT13_4_ from CLIENT clientx0_ where clientx0_.PASSWORD=? and clientx0_.LOGIN=?

When I execute manually the SQL query on the database, it takes only 3ms :

select * from CLIENT where PASSWORD='xxxxx' and LOGIN='yyyyyyyy'

We have 4000 clients in our development environnement. More than a million in production.

Here the context :

  • JDK 8
  • Spring 4.1.6.RELEASE + JPA + Hibernate
  • Oracle Database 10

Any idea ?

I have tested different types of DAO (I don't publish code here because it is so dirty) :

  • With Hibernate : ~200ms
  • With (Injected) Spring JDBCTemplate and RowMapper : ~70 ms
  • With Java Statement : ~2 ms
  • With Java OracleStatement : ~5 ms
  • With Java PreparedStatement : ~100ms
  • With Java PreparedStatement adjusted with Fetch size = 5000 : ~50ms
  • With Java OraclePreparedStatement : ~100ms
  • With Java OraclePreparedStatement adjusted with PreFetch size = 5000 : ~170ms

Notes :

  • DAO injected by Spring instead of new ClientDao() : +30ms lost (-sick-)
  • Connection time to DB : 46ms

I could use :

  • Java Statement with manual sanitized fields.
  • Pre-connection on application launch
  • Do not use Spring Injection

But :

  • Not really secured / safe
  • Fast for a small number of rows, slow to map ResultSet to entity on large number of rows (I also have this use case)

So :

The Spring JDBCTemplate with RowMapper seems to be the best solution to increase performances on specific case. And we can keep a security on SQL queries. But need to write specific RowMapper to transform ResultSet to Entity.

Example of Spring JDBCTemplate

@Repository
public class ClientJdbcTemplateDao {


    private final Logger logger = LoggerFactory.getLogger(ClientJdbcTemplateDao.class);

    private JdbcTemplate jdbcTemplate;

    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    public List<Client> find() {
        List<Client> c = this.jdbcTemplate.query( "SELECT login FROM Client WHERE LOGIN='xxxx' AND PASSWORD='xxx'", new ClientRowMapper());
        return c;
    }
}

Example of Client RowMapper

public class ClientRowMapper implements RowMapper<Client> {

    @Override
    public Client mapRow(ResultSet arg0, int arg1) throws SQLException {
        // HERE IMPLEMENTS THE CONVERTER
        // Sample : 
        // String login = arg0.getString("LOGIN")
        // Client client = new Client(login);
        // return client;
    }
}

Maybe can be better, any suggestion is welcome.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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