Mysql和Mongodb的读性能比较

本文通过在Windows10环境下,使用默认配置的MySQL和MongoDB,进行10万条数据的随机读取测试,对比两者在读性能上的差异。测试结果显示了不同数据库在数据检索速度上的表现。

引言: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();
	}
}

测试结果为
查询10000条记录时长:1140




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));

    }
}

测试结果为
查询10000条记录时长:1726

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值