引言:MongoDB是基于文档型的数据库,为了比较它与Mysql的区别,笔者写了两个小例子来测试,二者的读性能
环境:个人电脑/Window10 、未对mysql和mongodb做其余的配置(采用的是默认配置)
数据:数据表有10万条数据
测试:随机读取其中1万条数据,计算读取时间
Mysql测试代码:
package intelligence;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class MutilMysqlTest {
public MutilMysqlTest() {
}
@BeforeClass
public static void setUpClass() throws Exception {
}
@AfterClass
public static void tearDownClass() throws Exception {
}
@Test
public void test() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
//final CountDownLatch latch=new CountDownLatch(10);
List threads = new ArrayList();
final Connection connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/haimi", "root", "root");
long start = System.currentTimeMillis();
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(new Runnable() {
public void run() {
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
// System.out.println(String.format("线程%d查询开始", Thread
// .currentThread().getId()));
Random ran = new Random();
String sql = "select * from items where item_id = ?";
statement = connection.prepareStatement(sql);
for(int i = 0; i < 1000; i++){
int key = ran.nextInt(10000);
statement.setInt(1, key);
resultSet = statement.executeQuery();
}
// System.out.println(String.format("线程%d查询结束", Thread
// .currentThread().getId()));
} catch (Exception ex) {
} finally {
try {
connection.close();
} catch (SQLException ex) {
}
}
}
});
thread.start();
threads.add(thread);
}
for (Thread thread : threads) {
thread.join();
}
System.out.println("查询10000条记录时长:"+(System.currentTimeMillis()-start));
// connection.close();
}
}
测试结果为
MongoDB测试代码
package intelligence;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
public class MutilMongoTest {
public MutilMongoTest() {
}
@BeforeClass
public static void setUpClass() throws Exception {
}
@AfterClass
public static void tearDownClass() throws Exception {
}
@Test
public void test() throws Exception {
// 增大Mongo驱动的并发连接数量
Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("haimi");
final DBCollection coll = db.getCollection("new");
List threads = new ArrayList();
long start = System.currentTimeMillis();
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(new Runnable() {
Random random = new Random();
BasicDBObject query = new BasicDBObject();
public void run() {
// System.out.println(String.format("线程%d查询开始", Thread
// .currentThread().getId()));
for(int i = 0; i < 1000; i++){
int key = random.nextInt(10000);
query.put("_id", key);
DBObject result = coll.findOne(query);
}
// System.out.println(String.format("线程%d查询结束", Thread
// .currentThread().getId()));
}
});
thread.start();
threads.add(thread);
}
for (Thread thread : threads) {
thread.join();
}
System.out.println("查询10000条记录时长:"+(System.currentTimeMillis()-start));
}
}
测试结果为