Upload
michal-czyz
View
94
Download
0
Tags:
Embed Size (px)
Citation preview
POSTGRESQL & ROR
Tuesday, January 15, 13
SPEED ...
Tuesday, January 15, 13
SPEED (OR LACK OF)
Tuesday, January 15, 13
SPEED (OR LACK OF)
90seconds
for a pageTuesday, January 15, 13
#1 LOCATION
112 000
locations
Tuesday, January 15, 13
#1 LOCATION
Tuesday, January 15, 13
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
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
Tuesday, January 15, 13
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
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
to_json
Tuesday, January 15, 13
Oj.dump
to_json
Tuesday, January 15, 13
0
750
1500
2250
3000
2314
to_json Oj PostgreSQL
Tuesday, January 15, 13
#1 LOCATION
Tuesday, January 15, 13
query: China
=> <Location id: 9, name: “China”,ancestry: “1” ... >
=> ...=> <Location id: 26727, name: “Chinavita”,
ancestry: “214/228/26242” ... >
Tuesday, January 15, 13
query: China
=> <Location id: 9, name: “China”,
ancestry: “1” ... >=> ...
=> <Location id: 26727, name: “Chinavita”,
ancestry: “214/228/26242” ... >
Tuesday, January 15, 13
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
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
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
Tuesday, January 15, 13
RAILS 4 :-)
hstore
array (int, string)
ranges (date, time, int) [?]
Tuesday, January 15, 13
Tuesday, January 15, 13
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