Guest User

Untitled

a guest
Jun 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. ## I'm doing a lot of these, where X is an integer
  2.  
  3. INSERT INTO deviations(pivot_id, deviant_id, value)
  4. SELECT
  5. X as pivot_id,
  6. ratings2.content_id as deviant_id,
  7. AVG(ratings.rate - ratings2.rate) as value
  8. FROM ratings
  9. INNER JOIN ratings as ratings2
  10. ON ratings.viewer_id = ratings2.viewer_id
  11. AND ratings.viewer_type = ratings2.viewer_type
  12. AND ratings.content_id = X
  13. AND ratings.content_id < ratings2.content_id
  14. GROUP BY deviant_id
  15.  
  16. ## However I also tried doing a single query for all those IDs:
  17.  
  18. INSERT INTO deviations(pivot_id, deviant_id, value)
  19. SELECT
  20. ratings.content_id as pivot_id,
  21. ratings2.content_id as deviant_id,
  22. AVG(ratings.rate - ratings2.rate) as value
  23. FROM ratings
  24. INNER JOIN ratings as ratings2
  25. ON ratings.viewer_id = ratings2.viewer_id
  26. AND ratings.viewer_type = ratings2.viewer_type
  27. AND ratings.content_id < ratings2.content_id
  28. GROUP BY pivot_id, deviant_id
  29.  
  30. ## Results in seconds
  31. | MANY QUERIES | ONE BIG QUERY |
  32. Default my.cnf values: | 194 | 267
  33. 8MB for max_heap_table_size
  34. and tmp_table_size: | 367 | 298 |
  35. 265MB for max_heap_table_size
  36. and tmp_table_size: 389 seconds | 358 | 78 |
  37.  
  38. How come using more memory is worse???
Add Comment
Please, Sign In to add comment