Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PostgreSQL Hstore

PostgreSQL Hstore

Talk at Codeaholics Hong Kong
Wednesday 9th May 2012

Matthew Rudy Jacobs

May 09, 2012
Tweet

More Decks by Matthew Rudy Jacobs

Other Decks in Technology

Transcript

  1. a Coder • has a name • has a location

    • has a github (optional) • has a twitter (optional)
  2. Split the location { "name": "Matthew Rudy", "location_id": ObjectId("abc"), "twitter":

    "@matthewrudy", "github": "matthewrudy" } { "_id": ObjectId("abc"), "name": "Hong Kong" }
  3. Requirements Grow! • Coders have Projects • Coders have commits

    on Projects • Projects have Categories • How many commits does @MatthewRudy have on PHP projects?
  4. PostgreSQL CREATE TABLE coders ( name text NOT NULL, location_id

    integer NOT NULL REFERENCES locations, github text, twitter text );
  5. How many Coders have Github and Twitter? • SELECT COUNT(*)

    FROM coders WHERE github IS NOT NULL AND twitter IS NOT NULL;
  6. How many Coders have Github OR Twitter? • SELECT COUNT(*)

    FROM coders WHERE github IS NOT NULL OR twitter IS NOT NULL;
  7. How many HKers have Github and Twitter? • SELECT COUNT(*)

    FROM coders WHERE github IS NOT NULL AND twitter IS NOT NULL AND location_id = 2;
  8. value of key • (column -> ‘key’)::text • (column ->

    ‘key’)::integer • (column -> ‘key’)::date
  9. has key? • column ? key • column ?& ARRAY[‘key1’,

    ‘key2’] • column ?| ARRAY[‘key1’, ‘key2’]
  10. PostgreSQL hstore CREATE TABLE hcoders ( name text NOT NULL,

    location_id integer NOT NULL REFERENCES locations, identities hstore );
  11. Find MatthewRudy on Github • SELECT * FROM coders WHERE

    (identities -> ‘github’)::text = “matthewrudy”;
  12. Find MatthewRudy on Github • SELECT * FROM coders WHERE

    identities @> ‘github=>matthewrudy’
  13. How many Coders have Github and Twitter? • SELECT COUNT(*)

    FROM coders WHERE identities ?& ARRAY[‘github’, ‘twitter’];
  14. How many Coders have Github OR Twitter? • SELECT COUNT(*)

    FROM coders WHERE identities ?| ARRAY[‘github’, ‘twitter’];
  15. How many HKers have Github and Twitter? • SELECT COUNT(*)

    FROM coders WHERE identities ?& ARRAY[‘github’, ‘twitter’] AND location_id = 2;