___ ___ erkimt Improves performance - Ruby on Rails ______ qstntxt ___

I have 3 tables:

[users] 1 ----------- n [evaluations] n --------- 1 [items]

Where evaluation can be positive (valuation: true ) or negative (valuation: false ), I need a method that returns all items that the user liked and did not like ... For this I did the following method in the user model:

%pre%

The same principle was adopted for the method for the dislike method

The idea is to do this with many users, the problem is that it is taking a long time to return the data. Is there any way to improve the performance of this code snippet? Any structure that I can keep on the server, because this query is executed many times more than once per user ...

    
______ azszpr28800 ___

I had the idea of trying to do directly in the bank to simplify the process and modified the function to:

%pre%

I've had an improvement of 0.2 seconds in the average ... I'm putting it as an answer because I do not think it will improve more than that, but if someone wants to prove me to the contrary the answer will be very welcome!     

______ azszpr36666 ___

Would not it be less expensive if you paginated? You can use the library link so it will return the paged items I believe will shorten the search time.

    
___

1

I have 3 tables:

[users] 1 ----------- n [evaluations] n --------- 1 [items]

Where evaluation can be positive (valuation: true ) or negative (valuation: false ), I need a method that returns all items that the user liked and did not like ... For this I did the following method in the user model:

# Retorna o numero de Likes
def likes
  itens = Array.new
  self.avaliacoes.where( avaliacao: true ).includes(:item).each do |avaliacao|
    itens << avaliacao.item
  end
  return itens
end

The same principle was adopted for the method for the dislike method

The idea is to do this with many users, the problem is that it is taking a long time to return the data. Is there any way to improve the performance of this code snippet? Any structure that I can keep on the server, because this query is executed many times more than once per user ...

    
asked by anonymous 13.08.2014 / 02:10

2 answers

2

I had the idea of trying to do directly in the bank to simplify the process and modified the function to:

def likes
  query = "SELECT itm.id
           FROM   avaliacoes av INNER JOIN itens itm ON av.item_id = itm.id
           WHERE  avaliacao is true and
                usuario_id = #{self.id}"
  Usuario.connection.execute(query).to_set
end

I've had an improvement of 0.2 seconds in the average ... I'm putting it as an answer because I do not think it will improve more than that, but if someone wants to prove me to the contrary the answer will be very welcome!     

13.08.2014 / 04:31
0

Would not it be less expensive if you paginated? You can use the library link so it will return the paged items I believe will shorten the search time.

    
12.10.2014 / 19:08