I am working on a Spring MVC/Hibernate web app and the DB queries are awfully slow. I already searched, but none of the performance problems people asked about here seem to match my problem (suggestions included issues with model mapping or connection pooling).
No matter if I use plain JDBC or Hibernate to query the DB it takes equally long. To make sure it is not related with the Spring framework I wrote this simple application:
public class JDBCTest {
static final String JDBC_DRIVER = ...;
static final String DB_URL = ...;
static final String USER = ...;
static final String PASS = ...;
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
double time;
try {
Class.forName("com.mysql.jdbc.Driver");
start = System.currentTimeMillis();
conn = DriverManager.getConnection(DB_URL,USER,PASS);
System.out.println("Conn. duration: " + (System.currentTimeMillis() - start));
stmt = conn.createStatement();
String sql = "SELECT * FROM temperature WHERE SensorId = 1 AND metered >= '2016-09-15 14:00:00' AND metered <= '2016-09-16 20:00:00'";
start = System.currentTimeMillis();
ResultSet rs = stmt.executeQuery(sql);
System.out.println("Query duration: " + (System.currentTimeMillis() - start));
...
While opening the connection takes 200-300ms (seems reasonable), the query takes 1500-2000ms.
When executing the very same query in PHPMyAdmin it says the query took 0.0312 seconds (compared to >1 second with JDBC).
What could be the issue? No matter if it is a minimalistic JDBC app as shown above, the Spring app using JDBC or using Hibernate - the query always takes roughly the same amount of time.
It doesn't seem to have to do with the timestamps either, as even simple "SELECT * FROM temperature" queries are many times slower than what PHPMyAdmin takes.