NodeJS与带Mysql数据库的Laravel哪个性能更好?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NodeJS与带Mysql数据库的Laravel哪个性能更好?相关的知识,希望对你有一定的参考价值。

我正在开发一个带有mysql数据库的REST后端,现在我需要在哪种服务器端语言更好地消耗服务器资源(CPU和Ram)之间做出选择。

比如说,我有一个POST端点(注册一个新的用户账户)和另一个GET端点(以JSON格式获取用户信息),我希望我的网站有很高的流量,哪种编程语言会消耗更多的服务器资源?

我在网上搜索了NodejS和Laravel的比较, 我发现一些网站显示了Laravel的基准测试和其他网站显示NodeJS更好.

答案

对于基本的getset DB操作,无论选择哪种语言都不会有明显的时间。

根据非性能标准选择语言,并在出现具体性能问题时解决。

由于你已经标记了负载平衡,我假设你的应用已经可以水平扩展了。

另一答案

我自己建了一个这样的sql脚本编写器,对于我的项目来说,它的速度相当快,因为我之前的php后端太慢了(对于我的情况来说

const mysqlLib = require('mysql');
const ENV = require('../configs/env').ENV;
const database = ENV.database;
const mysql = mysqlLib.createPool(database);

function executeQuery(query) 
    return new Promise((resolve, reject) => 
        mysql.query(query, (err, result, fields) => 
            if (err) 
                console.log(query);
                return reject(err);
            

            resolve(result);
        );
    );


function executeQueryGetFirst(query) 
    return new Promise((resolve, reject) => 
        mysql.query(query, (err, result, fields) => 
            if (err) 
                console.log(query);
                return reject(err);
            

            result = JSON.parse(JSON.stringify(result));

            if (result.length == 0) 
                resolve(null);
             else 
                resolve(result[0]);
            
        );
    );


var Database = 
    getById(table, id) 
        var query = '';
        query += 'SELECT * FROM ' + table + ' ';
        query += 'WHERE Id = ' + id;
        query += ' LIMIT 1';

        return executeQueryGetFirst(query);
    ,
    getAllById(table, id) 
        var query = '';
        query += 'SELECT * FROM ' + table + ' ';
        query += 'WHERE Id = ' + id;

        return executeQuery(query);
    ,
    getByAttributes(table, attributes) 
        var query = 'SELECT * FROM ' + table;
        var whereQuery = '';

        for (prop in attributes) 
            if (whereQuery != '') 
                whereQuery += ' AND ';
            

            if (attributes[prop] == null) 
                whereQuery += prop + ' IS NULL';
             else 
                whereQuery += prop + ' = "' + attributes[prop] + '"';
            
        

        query += ' WHERE ' + whereQuery;
        query += ' LIMIT 1';

        return executeQueryGetFirst(query);
    ,
    getAllByAttributes(table, attributes) 
        var query = 'SELECT * FROM ' + table;
        var whereQuery = '';

        for (prop in attributes) 
            if (whereQuery != '') 
                whereQuery += ' AND ';
            

            if (attributes[prop] == null) 
                whereQuery += prop + ' IS NULL';
             else 
                whereQuery += prop + ' = "' + attributes[prop] + '"';
            
        

        query += ' WHERE ' + whereQuery;

        return executeQuery(query);
    ,
    getAll(table) 
        var query = 'SELECT * FROM ' + table;

        return executeQuery(query);
    ,
    add(table, attributes) 
        var insertName = '(Id';
        var insertData = '(NULL';

        for (prop in attributes) 
            var value = attributes[prop];
            if (value != null) 
                value = '"' + value + '"';
            

            insertName += ', ' + prop;
            insertData += ', ' + value;
        

        insertName += ')';
        insertData += ')';

        var query = 'INSERT INTO ' + table + ' ' + insertName + ' VALUES ' + insertData;

        return executeQuery(query);
    ,
    updateByID(table, id, attributes) 
        var query = 'UPDATE ' + table;
        var setQuery = '';

        for (prop in attributes) 
            if (setQuery != '') 
                setQuery += ', ';
            

            setQuery += prop + ' = "' + attributes[prop] + '"';
        

        query += ' SET ' + setQuery + ' WHERE Id = ' + id;

        return executeQuery(query);
    ,
    updateByAttributes(table, findAttributes, attributes) 
        var query = 'UPDATE ' + table;
        var setQuery = '';
        var whereQuery = '';

        for (prop in attributes) 
            if (setQuery != '') 
                setQuery += ', ';
            

            setQuery += prop + ' = "' + attributes[prop] + '"';
        

        for (prop in findAttributes) 
            if (whereQuery != '') 
                whereQuery += ', ';
            

            whereQuery += prop + ' = "' + findAttributes[prop] + '"';
        

        query += ' SET ' + setQuery + ' WHERE ' + whereQuery;

        return executeQuery(query);
    ,
    deleteById(table, id) 
        var query = 'DELETE FROM ' + table + ' WHERE Id = ' + id;

        return executeQuery(query);
    ,
    deleteByAttributes(table, attributes) 
        var query = 'DELETE FROM ' + table;
        var whereQuery = '';

        for (prop in attributes) 
            if (whereQuery != '') 
                whereQuery += ' AND ';
            

            whereQuery += prop + ' = "' + attributes[prop] + '"';
        

        query += ' WHERE ' + whereQuery;

        return executeQuery(query);
    


Database.executeQuery = executeQuery;
Database.executeQueryGetFirst = executeQueryGetFirst;

exports.Database = Database;

环境配置

var ENV = 
    isDev: true,
    database: 
        connectionLimit: 10,
        host: 'localhost',
        user: 'root',
        password: '',
        database: 'your_database'
    


exports.ENV = ENV;
exports.ENV.port = ENV.isDev ? 15001 : 8887;

以上是关于NodeJS与带Mysql数据库的Laravel哪个性能更好?的主要内容,如果未能解决你的问题,请参考以下文章

从前端存储记录的用户数据

nodejs使用Sequelize框架操作数据库

Spring JavaConfig与带注释的类[重复]

从 Laravel(NodeJS)外部推送到 Laravel 队列

Laravel与Socket.io Nodejs共享会话

laravel--为什么属性在模型中没有定义,却取出来了值,这些属性哪里来的