24
POSTGRESQL & ROR Tuesday, January 15, 13

Postgresql and ror

Embed Size (px)

Citation preview

Page 1: Postgresql and ror

POSTGRESQL & ROR

Tuesday, January 15, 13

Page 2: Postgresql and ror

SPEED ...

Tuesday, January 15, 13

Page 3: Postgresql and ror

SPEED (OR LACK OF)

Tuesday, January 15, 13

Page 4: Postgresql and ror

SPEED (OR LACK OF)

90seconds

for a pageTuesday, January 15, 13

Page 5: Postgresql and ror

#1 LOCATION

112 000

locations

Tuesday, January 15, 13

Page 6: Postgresql and ror

#1 LOCATION

Tuesday, January 15, 13

Page 7: Postgresql and ror

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

Page 8: Postgresql and ror

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

Page 9: Postgresql and ror

Tuesday, January 15, 13

Page 10: Postgresql and ror

Location.where('ancestry_depth < 3').select('id, ancestry, name, coordinates').all.inject({}) do |memo, record|

memo[record.ancestry] << recordmemo

end

{ nil => [ <Location name=‘Europe’, id: 5 ...>, ... ], “5” => [ <Location name=‘Poland’, id: 245>, ...], ... }

Tuesday, January 15, 13

Page 11: Postgresql and ror

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

}end

Tuesday, January 15, 13

Page 12: Postgresql and ror

to_json

Tuesday, January 15, 13

Page 13: Postgresql and ror

Oj.dump

to_json

Tuesday, January 15, 13

Page 14: Postgresql and ror

0

750

1500

2250

3000

2314

to_json Oj PostgreSQL

Tuesday, January 15, 13

Page 15: Postgresql and ror

#1 LOCATION

Tuesday, January 15, 13

Page 16: Postgresql and ror

query: China

=> <Location id: 9, name: “China”,ancestry: “1” ... >

=> ...=> <Location id: 26727, name: “Chinavita”,

ancestry: “214/228/26242” ... >

Tuesday, January 15, 13

Page 17: Postgresql and ror

query: China

=> <Location id: 9, name: “China”,

ancestry: “1” ... >=> ...

=> <Location id: 26727, name: “Chinavita”,

ancestry: “214/228/26242” ... >

Tuesday, January 15, 13

Page 18: Postgresql and ror

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

Page 19: Postgresql and ror

scope :find_with_ancestors, ->(query, only_without_coordinates) dowhere('array[locations.id] <@

(select * from find_locations_with_ancestors(?, ?))',query, only_without_coordinates)

end

Tuesday, January 15, 13

Page 20: Postgresql and ror

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_ids

Tuesday, January 15, 13

Page 21: Postgresql and ror

Tuesday, January 15, 13

Page 22: Postgresql and ror

RAILS 4 :-)

hstore

array (int, string)

ranges (date, time, int) [?]

Tuesday, January 15, 13

Page 23: Postgresql and ror

Tuesday, January 15, 13

Page 24: Postgresql and ror

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-icon

Tuesday, January 15, 13