屹砾

屹砾技术博客,记录生活点滴。
Email/QQ/MSN/GTalk: eli.wuhan@gmail.com

留言簿

积分与排名

ELi - 屹砾网络

JavaSE & JavaEE

JavaTesting

Linux & Unix

OpenSource

收藏链接

时事点评

阅读排行榜

评论排行榜

MySQL InnoDB/Falcon 两种存储引擎数据插入性能的测试

 

package eli.test.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.text.NumberFormat;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import junit.framework.TestCase;

/**
 * 对MySQL InnoDB/Falcon 两种存储引擎数据插入性能的测试。
 * 目前,MySQL 5.0GA版本仍然使用的是 InnoDB 存储引擎,
 * 而不久之前 InnoDB 这家挪威公司已经被 ORACLE 收购了,
 * 所以 MySQL 需要开发一个自己的存储引擎。通过测试,发现
 * 名为 Falcon 的这一个新的存储引擎性能强大,足以与 ORACLE
 * 的存储性能相媲美。于是,个人猜测 InnoDB 可能没有使用到
 * 缓存技术,而是直接把数据存到存储设备上,而 Falcon 则是
 * 把数据存储在内存中,在特定的时间存储到存储设备上。 Falcon
 * 的存储原理就与 ORACLE 的存储原理类似或者相同了。 希望
 * Falcon 存储引擎能够早日成熟,让 MySQL 更加强大起来。
 * 
 * 
@author <a href="mailto:eli.wuhan@gmail.com">屹砾</a>
 
*/

public class MySQLEngineInsertSpeed extends TestCase {
    
public static final Log log = LogFactory
            .getLog(MySQLEngineInsertSpeed.
class);
    
private Connection connect = null;
    
private String driver = "com.mysql.jdbc.Driver";
    
private String url = "jdbc:mysql:///test";
    
private String username = "root";
    
private String password = "1";
    
private NumberFormat numberFormat = NumberFormat.getNumberInstance();
    
public static final String INNODB = "`insert_speed_innodb`";
    
public static final String FALCON = "`insert_speed_falcon`";

    
public void test() throws Exception {
        
int times = 1000;
        insertTesting(times);
        insertTesting(times);
        insertTesting(times);
    }
;

    
public void insertTesting(int times) throws Exception {
        
long t1 = System.currentTimeMillis();
        insert(INNODB, times);
        
long t2 = System.currentTimeMillis();
        insert(FALCON, times);
        
long t3 = System.currentTimeMillis();
        log.info(
"innodb " + times + " times spend: "
                
+ numberFormat.format(t2 - t1) + "ms");
        log.info(
"falcon " + times + " times spend: "
                
+ numberFormat.format(t3 - t2) + "ms");
    }
;

    
public void insert(String table, int times) throws Exception {
        
for (int i = times; i > 0; i--{
            String sql 
= "INSERT INTO " + table
                    
+ "(`last_name`,`first_name`,`duty`,`cellphone`,"
                    
+ "`housephone`,`telephone`,`office_fax`,"
                    
+ "`home_address`,`office_address`,`remark`)"
                    
+ "values(?,?,?,?,?,?,?,?,?,?)";
            PreparedStatement statement 
= connect.prepareStatement(sql);
            String time 
= String.valueOf(System.currentTimeMillis());
            statement.setString(
1, time);
            statement.setString(
2, time);
            statement.setString(
3, time);
            statement.setString(
4, time);
            statement.setString(
5, time);
            statement.setString(
6, time);
            statement.setString(
7, time);
            statement.setString(
8, time);
            statement.setString(
9, time);
            statement.setString(
10, time);
            statement.execute();
            statement.close();
        }

    }


    
protected void createTable() throws Exception {
        dropTable();
        String create 
= "`id` bigint(20) NOT NULL AUTO_INCREMENT,"
                
+ "`last_name` varchar(256) DEFAULT NULL,"
                
+ "`first_name` varchar(256) DEFAULT NULL,"
                
+ "`duty` varchar(256) DEFAULT NULL,"
                
+ "`cellphone` varchar(256) DEFAULT NULL,"
                
+ "`housephone` varchar(256) DEFAULT NULL,"
                
+ "`telephone` varchar(256) DEFAULT NULL,"
                
+ "`office_fax` varchar(256) DEFAULT NULL,"
                
+ "`home_address` varchar(256) DEFAULT NULL,"
                
+ "`office_address` varchar(256) DEFAULT NULL,"
                
+ "`remark` text, PRIMARY KEY (`id`)";
        String createInnoDB 
= "CREATE TABLE " + INNODB + " (" + create
                
+ ")ENGINE=InnoDB DEFAULT CHARSET=utf8";
        String createFalcon 
= "CREATE TABLE " + FALCON + " (" + create
                
+ ")ENGINE=Falcon DEFAULT CHARSET=utf8";
        Statement statement 
= connect.createStatement();
        statement.execute(createInnoDB);
        statement.execute(createFalcon);
        statement.close();
    }


    
protected void dropTable() throws Exception {
        String dropInnoDB 
= "DROP TABLE IF EXISTS " + INNODB;
        String dropFalcon 
= "DROP TABLE IF EXISTS " + FALCON;
        Statement statement 
= connect.createStatement();
        statement.execute(dropInnoDB);
        statement.execute(dropFalcon);
        statement.close();
    }


    @Override
    
protected void setUp() throws Exception {
        Class.forName(driver);
        connect 
= DriverManager.getConnection(url, username, password);
        createTable();
        numberFormat.setMinimumIntegerDigits(
6);
        numberFormat.setMaximumIntegerDigits(
6);
        numberFormat.setParseIntegerOnly(
true);
    }


    @Override
    
protected void tearDown() throws Exception {
        dropTable();
        connect.close();
    }

}

// <控制台输出的内容>
// 2007-6-30 10:23:46 eli.test.db.MySQLEngineInsertSpeed insertTesting
// 信息: innodb 1000 times spend: 011,687ms
// 2007-6-30 10:23:46 eli.test.db.MySQLEngineInsertSpeed insertTesting
// 信息: falcon 1000 times spend: 001,250ms
// 2007-6-30 10:23:57 eli.test.db.MySQLEngineInsertSpeed insertTesting
// 信息: innodb 1000 times spend: 009,719ms
// 2007-6-30 10:23:57 eli.test.db.MySQLEngineInsertSpeed insertTesting
// 信息: falcon 1000 times spend: 001,047ms
// 2007-6-30 10:24:08 eli.test.db.MySQLEngineInsertSpeed insertTesting
// 信息: innodb 1000 times spend: 010,125ms
// 2007-6-30 10:24:08 eli.test.db.MySQLEngineInsertSpeed insertTesting
// 信息: falcon 1000 times spend: 001,109ms

posted on 2008-06-30 17:12 屹砾 阅读(126) 评论(1)  编辑  收藏 所属分类: Database

评论

# re: MySQL InnoDB/Falcon 两种存储引擎数据插入性能的测试 2008-09-05 09:47 hightower

myisam 大概也可以达到这个级别的  回复  更多评论   


标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
 
 
相关链接:
网站导航: