Skip to content

Instantly share code, notes, and snippets.

@sebastianrothbucher
Created February 17, 2016 14:01
Show Gist options
  • Save sebastianrothbucher/81245a52634a91f5c159 to your computer and use it in GitHub Desktop.
Save sebastianrothbucher/81245a52634a91f5c159 to your computer and use it in GitHub Desktop.
Another SQLite performance test: 3 GB w/ blobs (VS w/out)
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class SQLiteJDBCBlob {
public static void main(String args[]) throws SQLException, ClassNotFoundException, IOException {
// load a huge file (10MB)
File fle = new File(System.getProperty("user.home"), "Documents/fullrefman.pdf");
byte[] bytes = new byte[(int) fle.length()];
InputStream fleInput = new FileInputStream(fle);
int readSoFar = 0;
int readNow;
while ((readNow = fleInput.read(bytes, readSoFar, Math.min(200000, (bytes.length - readSoFar)))) >= 0 && readSoFar < bytes.length) {
readSoFar += readNow;
System.out.println("> read " + readSoFar + " bytes");
}
fleInput.close();
if (readSoFar != bytes.length) {
throw new IllegalStateException("Not all has been read!");
}
// and work
Connection c = null;
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:myblobdb.sqlite");
c.createStatement().executeUpdate("CREATE TABLE if not exists lorem (num NUMBER, info TEXT, fle BLOB)");
PreparedStatement stmt = c.prepareStatement("INSERT INTO lorem VALUES (?, ?, ?)");
for (int i = 0; i < 100; i++) {
System.out.println(" > " + i);
stmt.setInt(1, i);
stmt.setString(2, "Ipsum " + i);
// and the Blob
stmt.setBytes(3, bytes);
stmt.executeUpdate();
}
ResultSet res = c.createStatement().executeQuery("SELECT rowid AS id, num, info FROM lorem order by num");
long begin = System.currentTimeMillis();
while (res.next()) {
System.out.println(res.getString("id") + ": " + res.getString("info") + " (" + res.getInt("num") + ")");
}
System.out.println("Took " + (System.currentTimeMillis() - begin) + "ms");
c.close();
}
}
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class SQLiteJDBCPlain {
public static void main(String args[]) throws SQLException, ClassNotFoundException, IOException {
// and work
Connection c = null;
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:myplaindb.sqlite");
c.createStatement().executeUpdate("CREATE TABLE if not exists lorem (num NUMBER, info TEXT)");
PreparedStatement stmt = c.prepareStatement("INSERT INTO lorem VALUES (?, ?)");
for (int i = 0; i < 100; i++) {
System.out.println(" > " + i);
stmt.setInt(1, i);
stmt.setString(2, "Ipsum " + i);
stmt.executeUpdate();
}
ResultSet res = c.createStatement().executeQuery("SELECT rowid AS id, num, info FROM lorem order by num");
long begin = System.currentTimeMillis();
while (res.next()) {
System.out.println(res.getString("id") + ": " + res.getString("info") + " (" + res.getInt("num") + ")");
}
System.out.println("Took " + (System.currentTimeMillis() - begin) + "ms");
c.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment