admin 管理员组

文章数量: 1184232


2024年4月14日发(作者:spoilceapp)

Mongodb VS Mysql 查询性能

admin , 2010/07/16 12:34 , mongodb , 评论(0) , 阅读(54) , Via 本站原创 大 | 中 | 小

最近想用 Mongodb 做些存储层的事情。目前主要用在 id 查询或 ids 查询(如 mysql 的

in 查询)。新的方案能否上线还要看性能。现在把 mongodb 与 mysql 来对比下。

环境:同一台机器,装有 mongodb 1.4, mysql;内存 4g;两个数据库都只有 id 的唯一搜索;

数据量 120w 左右,5w 个查询。对它们进行 id 或 ids 查询。

mongodb

total time

11954ms, 00:11,

954

35885ms, 00:35,

885

63714ms, 00:1:

3,714

-

-

avg time/per

query

0ms, 00,0

0ms, 00,0

1ms, 00,1

5ms, 00,5

10ms, 00,10

613

107274ms, 00:1:4

7,274

186398ms, 00:3:6,

398

-

-

total time

78613ms, 00:1:18,

mysql

avg time/per

query

1ms, 00,1

2ms, 00,2

3ms, 00,3

11ms, 00,11

22ms, 00,22

1 id/per query

10 id/per query

20 id/per query

5 thread, 20 id/per

query

10 thread, 20 id/per

query

从上表看:

每次查一个 id 的查询,在基本相同条件下 mongodb 速度是 mysql 的 7 倍。

每次查多个 id (10/20)的查询,mongodb 比 mysql 快 2 倍。

多线程下查多个 id,mongodb 比 mysql 快 1 倍。

从上面的测试来看:主要用在 id 查询或 ids 查询的应用中 mongodb 要优越 mysql。

在插入这些数据的时候,mongodb 更加快于 mysql,插入120w 到 mongodb 用 83159ms,

->00:1:23,159 就够了。mysql 有几十分钟。

当我在实际应用中(kw 级数据量),使用 mongodb 速度没有 mysql 好。原因可能有,mysql

的机器好,mongodb 使用不够得当。继续调整和优化程序吧……

附 mongodb 的 in 查询:

代码

BasicDBObject q = new BasicDBObject();

BasicDBObject in = new BasicDBObject();

void createQ(Integer[] ids) {

 ("id", in);



 ("$in", ids);



 }

上一篇:Mongodb VS Mysql 查询性能,测试了 mongodb 与 mysql 的查询性能。结果说明

mongodb 性能可以, 可以代替 mysql 来使用。

但是这个测试都是在百万级别,我的场景在 KW 级别。所以还要对 mongodb 在 kw 级别

下测试效果。

我测试环境是 4G 内存(有好些内存被其它程序占用),2kw 数据,查询随机生成 id(一

次查询 20 个id)。

在这样的环境中测试不理想,比较失望。平均一次查询 500ms(比 mysql 还差,特别是在

并发查询下,性能较差。很底的吞吐量)。查看其索引大小(用 () 可以查询):

2kw 数据中有 1.1G 左右的索引,存储的数据在 11G 左右。

测试过程中发现 iowait 占 50% 左右,看来还是 io 的瓶颈。还看到 mongodb 使用的内存

不多(小于索引的大小,看来这机器不足够来测试)。

换了个有可用 6G 内存的机器。在 50 个并发下,可以达到平均 100 ms 左右,算比较满

意,但是并发好像能力不够强。但这个性能不能由我控制,还由机器的可用内存控制。原因

就是 mongodb 没有指定可占用的内存大小,它把所有空闲内存当缓存使用,既是优点也是

缺点:优点--可以最大限度提升性能;缺点--容易受其它程序干扰(占用了它的缓存)。由我

测试来看,它抢占内存的能力不强。mongodb 是用内存映射文件 vmm,官方的说明:

Memory Mapped Storage Engine

This is the current storage engine for MongoDB, and it uses memory-mapped files for all disk I/O.

Using this strategy, the operating system's virtual memory manager is in charge of caching. This

has several implications:

 There is no redundancy between file system cache and database cache: they are one and

the same.

 MongoDB can use all free memory on the server for cache space automatically without

any configuration of a cache size.

 Virtual memory size and resident size will appear to be very large for the mongod process.


本文标签: 查询 性能 内存