|
本帖最后由 chszs 于 2011-12-19 17:29 编辑
基于jieforest的代码进一步对达梦数据库测试。
1、环境条件
· 软件版本
达梦数据库:DM v7.1
MySQL:5.1.44 community 32位
· 其它
其它软硬件相同,在同一台台式机,Windows XP操作系统上运行。
MySQL 5.1与DM 7.1均未做优化,都为默认配置。
2、数据库脚本
MySQL:- CREATE DATABASE /*!32312 IF NOT EXISTS*/`dm7` /*!40100 DEFAULT CHARACTER SET latin1 COLLATE latin1_bin */;
- USE `dm7`;
- CREATE TABLE `forum` ( `fid` int(11) NOT NULL AUTO_INCREMENT, `link` varchar(256) COLLATE latin1_bin DEFAULT NULL, `title` varchar(256) COLLATE latin1_bin DEFAULT NULL, `author` varchar(128) COLLATE latin1_bin DEFAULT NULL, `publishtime` date DEFAULT NULL, `content` varchar(20000) COLLATE latin1_bin DEFAULT NULL, `createtime` timestamp NULL DEFAULT NULL, PRIMARY KEY (`fid`)) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin;
复制代码 DM:- --创建表空间
- CREATE TABLESPACE dm7 DATAFILE 'dm7.DBF' size 60;
- --创建模式
- CREATE SCHEMA social;
- --创建表
- --CREATE social.forum
- CREATE TABLE social.forum
- (
- fid INT IDENTITY(1,1) PRIMARY KEY,
- links VARCHAR(256),
- title VARCHAR(256),
- author VARCHAR(128),
- publishtime DATE,
- content VARCHAR(8188),
- createtime TIMESTAMP
- )STORAGE(ON dm7);
复制代码 3、程序(借用jieforest的部分代码)- // MYSQL
- 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.Date;
- public class MySQLtest {
- private static String driver = "com.mysql.jdbc.Driver";
- private static String db_url = "jdbc:mysql://localhost:3306/dm7";
- private static String user = "sysdba";
- private static String password = "adminadmin";
- public void insertTest(int times) throws SQLException {
- PreparedStatement stmt = null;
- Connection conn = null;
- try {
- Class.forName(driver);
- conn = DriverManager.getConnection(db_url, user, password);
- String sql = "INSERT INTO forum(link,title,author,publishtime,content,createtime) VALUES(?,?,?,?,?,?)";
-
- for(int i=0; i<times; i++){
- stmt = conn.prepareStatement(sql);
-
- String link = Utils.randomString(256);
- String title = Utils.randomString(256);
- String author = Utils.randomString(128);
- long publishtime = new Date().getTime();
- String content = Utils.randomString(1024);
- java.sql.Timestamp createtime = new java.sql.Timestamp(new Date().getTime());
-
- stmt.setString(1, link);
- stmt.setString(2, title);
- stmt.setString(3, author);
- stmt.setDate(4, new java.sql.Date(publishtime));
- stmt.setString(5, content);
- stmt.setTimestamp(6, createtime);
- stmt.executeUpdate();
- }
- } catch (Exception e) {
- System.err.println("插入失败: " + e.getMessage());
- e.printStackTrace();
- } finally {
- stmt.close();
- conn.close();
- }
- }
-
- public int queryTest(String str) throws SQLException{
- int number = 0;
- Statement stmt = null;
- Connection conn = null;
- ResultSet rs = null;
- try {
- Class.forName(driver);
- try {
- conn = DriverManager.getConnection(db_url, user, password);
- } catch (SQLException e) {
- throw new SQLException("Connect to DM7 Database Server Error : "
- + e.getMessage());
- }
- String sql = "SELECT COUNT(*) AS total FROM forum WHERE content LIKE '%" + str + "%'";
- stmt = conn.createStatement();
-
- rs = stmt.executeQuery(sql);
- if(rs.next()){
- number = rs.getInt("total");
- }
- } catch (Exception e) {
- System.err.println("查询失败: " + e.getMessage());
- e.printStackTrace();
- } finally {
- rs.close();
- stmt.close();
- conn.close();
- }
- return number;
- }
-
- public int deleteTest(String str) throws SQLException{
- int number = 0;
- Statement stmt = null;
- Connection conn = null;
- try {
- Class.forName(driver);
- try {
- conn = DriverManager.getConnection(db_url, user, password);
- } catch (SQLException e) {
- throw new SQLException("Connect to DM7 Database Server Error : "
- + e.getMessage());
- }
- String sql = "DELETE FROM forum WHERE content LIKE '%" + str + "%'";
- stmt = conn.createStatement();
-
- number = stmt.executeUpdate(sql);
- } catch (Exception e) {
- System.err.println("删除失败: " + e.getMessage());
- e.printStackTrace();
- } finally {
- stmt.close();
- conn.close();
- }
- return number;
- }
-
- public void updateTest(String str) throws SQLException{
- Statement stmt = null;
- Connection conn = null;
- try {
- Class.forName(driver);
- try {
- conn = DriverManager.getConnection(db_url, user, password);
- } catch (SQLException e) {
- throw new SQLException("Connect to DM7 Database Server Error : "
- + e.getMessage());
- }
- String sql = "UPDATE forum SET link = \"" + str + "\"";
- stmt = conn.createStatement();
-
- stmt.executeUpdate(sql);
- } catch (Exception e) {
- System.err.println("修改失败: " + e.getMessage());
- e.printStackTrace();
- } finally {
- stmt.close();
- conn.close();
- }
- }
-
- public static void main(String[] args) throws SQLException{
- MySQLtest test = new MySQLtest();
-
- long d1 = new Date().getTime();
- test.insertTest(100);
- long d2 = new Date().getTime();
- System.out.println("插入100条记录所需的时间为: " + (d2-d1) + "毫秒");
- int n = test.queryTest("AA");
- long d3 = new Date().getTime();
- System.out.println("遍历100条记录, 查询CONTENT字段包含‘AA’得到记录数:" + n + "条");
- System.out.println("遍历100条记录, 查询CONTENT字段包含‘AA’记录所需的时间为: " + (d3-d2) + "毫秒");
- test.updateTest("http://www.google.com.hk");
- long d4 = new Date().getTime();
- System.out.println("修改100条记录所需的时间为: " + (d4-d3) + "毫秒");
-
- d1 = new Date().getTime();
- test.insertTest(1000);
- d2 = new Date().getTime();
- System.out.println("插入1000条记录所需的时间为: " + (d2-d1) + "毫秒");
- n = test.queryTest("AA");
- d3 = new Date().getTime();
- System.out.println("遍历1000条记录, 查询CONTENT字段包含‘AA’得到记录数:" + n + "条");
- System.out.println("遍历1000条记录, 查询CONTENT字段包含‘AA’记录所需的时间为: " + (d3-d2) + "毫秒");
- test.updateTest("http://www.google.com.hk");
- d4 = new Date().getTime();
- System.out.println("修改1000条记录所需的时间为: " + (d4-d3) + "毫秒");
-
- d1 = new Date().getTime();
- test.insertTest(5000);
- d2 = new Date().getTime();
- System.out.println("插入5000条记录所需的时间为: " + (d2-d1) + "毫秒");
- n = test.queryTest("AA");
- d3 = new Date().getTime();
- System.out.println("遍历5000条记录, 查询CONTENT字段包含‘AA’得到记录数:" + n + "条");
- System.out.println("遍历5000条记录, 查询CONTENT字段包含‘AA’记录所需的时间为: " + (d3-d2) + "毫秒");
- test.updateTest("http://www.google.com.hk");
- d4 = new Date().getTime();
- System.out.println("修改5000条记录所需的时间为: " + (d4-d3) + "毫秒");
-
- d1 = new Date().getTime();
- n = test.deleteTest("AA");
- d2 = new Date().getTime();
- System.out.println("删除所有记录中CONTENT字段包含‘AA’的记录所需的时间为: " + (d2-d1) + "毫秒");
- System.out.println("删除的记录数:" + n + "条");
-
- }
- }
- // DM7
- 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.Date;
- public class DMtest {
- private static String driver = "dm7.jdbc.driver.DmDriver";
- private static String db_url = "jdbc:dm://localhost:5236";
- private static String user = "SYSDBA";
- private static String password = "adminadmin";
-
- public void insertTest(int times) throws SQLException {
- PreparedStatement stmt = null;
- Connection conn = null;
- try {
- Class.forName(driver);
- try {
- conn = DriverManager.getConnection(db_url, user, password);
- } catch (SQLException e) {
- throw new SQLException("Connect to DM Server Error : "
- + e.getMessage());
- }
- String sql = "INSERT INTO social.forum(links,title,author,publishtime,content,createtime) VALUES(?,?,?,?,?,?)";
-
- for(int i=0; i<times; i++){
- stmt = conn.prepareStatement(sql);
-
- String links = Utils.randomString(256);
- String title = Utils.randomString(256);
- String author = Utils.randomString(128);
- long publishtime = new Date().getTime();
- String content = Utils.randomString(1024);
- java.sql.Timestamp createtime = new java.sql.Timestamp(new Date().getTime());
-
- stmt.setString(1, links);
- stmt.setString(2, title);
- stmt.setString(3, author);
- stmt.setDate(4, new java.sql.Date(publishtime));
- stmt.setString(5, content);
- stmt.setTimestamp(6, createtime);
- stmt.executeUpdate();
- }
- } catch (Exception e) {
- System.err.println("插入失败: " + e.getMessage());
- e.printStackTrace();
- } finally {
- stmt.close();
- conn.close();
- }
- }
-
- public int queryTest(String str) throws SQLException{
- int number = 0;
- Statement stmt = null;
- Connection conn = null;
- ResultSet rs = null;
- try {
- Class.forName(driver);
- try {
- conn = DriverManager.getConnection(db_url, user, password);
- } catch (SQLException e) {
- throw new SQLException("Connect to DM7 Database Server Error : "
- + e.getMessage());
- }
- String sql = "SELECT COUNT(*) AS TOTALS FROM social.forum WHERE content LIKE '%" + str + "%'";
- stmt = conn.createStatement();
-
- rs = stmt.executeQuery(sql);
- if(rs.next()){
- number = rs.getInt("TOTALS");
- }
- } catch (Exception e) {
- System.err.println("查询失败: " + e.getMessage());
- e.printStackTrace();
- } finally {
- rs.close();
- stmt.close();
- conn.close();
- }
- return number;
- }
-
- public int deleteTest(String str) throws SQLException{
- int number = 0;
- Statement stmt = null;
- Connection conn = null;
- try {
- Class.forName(driver);
- try {
- conn = DriverManager.getConnection(db_url, user, password);
- } catch (SQLException e) {
- throw new SQLException("Connect to DM7 Database Server Error : "
- + e.getMessage());
- }
- String sql = "DELETE FROM social.forum WHERE content LIKE '%" + str + "%'";
- stmt = conn.createStatement();
-
- number = stmt.executeUpdate(sql);
- } catch (Exception e) {
- System.err.println("删除失败: " + e.getMessage());
- e.printStackTrace();
- } finally {
- stmt.close();
- conn.close();
- }
- return number;
- }
-
- public void updateTest(String str) throws SQLException{
- Statement stmt = null;
- Connection conn = null;
- try {
- Class.forName(driver);
- try {
- conn = DriverManager.getConnection(db_url, user, password);
- } catch (SQLException e) {
- throw new SQLException("Connect to DM7 Database Server Error : "
- + e.getMessage());
- }
- String sql = "UPDATE social.forum SET links = '" + str + "'";
- stmt = conn.createStatement();
-
- stmt.executeUpdate(sql);
- } catch (Exception e) {
- System.err.println("修改失败: " + e.getMessage());
- e.printStackTrace();
- } finally {
- stmt.close();
- conn.close();
- }
- }
-
- public static void main(String[] args) throws SQLException{
- DMtest test = new DMtest();
-
- long d1 = new Date().getTime();
- test.insertTest(100);
- long d2 = new Date().getTime();
- System.out.println("插入100条记录所需的时间为: " + (d2-d1) + "毫秒");
- int n = test.queryTest("AA");
- long d3 = new Date().getTime();
- System.out.println("遍历100条记录, 查询CONTENT字段包含‘AA’得到记录数:" + n + "条");
- System.out.println("遍历100条记录, 查询CONTENT字段包含‘AA’记录所需的时间为: " + (d3-d2) + "毫秒");
- test.updateTest("http://www.google.com.hk");
- long d4 = new Date().getTime();
- System.out.println("修改100条记录所需的时间为: " + (d4-d3) + "毫秒");
-
- d1 = new Date().getTime();
- test.insertTest(1000);
- d2 = new Date().getTime();
- System.out.println("插入1000条记录所需的时间为: " + (d2-d1) + "毫秒");
- n = test.queryTest("AA");
- d3 = new Date().getTime();
- System.out.println("遍历1000条记录, 查询CONTENT字段包含‘AA’得到记录数:" + n + "条");
- System.out.println("遍历1000条记录, 查询CONTENT字段包含‘AA’记录所需的时间为: " + (d3-d2) + "毫秒");
- test.updateTest("http://www.google.com.hk");
- d4 = new Date().getTime();
- System.out.println("修改1000条记录所需的时间为: " + (d4-d3) + "毫秒");
-
- d1 = new Date().getTime();
- test.insertTest(5000);
- d2 = new Date().getTime();
- System.out.println("插入5000条记录所需的时间为: " + (d2-d1) + "毫秒");
- n = test.queryTest("AA");
- d3 = new Date().getTime();
- System.out.println("遍历5000条记录, 查询CONTENT字段包含‘AA’得到记录数:" + n + "条");
- System.out.println("遍历5000条记录, 查询CONTENT字段包含‘AA’记录所需的时间为: " + (d3-d2) + "毫秒");
- test.updateTest("http://www.google.com.hk");
- d4 = new Date().getTime();
- System.out.println("修改5000条记录所需的时间为: " + (d4-d3) + "毫秒");
-
- d1 = new Date().getTime();
- n = test.deleteTest("AA");
- d2 = new Date().getTime();
- System.out.println("删除所有记录中CONTENT字段包含‘AA’的记录所需的时间为: " + (d2-d1) + "毫秒");
- System.out.println("删除的记录数:" + n + "条");
- }
- }
复制代码- // DM7
- 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.Date;
- public class DMtest {
- private static String driver = "dm7.jdbc.driver.DmDriver";
- private static String db_url = "jdbc:dm://localhost:5236";
- private static String user = "SYSDBA";
- private static String password = "adminadmin";
-
- public void insertTest(int times) throws SQLException {
- PreparedStatement stmt = null;
- Connection conn = null;
- try {
- Class.forName(driver);
- try {
- conn = DriverManager.getConnection(db_url, user, password);
- } catch (SQLException e) {
- throw new SQLException("Connect to DM Server Error : "
- + e.getMessage());
- }
- String sql = "INSERT INTO social.forum(links,title,author,publishtime,content,createtime) VALUES(?,?,?,?,?,?)";
-
- for(int i=0; i<times; i++){
- stmt = conn.prepareStatement(sql);
-
- String links = Utils.randomString(256);
- String title = Utils.randomString(256);
- String author = Utils.randomString(128);
- long publishtime = new Date().getTime();
- String content = Utils.randomString(1024);
- java.sql.Timestamp createtime = new java.sql.Timestamp(new Date().getTime());
-
- stmt.setString(1, links);
- stmt.setString(2, title);
- stmt.setString(3, author);
- stmt.setDate(4, new java.sql.Date(publishtime));
- stmt.setString(5, content);
- stmt.setTimestamp(6, createtime);
- stmt.executeUpdate();
- }
- } catch (Exception e) {
- System.err.println("插入失败: " + e.getMessage());
- e.printStackTrace();
- } finally {
- stmt.close();
- conn.close();
- }
- }
-
- public int queryTest(String str) throws SQLException{
- int number = 0;
- Statement stmt = null;
- Connection conn = null;
- ResultSet rs = null;
- try {
- Class.forName(driver);
- try {
- conn = DriverManager.getConnection(db_url, user, password);
- } catch (SQLException e) {
- throw new SQLException("Connect to DM7 Database Server Error : "
- + e.getMessage());
- }
- String sql = "SELECT COUNT(*) AS TOTALS FROM social.forum WHERE content LIKE '%" + str + "%'";
- stmt = conn.createStatement();
-
- rs = stmt.executeQuery(sql);
- if(rs.next()){
- number = rs.getInt("TOTALS");
- }
- } catch (Exception e) {
- System.err.println("查询失败: " + e.getMessage());
- e.printStackTrace();
- } finally {
- rs.close();
- stmt.close();
- conn.close();
- }
- return number;
- }
-
- public int deleteTest(String str) throws SQLException{
- int number = 0;
- Statement stmt = null;
- Connection conn = null;
- try {
- Class.forName(driver);
- try {
- conn = DriverManager.getConnection(db_url, user, password);
- } catch (SQLException e) {
- throw new SQLException("Connect to DM7 Database Server Error : "
- + e.getMessage());
- }
- String sql = "DELETE FROM social.forum WHERE content LIKE '%" + str + "%'";
- stmt = conn.createStatement();
-
- number = stmt.executeUpdate(sql);
- } catch (Exception e) {
- System.err.println("删除失败: " + e.getMessage());
- e.printStackTrace();
- } finally {
- stmt.close();
- conn.close();
- }
- return number;
- }
-
- public void updateTest(String str) throws SQLException{
- Statement stmt = null;
- Connection conn = null;
- try {
- Class.forName(driver);
- try {
- conn = DriverManager.getConnection(db_url, user, password);
- } catch (SQLException e) {
- throw new SQLException("Connect to DM7 Database Server Error : "
- + e.getMessage());
- }
- String sql = "UPDATE social.forum SET links = '" + str + "'";
- stmt = conn.createStatement();
-
- stmt.executeUpdate(sql);
- } catch (Exception e) {
- System.err.println("修改失败: " + e.getMessage());
- e.printStackTrace();
- } finally {
- stmt.close();
- conn.close();
- }
- }
-
- public static void main(String[] args) throws SQLException{
- DMtest test = new DMtest();
-
- long d1 = new Date().getTime();
- test.insertTest(100);
- long d2 = new Date().getTime();
- System.out.println("插入100条记录所需的时间为: " + (d2-d1) + "毫秒");
- int n = test.queryTest("AA");
- long d3 = new Date().getTime();
- System.out.println("遍历100条记录, 查询CONTENT字段包含‘AA’得到记录数:" + n + "条");
- System.out.println("遍历100条记录, 查询CONTENT字段包含‘AA’记录所需的时间为: " + (d3-d2) + "毫秒");
- test.updateTest("http://www.google.com.hk");
- long d4 = new Date().getTime();
- System.out.println("修改100条记录所需的时间为: " + (d4-d3) + "毫秒");
-
- d1 = new Date().getTime();
- test.insertTest(1000);
- d2 = new Date().getTime();
- System.out.println("插入1000条记录所需的时间为: " + (d2-d1) + "毫秒");
- n = test.queryTest("AA");
- d3 = new Date().getTime();
- System.out.println("遍历1000条记录, 查询CONTENT字段包含‘AA’得到记录数:" + n + "条");
- System.out.println("遍历1000条记录, 查询CONTENT字段包含‘AA’记录所需的时间为: " + (d3-d2) + "毫秒");
- test.updateTest("http://www.google.com.hk");
- d4 = new Date().getTime();
- System.out.println("修改1000条记录所需的时间为: " + (d4-d3) + "毫秒");
-
- d1 = new Date().getTime();
- test.insertTest(5000);
- d2 = new Date().getTime();
- System.out.println("插入5000条记录所需的时间为: " + (d2-d1) + "毫秒");
- n = test.queryTest("AA");
- d3 = new Date().getTime();
- System.out.println("遍历5000条记录, 查询CONTENT字段包含‘AA’得到记录数:" + n + "条");
- System.out.println("遍历5000条记录, 查询CONTENT字段包含‘AA’记录所需的时间为: " + (d3-d2) + "毫秒");
- test.updateTest("http://www.google.com.hk");
- d4 = new Date().getTime();
- System.out.println("修改5000条记录所需的时间为: " + (d4-d3) + "毫秒");
-
- d1 = new Date().getTime();
- n = test.deleteTest("AA");
- d2 = new Date().getTime();
- System.out.println("删除所有记录中CONTENT字段包含‘AA’的记录所需的时间为: " + (d2-d1) + "毫秒");
- System.out.println("删除的记录数:" + n + "条");
- }
- }
复制代码 4、结果
结论1:DM数据库的INSERT插入INSERT语句执行特别快,速度为MySQL的7、8倍;
结论2:DM数据库的UPDATE修改语句执行也比较快,速度为MySQL的2、3倍,随着数据量的增加,与MySQL的速度差异逐步下降;
结论3:DM数据库的DELETE删除语句也比MySQL快,大约是1.5倍的速度;
结论4:DM数据库的查询SELECT语句与MySQL差别不大。
总结:增删改查语句是数据库操作最常用的语句,对基本语句的测试有助于客观看待达梦数据库。
|
|