这两天测了几个数据库的速度,请求大神指教!
发布于 6 年前 作者 klausgao 4645 次浏览 来自 分享

我的原文在这里 https://github.com/c58/marsdb/issues/7 贴出来: those days I am looking for good leveldb-base complete database. And I find marsdb and linvodb3. I don‘t like linvodb3 cos it don’t support promise. so first I choose marsdb. I did a try to insert 1000 records with some data, and found it very slow. then I tried sqlite3 and sequelize, even it is faster than marsdb. today I tried linvodb3, it is really very fast. so I am confused, maybe I make some mistakes on marsdb? below are the codes:

//-------------------------
var co = require('co');
var randomize = require('randomatic');
var Q = require('q');

//-----------LinvoDB----------
const LinvoDB = require("linvodb3");
LinvoDB.dbPath = './dbBenchmark/linvodb';
let modelName = "doc";
let schema = {}; // Non-strict always, can be left empty
let options = {};
let Doc = new LinvoDB(modelName, schema, options); // New model; Doc is the constructor

function linvodb3Insert(_index) {
    var deferred = Q.defer();
    Doc.insert(
        {
            string1: 'LinvoDB' + randomize('A', 20),
            number1: _.now(),
            indexID: _index
        },
        function (err, newDoc) {
            if (err) {
                deferred.reject(new Error(err.message));
            }
            else {
                deferred.resolve(newDoc);
            }
        });
    return deferred.promise;
}

router.get('/linvodb3Insert', function (req, res) {
    let promises = [];
    for (let i = 0; i < 1000; i++) {
        promises.push(linvodb3Insert(i));
    }
    Q.all(promises).then(function (results) {
        return res.json({msg: 'success'});
    }, function (err) {
        return res.json({msg: 'err'});
    });
});

router.get('/linvodb3Count', function (req, res) {
    Doc.count({}, function (err, count) {
        return res.json({msg: 'success', count: count});
    });
});
//-----------LinvoDB----------

//------marsdb--------
let Collection = require("marsdb").Collection;
let LevelStorageManager = require("marsdb-levelup");
LevelStorageManager.defaultStorageLocation('./dbBenchmark/marsdb');
Collection.defaultStorageManager(LevelStorageManager);
router.get('/marsdbInsert', function (req, res) {
    co(function*() {
        for (let i = 0; i < 1000; i++) {
            const posts = new Collection('posts');
            yield posts.insert(
                {
                    string1: 'MarsDB' + randomize('A', 20),
                    number1: _.now(),
                    indexID: i
                }
            );
        }

        return res.json({msg: 'success'});
    }).catch(function (err) {
        return res.json({msg: 'err'});
    });
});
//---------marsdb------------

//------------sqlite----------
let Sequelize = require('sequelize');
let sequelize = new Sequelize('database', 'username', 'password', {
    dialect: 'sqlite',
    pool: {
        max: 5,
        min: 0,
        idle: 10000
    },
    logging: null,
    storage: './dbBenchmark/sqlite/test.db'
});
let User = sequelize.define(
    'user',
    {
        'indexID': {
            'type': Sequelize.BIGINT(10),
            'allowNull': false
        },
        'string1': {
            'type': Sequelize.STRING(10),
            'allowNull': false
        },
        'number1': {
            'type': Sequelize.BIGINT(20),
            'allowNull': false
        }
    }
);
User.sync();

router.get('/sqliteInsert', function (req, res) {
    co(function*() {
        for (let i = 0; i < 1000; i++) {
            yield User.create({
                string1: 'sqlite' + randomize('A', 20),
                number1: _.now(),
                indexID: i
            });
        }

        return res.json({msg: 'success'});
    }).catch(function (err) {
        return res.json({msg: 'err'});
    });
});
//------------sqlite----------

//--------mongodb----------
let mongoose = require('mongoose');
//多数据集支持
let connection = require(path.join(__dirname, '..', '..', 'dbConnection_chihuo.js')).connection;
let schema1 = mongoose.Schema;

let testCollectionName = 'dbBenchmark';
let testSchema = new schema1({
    //_id
    string1: String,
    number1: Number,
    indexID: Number
});
testSchema.index({_id: 1, indexID: 1});
let tester = connection.model(testCollectionName, testSchema);
router.get('/mongoInsert', function (req, res) {
    co(function*() {
        for (let i = 0; i < 1000; i++) {
            yield tester.create({
                string1: 'mongo' + randomize('A', 20),
                number1: _.now(),
                indexID: i
            });
        }
        return res.json({msg: 'success'});
    }).catch(function (err) {
        return res.json({msg: 'err'});
    });
});
//--------mongodb----------

the benchmark is: sqlite: 8658ms marsdb: can’t finish, time out. so I only try 100 inserts, and the result is 13927. linvodb3:325ms mongodb:966ms

4 回复

这是要测试什么??单条插入速度??

@TimothyJin 其实只是要选择一个好的数据库。只测试了单条插入速度。

后续更新,和marsdb的作者反馈后,他及时更新了库,修改了一些bug,后面提速到了968ms。 但是相对linvodb3,还是差了3倍。 所以已经选定了linvodb

@klausgao 这应该还达不到数据库的性能问题吧,写法上你是一次写一条数据,写1000次,难道不应该是一次写1000条数据,写一次吗,1000条数据的水准,明显搞不垮sql

回到顶部