I
I
iGarett2016-07-14 07:42:30
PostgreSQL
iGarett, 2016-07-14 07:42:30

Why do I have such a big difference in response time from the server (postgreSQL Vs MongoDB)?

NODE.JS + EXPRESS
POSTGRESQL + BLUEBIRD
MONGODB + MONGOOSE

POSTGRESQL :

function getAllCategory(req, res, next) {
    db.any('SELECT * FROM pcategory WHERE isdeleted = FALSE')
        .then(function(data) {
            res.json({
                status: 'success',
                data: data,
                message: 'GET all DATA'
            });
        })
        .catch(function(err) {
            return next(err);
        });
}

GET /api/v1/category/ 200 6.460 ms - 191
GET /api/v1/category/ 200 3.310 ms - 191
GET /api/v1/category/ 200 3.850 ms - 191
GET /api/v1/category/ 200 2.459 ms - 191
GET /api/v1/category/ 200 2.598 ms - 191
GET /api/v1/category/ 200 4.042 ms - 191
GET /api/v1/category/ 200 3.694 ms - 191
GET /api/v1/category/ 200 2.319 ms - 191
GET /api/v1/category/ 200 2.611 ms - 191
GET /api/v1/category/ 200 2.227 ms - 191
GET /api/v1/category/ 200 2.698 ms - 191

MONGODB:
function getAllCategory(req, res, next) {
    model.CategoryModel.find({$where:"this.isDeleted == false"}, function(err, category) {
        if (err) return next(err);
        res.json(category);
    });
}

GET /api/v2/category/ 200 43.354 ms - 32230
GET /api/v2/category/ 200 99.844 ms - 32230
GET /api/v2/category/ 200 55.187 ms - 32230
GET /api/v2/category/ 200 49.486 ms - 32230
GET /api/v2/category/ 200 65.210 ms - 32230
GET /api/v2/category/ 200 51.937 ms - 32230
GET /api/v2/category/ 200 45.078 ms - 32230
GET /api/v2/category/ 200 50.517 ms - 32230

POSTGRESQL :
function getAllImageFromCategory(req, res, next) {
    db.any(
        `SELECT p.image_id, p.image_title, p.image_url, p.rating, p.desc_short, p.desc_full FROM pcategory
            JOIN nn_category_image nn
                ON nn.pcategory_id = pcategory.pcategory_id
            JOIN image p
                ON nn.image_id = p.image_id
         WHERE pcategory.pcategory_id =${req.params.category_id} AND p.isdeleted = FALSE`)
    .then(function(data) {
        res.json({data: data});
    })
    .catch(function(err) {
        return next(err);
    });
}

GET /api/v1/category/1/image 200 7.692 ms - 25232
GET /api/v1/category/1/image 200 7.433 ms - 25232
GET /api/v1/category/1/image 200 8.680 ms - 25232
GET /api/v1/category/1/image 200 8.676 ms - 25232
GET /api/v1/category/1/image 200 5.980 ms - 25232
GET /api/v1/category/1/image 200 9.330 ms - 25232
GET /api/v1/category/1/image 200 9.242 ms - 25232
GET /api/v1/category/1/image 200 9.438 ms - 25232
GET /api/v1/category/1/image 200 8.978 ms - 25232

MONGODB :
function getAllImageFromCategory(req, res, next) {
    model.CategoryModel.findById(req.params.category_id, function(err, category) {
        if (err) {
            return res.json({ message: err.message });
        }
        res.json(category.image);
    });
}

GET /api/v2/category/578706e28862784508cb7a1b/image 200 62.497 ms - 31312
GET /api/v2/category/578706e28862784508cb7a1b/image 200 82.299 ms - 31312
GET /api/v2/category/578706e28862784508cb7a1b/image 200 74.558 ms - 31312
GET /api/v2/category/578706e28862784508cb7a1b/image 200 50.366 ms - 31312
GET /api/v2/category/578706e28862784508cb7a1b/image 200 65.073 ms - 31312
GET /api/v2/category/578706e28862784508cb7a1b/image 200 45.323 ms - 31312
GET /api/v2/category/578706e28862784508cb7a1b/image 200 48.513 ms - 31312
GET /api/v2/category/578706e28862784508cb7a1b/image 200 53.180 ms - 31312

And all this on localhost.
Tell me, should it be like this or do I need to write requests differently on mongo?
Yes, and on postgreSQL, it’s not so hot what queries turned out, but the time is different at times.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya Shatokhin, 2016-07-14
@iShatokhin

So much nonsense was written in the answers and comments ...
The problem here is exclusively in the crooked request. You are executing arbitrary js code in the request body, this will always be VERY slow.
A normal query would look like this:
Change and compare speed again. Always avoid $where, it is only used for complex logic, also such queries do NOT use indexes.

M
maxt888, 2016-07-14
@maxt888

Here it is well written https://habrahabr.ru/post/197590/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question