Successfully reported this slideshow.

Postgresql and ror

239 views

Published on

Published in: Technology
  • Be the first to comment

Postgresql and ror

  1. 1. POSTGRESQL & RORTuesday, January 15, 13
  2. 2. SPEED ...Tuesday, January 15, 13
  3. 3. SPEED (OR LACK OF)Tuesday, January 15, 13
  4. 4. SPEED (OR LACK OF) 90 seconds for a pageTuesday, January 15, 13
  5. 5. #1 LOCATION 112 000 locationsTuesday, January 15, 13
  6. 6. #1 LOCATIONTuesday, January 15, 13
  7. 7. select row_to_json (location) from (select id, ancestry, name, coordinates from locations) as location {"id":23727,"ancestry":"214/217/22888","name":"Olho dÁgua do Piauí","coordinates":"POINT (-42.53937 -5.84854)"} {"id":69289,"ancestry":"74/115/68153","name":"la Sentiu de Sió","coordinates":"POINT (0.86948 41.79807)"}Tuesday, January 15, 13
  8. 8. select array_to_json (array_agg(location)) from (select id, ancestry, name, coordinates from locations) as location [{"id":23727,"ancestry":"214/217/22888","name":"Olho dÁgua do Piauí", "coordinates":"POINT (-42.53937 -5.84854)"}, {"id"69289,"ancestry":"74/115/68153", "name":"la Sentiu de Sió","coordinates":"POINT (0.86948 41.79807)"}]Tuesday, January 15, 13
  9. 9. Tuesday, January 15, 13
  10. 10. Location.where(ancestry_depth < 3). select(id, ancestry, name, coordinates).all. inject({}) do |memo, record| memo[record.ancestry] << record memo end { nil => [ <Location name=‘Europe’, id: 5 ...>, ... ], “5” => [ <Location name=‘Poland’, id: 245>, ...], ... }Tuesday, January 15, 13
  11. 11. nodes[nil].collect { |location| prepare_node(location) } def prepare_node(location) node(location). merge(prepare_children(nodes[node_key_for_children(location)])) end def node(location) location.to_node_data end def prepare_children(children) { children: children.collect do |location| prepare_node(location) end } endTuesday, January 15, 13
  12. 12. to_jsonTuesday, January 15, 13
  13. 13. Oj.dump to_jsonTuesday, January 15, 13
  14. 14. to_json Oj PostgreSQL 3000 2250 1500 750 0 2314Tuesday, January 15, 13
  15. 15. #1 LOCATIONTuesday, January 15, 13
  16. 16. query: China => <Location id: 9, name: “China”, ancestry: “1” ... > => ... => <Location id: 26727, name: “Chinavita”, ancestry: “214/228/26242” ... >Tuesday, January 15, 13
  17. 17. query: China => <Location id: 9, name: “China”, ancestry: “1” ... > => ... => <Location id: 26727, name: “Chinavita”, ancestry: “214/228/26242” ... >Tuesday, January 15, 13
  18. 18. CREATE OR REPLACE FUNCTION find_locations_with_ancestors(text, boolean) RETURNS int[] AS $$ DECLARE search_result_ids int[]; all_ids int[]; pattern text; BEGIN pattern := ^(||$1||)|[ |,](||$1||); FOR search_result_ids IN ( select (locations.id || string_to_array(locations.ancestry, /)::int[]) as ids from locations where (locations.name ~* pattern or locations.code ~* pattern or locations.alternative_name ~* pattern) and (not($2) or locations.coordinates = or locations.coordinates is null) ) LOOP all_ids := all_ids || search_result_ids; END LOOP; RETURN all_ids; END; $$ LANGUAGE plpgsql;Tuesday, January 15, 13
  19. 19. scope :find_with_ancestors, ->(query, only_without_coordinates) do where(array[locations.id] <@ (select * from find_locations_with_ancestors(?, ?)), query, only_without_coordinates) endTuesday, January 15, 13
  20. 20. find_locations_with_ancestors = (quary::text, without_cordinates::boolean) -> declare: record_location_ids::int[] ids::int[] begin: FOR record_location_ids IN :ruby- pattern = /^(||$1||)|[ |,](||$1||)/ :ruby= Location.where{ { name: pattern, code: pattern, alternative_name: pattern }.sql_or, ‘(not($2) or locations.coordinates = or locations.coordinates is null)’ }.select{ id || string_to_array(ancestry, ‘/’)::int[]) as ids }.to_sql ) LOOP all_ids := all_ids || search_result_ids END LOOP RETURN all_idsTuesday, January 15, 13
  21. 21. Tuesday, January 15, 13
  22. 22. RAILS 4 :-) hstore array (int, string) ranges (date, time, int) [?]Tuesday, January 15, 13
  23. 23. Tuesday, January 15, 13
  24. 24. PICTURES SOURCE http://xanthe93.deviantart.com/art/Steampunk-Elephant-Train- Lineart-290163415 http://www.businessinsider.com/fastest-trains-in-the-world-2011-11?op=1 http://www.fortogden.com/goldenspike.html http://noweevil.com/portfolio/wednesday-morning-wakeup-call-20-photos/ http://www.softicons.com/free-icons/application-icons/ruby-programming- icons-by-ahmad-galal/ruby-on-rails-iconTuesday, January 15, 13

×