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

4

Let's suppose that in a MYSQL database query, I need to pick up the last 1000 data that was posted, but within those results, the order must be growing (not descending, as would happen in ORDER BY ID DESC Limit 1000 ).

The response of this data will be via JSON , using the json_encode function of PHP , in a specific url, where who will capture and process this data is < strong> Javascript .

The question I have is the following: Considering examples 1, 2, and 3, and given that the data must be formatted in reverse order, which would be the most appropriate way, taking into account 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()});

    }
})
    
asked by anonymous 16.10.2014 / 13:41

1 answer

4

Take care of the ordination as soon as possible. In your case, that means in the database.

  

Considering memory consumption and data processing speed

Generally, this can not be taken into account, it will depend on the characteristics of your application and your DB. If you have performance problems with this query and can not optimize it (by reformulating the query or creating indexes), move to the next level (in this case, PHP).

    
16.10.2014 / 15:22