I've study sqlite and just wanted to ask: speed record in the file of a prime number in a simple database from one column is 4 seconds. is it ok or i have some software/hardware problems? 256 notes writing above 4 seconds in SQLite and (2ms for not-mysql file writing ) I just can't believe that sqlite is so slow in write.
my code for comparison sqlite db work and file db work:
package demo_SQLite;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
public class demo_SQLite {
private static Connection conn;
private static Statement state;
private static File db = new File("datafile");
public static void main(String[] args) throws IOException {
Debug("Start DB!");
run();
}
private static void run() throws IOException {
try {
if (!db.exists()) {
db.createNewFile();
}
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:" + db);
state = conn.createStatement();
state.executeUpdate("create table if not exists a(id integer,"
+ "time INT,"
+ "primary key (id));");
PreparedStatement prep = conn.prepareStatement("insert into a values(?,?)");
for (int i = 0; i < 257; i++) {
int l = new Random().nextInt();
prep.setInt(2, l);
prep.execute();
}
prep.close();
state.close();
Debug("Done! (256)");
file();
} catch (Exception ex) {
Logger.getLogger(Demo_MySQL.class.getName()).log(Level.SEVERE, null, ex);
}
}
private static void file() {
try {
Debug("Start File!");
File file = new File("filebase");
DataOutputStream dos = null;
dos = new DataOutputStream
(new BufferedOutputStream(new FileOutputStream(file)));
int r = 0;
for (int i = 0; i < 257; ++i) {
r = new Random().nextInt();
dos.writeInt(r);
}
dos.flush();
dos.close();
Debug("Done! (256)");
} catch (IOException ex) {
Logger.getLogger(Demo_MySQL.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void Debug(String msg) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
Date date = new Date();
System.out.println("[" + sdf.format(date) + "][DEBUG]: " + msg);
}
}
result:
run:
[07:53:49.829][DEBUG]: Start DB!
[07:53:53.223][DEBUG]: Done! (256)
[07:53:53.223][DEBUG]: Start File!
[07:53:53.253][DEBUG]: Done! (256)
BUILD SUCCESSFUL (total time: 5 seconds)
4 seconds! wow! it's awesome! please just tell me - this is normal or i do wrong something?
UPDATED.
seems, problem with auto commit. as i read somewhere just now, "By default, each INSERT statement is its own transaction. But if you surround multiple INSERT statements with BEGIN...COMMIT then all the inserts are grouped into a single transaction. The time needed to commit the transaction is amortized over all the enclosed insert statements and so the time per insert statement is greatly reduced."
i've just disable auto commit - corrected code and new result:
package demo_sqlite;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
public class demo_SQLite {
private static Connection conn;
private static Statement state;
private static File db = new File("datafile");
public static void main(String[] args) throws IOException {
Debug("Start DB!");
run();
}
private static void run() throws IOException {
try {
if (!db.exists()) {
db.createNewFile();
}
//run db driver
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:" + db);
state = conn.createStatement();
//check table
state.executeUpdate("create table if not exists a(id integer,"
+ "time INT,"
+ "primary key (id));");
conn.setAutoCommit(false);
// clear table if table not clear
state.execute("DELETE FROM a");
conn.commit();
//make needed notes for db
PreparedStatement prep = conn.prepareStatement("insert into a values(?,?)");
for (int i = 0; i < 256; ++i) {
int l = new Random().nextInt();
prep.setInt(2, l);
prep.execute();
}
conn.commit();
prep.close();
//reading notes for chek
List<Integer> notes = new LinkedList<Integer>();
ResultSet res = state.executeQuery("select * from a");
while (res.next()) { int n = res.getInt("time"); notes.add(n); }
res.close();
state.close();
Debug("Done! ("+ notes.size() + ")");
//write to a file for comparing the write speed
Debug("Start File!");
File file = new File("filebase");
DataOutputStream dos = null;
dos = new DataOutputStream
(new BufferedOutputStream(new FileOutputStream(file)));
int r = 0;
for (int i = 0; i < 256; ++i) {
r = new Random().nextInt();
dos.writeInt(r);
}
dos.flush();
dos.close();
Debug("Done! (256)");
//done! see log for result
} catch (Exception ex) { }
}
public static void Debug(String msg) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
Date date = new Date();
System.out.println("[" + sdf.format(date) + "][DEBUG]: " + msg);
}
}
result:
run:
[09:01:36.890][DEBUG]: Start DB!
[09:01:37.052][DEBUG]: Done! (256)
[09:01:37.053][DEBUG]: Start File!
[09:01:37.054][DEBUG]: Done! (256)
BUILD SUCCESSFUL (total time: 0 seconds)
thanks for all for a answers! but if you have some considerations about this, please, welcome