Re: Fwd: HSQL/MySQL query performance

Viktor Ádám <[email protected]>
Newsgroups gmane.comp.java.hsqldb.user
Message-ID <CAGX1HrrWLXwaQ255w5stm2aLZN4iaovt3CxSc_mTNPdEyabVhA@mail.gmail.com>
Hi Fred,

thanks for your help! It is true that inserts are much faster and that
is very impressive.
I wasn't entirely informative about our setup. We would like to use
HSQLDB in server mode. I've attached a new test and its result where
we executed the sample with a server HSQLDB instance, an in-process
instance (file:) and with MySQL. The parameters you suggested
definitely helps in in-process mode (our select times has gone down to
about 10 ms from about 150-200 ms). In server mode there is a slight
improvement but it is still slower for us than MySQL.
Do you think that it should be faster even in server mode with HSQL protocol?

Thanks again for your help!

Viktor

On Thu, Dec 19, 2013 at 1:08 AM, Fred Toussi
<[email protected]> wrote:
> The log shows the inserts are 3.5 times faster with HSQLDB.
>
> As for your SELECT statements, you are selecting all the rows in the
> table. The default CACHE ROWS and CACHE SIZE settings for HSQLDB allow
> fewer rows, so each time you select, lots of rows are fetched from the
> disk.
>
> Just increase the above settings until SELECT speed is what you want. I
> think it should be faster than MySQL.
>
> Fred
>
> On Wed, Dec 18, 2013, at 10:22, Viktor Ádám wrote:
>> Hi!
>>
>> We are migrating our application from MySQL to HSQLDB to have greater
>> control over the database. Everything works great except that queries
>> are slower than they were on MySQL.
>> I've attached a simple test running the same CREATE/INSERT/SELECT
>> statements on HSQL and MySQL and the produced output also. In the
>> attached output MySQL queries are 4-5x times faster and on another
>> development machine this ratio is about 8-10x.
>> Can you give as a hint about what we're doing wrong? Are there any
>> configuration parameters which could be tweaked?
>>
>> Our test PC was:
>> CPU: Intel Core i5-2400 @ 3.30GHz
>> RAM: 4GB
>> OS: Windows 7 Home Premium
>> MySQL: 5.5.11
>>
>> Thank you in advance!
>>
>> Best regards,
>> Viktor Ádám
>> ------------------------------------------------------------------------------
>> Rapidly troubleshoot problems before they affect your business. Most IT
>> organizations don't have a clear picture of how application performance
>> affects their revenue. With AppDynamics, you get 100% visibility into
>> your
>> Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics
>> Pro!
>> http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk
>> _______________________________________________
>> Hsqldb-user mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/hsqldb-user
>> Email had 2 attachments:
>> + dbperformance.txt
>>   3k (text/plain)
>> + DBPerformanceTester.java
>>   8k (text/x-java)
>
> ------------------------------------------------------------------------------
> Rapidly troubleshoot problems before they affect your business. Most IT
> organizations don't have a clear picture of how application performance
> affects their revenue. With AppDynamics, you get 100% visibility into your
> Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro!
> http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk
> _______________________________________________
> Hsqldb-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/hsqldb-user

------------------------------------------------------------------------------
Rapidly troubleshoot problems before they affect your business. Most IT 
organizations don't have a clear picture of how application performance 
affects their revenue. With AppDynamics, you get 100% visibility into your 
Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro!
http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk

_______________________________________________
Hsqldb-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/hsqldb-user
dbperformance2.txt (text/plain, 3.9 KB)
---   HSQLDB (server)   ---
[Server@169e11]: [Thread[main,5,main]]: setSilent(true)
2013-12-19 09:26:43,310 [main] INFO  hsqldb.db.HSQLDB4309F6C2C4.ENGINE - Checkpoint start
2013-12-19 09:26:43,310 [main] INFO  hsqldb.db.HSQLDB4309F6C2C4.ENGINE - checkpointClose start
2013-12-19 09:26:43,323 [main] INFO  hsqldb.db.HSQLDB4309F6C2C4.ENGINE - checkpointClose end
2013-12-19 09:26:43,325 [main] INFO  hsqldb.db.HSQLDB4309F6C2C4.ENGINE - Checkpoint end - txts: 1
[Server@169e11]: Initiating startup sequence...
[Server@169e11]: Server socket opened successfully in 9 ms.
[Server@169e11]: Database [index=0, id=0, db=file:db/testdb2, alias=name] opened sucessfully in 0 ms.
[Server@169e11]: Startup sequence completed in 10 ms.
[Server@169e11]: 2013-12-19 09:26:43.347 HSQLDB server 2.3.0 is online on port 1234
[Server@169e11]: To close normally, connect and execute SHUTDOWN SQL
[Server@169e11]: From command line, use [Ctrl]+[C] to abort abruptly
Init: 738
GetConnection: 139
Drop: 1
Create: 1
2013-12-19 09:26:44,050 [HSQLDB Connection @199f91c] INFO  hsqldb.db.HSQLDB4309F6C2C4.ENGINE - dataFileCache open start
2013-12-19 09:26:44,066 [HSQLDB Connection @199f91c] INFO  hsqldb.db.HSQLDB4309F6C2C4.ENGINE - dataFileCache open end
Insert: 847
Select #1: 486
Select #2: 397
Select #3: 388
Select #4: 419
Select #5: 372
Select #6: 372
Select #7: 375
Select #8: 415
Select #9: 380
Select #10: 379
2013-12-19 09:26:48,420 [main] INFO  hsqldb.db.HSQLDB4309F6C2C4.ENGINE - dataFileCache commit start
2013-12-19 09:26:48,602 [main] INFO  hsqldb.db.HSQLDB4309F6C2C4.ENGINE - dataFileCache commit end
2013-12-19 09:26:48,617 [main] INFO  hsqldb.db.HSQLDB4309F6C2C4.ENGINE - Database closed
[Server@169e11]: Initiating shutdown sequence...
[Server@169e11]: Shutdown sequence completed in 0 ms.
[Server@169e11]: 2013-12-19 09:26:48.718 SHUTDOWN : System.exit() was not called
Close: 306
--- HSQLDB (in-process) ---
[Server@111a3a4]: [Thread[main,5,main]]: setSilent(true)
2013-12-19 09:26:48,751 [main] INFO  hsqldb.db.HSQLDB4309F6D82C.ENGINE - Checkpoint start
2013-12-19 09:26:48,751 [main] INFO  hsqldb.db.HSQLDB4309F6D82C.ENGINE - checkpointClose start
2013-12-19 09:26:48,758 [main] INFO  hsqldb.db.HSQLDB4309F6D82C.ENGINE - checkpointClose end
2013-12-19 09:26:48,758 [main] INFO  hsqldb.db.HSQLDB4309F6D82C.ENGINE - Checkpoint end - txts: 1
[Server@111a3a4]: Initiating startup sequence...
[Server@111a3a4]: Server socket opened successfully in 3 ms.
[Server@111a3a4]: Database [index=0, id=1, db=file:db/testdb2, alias=name] opened sucessfully in 0 ms.
[Server@111a3a4]: Startup sequence completed in 3 ms.
[Server@111a3a4]: 2013-12-19 09:26:48.763 HSQLDB server 2.3.0 is online on port 1234
[Server@111a3a4]: To close normally, connect and execute SHUTDOWN SQL
[Server@111a3a4]: From command line, use [Ctrl]+[C] to abort abruptly
Init: 141
GetConnection: 0
Drop: 0
Create: 1
2013-12-19 09:26:48,924 [main] INFO  hsqldb.db.HSQLDB4309F6D82C.ENGINE - dataFileCache open start
2013-12-19 09:26:48,926 [main] INFO  hsqldb.db.HSQLDB4309F6D82C.ENGINE - dataFileCache open end
Insert: 301
Select #1: 7
Select #2: 6
Select #3: 6
Select #4: 6
Select #5: 6
Select #6: 6
Select #7: 6
Select #8: 6
Select #9: 6
Select #10: 6
2013-12-19 09:26:49,284 [main] INFO  hsqldb.db.HSQLDB4309F6D82C.ENGINE - dataFileCache commit start
2013-12-19 09:26:49,506 [main] INFO  hsqldb.db.HSQLDB4309F6D82C.ENGINE - dataFileCache commit end
2013-12-19 09:26:49,517 [main] INFO  hsqldb.db.HSQLDB4309F6D82C.ENGINE - Database closed
[Server@111a3a4]: Initiating shutdown sequence...
[Server@111a3a4]: Shutdown sequence completed in 0 ms.
[Server@111a3a4]: 2013-12-19 09:26:49.619 SHUTDOWN : System.exit() was not called
Close: 396
---        MySQL        ---
Init: 0
GetConnection: 200
Drop: 7
Create: 2
Insert: 4255
Select #1: 201
Select #2: 200
Select #3: 199
Select #4: 202
Select #5: 202
Select #6: 200
Select #7: 203
Select #8: 199
Select #9: 199
Select #10: 202
Close: 2
DBPerformanceTester.java (text/x-java, 6.2 KB)
package dbtest;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.hsqldb.Database;
import org.hsqldb.DatabaseManager;
import org.hsqldb.DatabaseURL;
import org.hsqldb.Server;
import org.hsqldb.jdbcDriver;
import org.hsqldb.persist.HsqlDatabaseProperties;
import org.hsqldb.persist.HsqlProperties;

public class DBPerformanceTester {
	
	private static Server server;
	
	private static String user = "user";
	private static String password = "user";
	
	private static Connection connection;
	private static Statement statement;
	
	private static String[] data;
	
	private static void prepareRandomData(int count) {
		data = new String[count];
		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < count; i++) {
			builder.setLength(0);
			for (int c = 0; c < 255; c++) {
				char ch = (char) ('A' + (Math.random()*26));
				builder.append(ch);
			}
			data[i] = builder.toString();
		}
	}
	
	private static void cleanupHSQLDBFiles() {
		new File("db").mkdir();
		new File("db/testdb2.data").delete();
		new File("db/testdb2.log").delete();
		new File("db/testdb2.properties").delete();
		new File("db/testdb2.script").delete();
	}

	private static void init(boolean hsql) throws Exception {
		long time = System.currentTimeMillis();
		if (hsql) {
			Class.forName(jdbcDriver.class.getCanonicalName());
			
			cleanupHSQLDBFiles();
			
			server = new Server();
			
			server.setSilent(true);
			server.setNoSystemExit(true);
			server.setPort(1234);
			server.setDatabasePath(0, "file:db/testdb2");
			server.setDatabaseName(0, "name");
			
			HsqlProperties props = new HsqlProperties();
			props.setProperty(HsqlDatabaseProperties.hsqldb_tx, "MVCC");
			props.setProperty(HsqlDatabaseProperties.hsqldb_tx_level, "READ_COMMITTED");
			props.setProperty(HsqlDatabaseProperties.hsqldb_tx_conflict_rollback, true);
			props.setProperty(HsqlDatabaseProperties.hsqldb_default_table_type, "CACHED");
			props.setProperty(HsqlDatabaseProperties.hsqldb_cache_rows, 1*1024*1024);
			props.setProperty(HsqlDatabaseProperties.hsqldb_cache_size, 1*1024*1024);
			props.setProperty("user", user);
			props.setProperty("password", password);
			
			DatabaseManager.getDatabase(DatabaseURL.S_FILE, "db/testdb2", server, props);
			
			server.start();
		} else {
			Class.forName("com.mysql.jdbc.Driver");
		}
		time = System.currentTimeMillis() - time;
		System.out.println("Init: " + time);
	}
	
	private static Connection getConnection(boolean hsql, int mode) throws SQLException {
		long time = System.currentTimeMillis();
		Connection result;
		if (hsql) {
			if (mode == 0) {
				result = DriverManager.getConnection("jdbc:hsqldb:hsql://127.0.0.1:1234/name", user, password);
			} else {
				result = DriverManager.getConnection("jdbc:hsqldb:file:db/testdb2", user, password);
			}
		} else {
			result = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", user, password);
		}
		time = System.currentTimeMillis() - time;
		System.out.println("GetConnection: " + time);
		return result;
	}
	
	private static void drop() throws Exception {
		long time = System.currentTimeMillis();
		statement.execute("DROP TABLE IF EXISTS Test");
		time = System.currentTimeMillis() - time;
		System.out.println("Drop: " + time);
	}
	
	private static void create(boolean hsql) throws Exception {
		long time = System.currentTimeMillis();
		String create;
		if (hsql) {
			create = "CREATE TABLE Test(Id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, t1 VARCHAR(255), t2 VARCHAR(255), t3 VARCHAR(255), t4 VARCHAR(255))";
		} else {
			create = "CREATE TABLE Test(Id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY, t1 VARCHAR(255), t2 VARCHAR(255), t3 VARCHAR(255), t4 VARCHAR(255))";
		}
		statement.execute(create);
		time = System.currentTimeMillis() - time;
		System.out.println("Create: " + time);
	}
	
	private static void insert(int count) throws Exception {
		long time = System.currentTimeMillis();
		connection.setAutoCommit(false);
		PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO Test (t1,t2,t3,t4) VALUES (?, ?, ?, ?)");
		for (int i = 0; i < count; i++) {
			preparedStatement.setString(1, data[i]);
			preparedStatement.setString(2, data[i]);
			preparedStatement.setString(3, data[i]);
			preparedStatement.setString(4, data[i]);
			preparedStatement.addBatch();
		}
		preparedStatement.executeBatch();
		connection.commit();
		connection.setAutoCommit(true);
		time = System.currentTimeMillis() - time;
		System.out.println("Insert: " + time);
	}
	
	private static void select(int selects) throws Exception {
		for(int r = 0; r < selects; r++) {
			long time = System.currentTimeMillis();
			
			ResultSet rs = connection.createStatement().executeQuery("SELECT * FROM Test");
			while (rs.next());
			rs.close();

			time = System.currentTimeMillis() - time;
			System.out.println("Select #" + (r+1) + ": " + time);
		}
	}
	
	private static void close(boolean hsql) throws Exception {
		long time = System.currentTimeMillis();
		if (!connection.getAutoCommit()) {
			connection.commit();
		}
		connection.close();
		
		if (hsql) {
			if (server != null && server.getState() == 1) {
				server.shutdownWithCatalogs(Database.CLOSEMODE_NORMAL);
				server.stop();
				server = null;
			}
		}
		time = System.currentTimeMillis() - time;
		System.out.println("Close: " + time);
	}

	private static void run(boolean hsql, int mode, int count, int selects) throws Exception {
		init(hsql);

		connection = getConnection(hsql, mode);
		statement = connection.createStatement();

		drop();

		create(hsql);

		insert(count);

		select(selects);

		close(hsql);
	}
	
	public static void main(String[] args) throws Exception {
		int count = 20000;
		int selects = 10;
		
		prepareRandomData(count);

		System.out.println("---   HSQLDB (server)   ---");
		run(true, 0, count, selects);
		System.out.println("--- HSQLDB (in-process) ---");
		run(true, 1, count, selects);
		System.out.println("---        MySQL        ---");
		run(false, 0, count, selects);
	}

}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.