In which part of the application is it more appropriate to reorder an array (database, server application, client code)?

Asked

Viewed 97 times

4

Suppose, in a query to the MYSQL database, I need to take the last 1000 dice released, however, within these displayed results, his order must be growing (and not decreasing, as would happen in the ORDER BY ID DESC Limit 1000).

The response to such data shall be via JSON, through function json_encode of PHP, at a specific url, where who will capture and process this data is the Javascript.

The question I have is this: Considering examples 1, 2 and 3 and considering that the data should be formatted in reverse order, which of these would be the most appropriate way, taking into account the memory consumption and data processing speed ?

1 - MYSQL

SELECT * FROM (
    SELECT id, pessoa_id, chat 
    FROM chat 
    ORDER BY id DESC LIMIT 1000
) AS reverse_chat ORDER BY id ASC

2 - PHP

return Response::json(array_reverse($chat)); //lembrando que é $chat é um array com 1000 dados

3 - Javascript

$.ajax({
    url: 'ajax_response',
    dataType: 'json',
    data: {pessoa_id: pessoa_id},
    success: function(response){
        // response também possui 1000 dados
        var html = _.template(tpl_chat, {chat: response.reverse()});

    }
})
  • 5

    The option 1 is the best.

  • 5

    It does not seem to me to be a constructive question in its present form. Perhaps it can be improved to make clear its real need. If you will query in the database do with SQL. If you have to do in a browser, do with JS and if you are between one and the other, do with PHP.

  • 1

    The earlier the data are processed and organized the better for the efficiency of the application. In your practical case if the data comes from a database and you can sort them when performing the query, no doubt vote on @Sergio’s comment.

  • @bigown, as mentioned in the example, I am assuming that I will use all three : Mysql in query, PHP in response, javascript in processing. I just want to know which one of these is faster and which will waste less memory.

  • 3

    Exactly, there is no real question. Therefore the question is not constructive. You want to know something that has no relevance. At least this is not clear in the question and the comment did not improve that perception.

  • 1

    @Wallacemaxters how so use all three? The question is on what is the best language to sort the information, right? and that only needs to be done once, right? then the answer is definitely option 1. Or I’m getting it wrong?

  • The title was a call for closing votes. "Better" is very much a matter of opinion. I switched to something more technical and feasible, ok?

  • Thank you, @Renan

Show 3 more comments

1 answer

4


Take care of the ordering as soon as possible. In your case, this means in the database.

taking into account memory consumption and data processing speed

It is not possible to take this into account in a generic way, it will depend on the characteristics of your application and your BD. If you have performance problems with this query and cannot optimize it (by reformulating the query or creating indexes), move to the next level (in this case, PHP).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.