太失望,Redis比H2慢很多很多
2018-06-12
插一百萬行record, Redis比H2慢太多,H2只需要七秒,redis用了40秒,為什麼用c++寫出來的redis會比用java寫出來的h2慢這麼多的? NoSQL不是比傳統database爽快的嗎?
<?
$redis = new Redis();
$redis->connect('localhost', 6379);
$redis->auth('password');
echo "Connection to server sucessfully\n";
echo "Server is running: " . $redis->ping()."\n";
//echo $redis->get('name')."\n";
//echo $redis->hget('car', 'price')."\n";
echo date('i:s')."\n";
for ($x=0;$x<1000000;$x++){
// if ($x%10000==0)
// echo $x.' > '.date('i:s')."\n";
$redis->hset('car'.$x, 'price', $x);
}
echo date('i:s')."\n";
?>
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 java.util.*;
import org.h2.tools.DeleteDbFiles;
// H2 Database Example
public class H2FileDatabaseExample {
private static final String DB_DRIVER = "org.h2.Driver";
private static final String DB_CONNECTION = "jdbc:h2:~/test";
private static final String DB_USER = "sa";
private static final String DB_PASSWORD = "";
public static void main(String[] args) throws Exception {
try {
// delete the H2 database named 'test' in the user home directory
//DeleteDbFiles.execute("~", "test", true);
System.out.println(new Date());
insertWithStatement();
System.out.println(new Date());
//DeleteDbFiles.execute("~", "test", true);
} catch (SQLException e) {
e.printStackTrace();
}
}
// H2 SQL Statement Example
private static void insertWithStatement() throws SQLException {
Connection connection = getDBConnection();
Statement stmt = null;
try {
connection.setAutoCommit(false);
stmt = connection.createStatement();
stmt.execute("delete from PERSON");
// stmt.execute("CREATE TABLE PERSON(id int primary key, name varchar(255))");
for (int x=0;x<1000000;x++){
stmt.execute("INSERT INTO PERSON(id, name) VALUES("+x+", 'Anju"+x+"')");
}
/* ResultSet rs = stmt.executeQuery("select * from PERSON");
System.out.println("H2 Database inserted through Statement");
while (rs.next()) {
System.out.println("Id "+rs.getInt("id")+" Name "+rs.getString("name"));
}
*/
stmt.close();
connection.commit();
} catch (SQLException e) {
System.out.println("Exception Message " + e.getLocalizedMessage());
} catch (Exception e) {
e.printStackTrace();
} finally {
connection.close();
}
}
private static Connection getDBConnection() {
Connection dbConnection = null;
try {
Class.forName(DB_DRIVER);
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER,
DB_PASSWORD);
return dbConnection;
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return dbConnection;
}
}
