PostgreSQL-Datentyp hstore: Unterschied zwischen den Versionen

Aus Geoinformation HSR
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: „Siehe auch: * PostgreSQL und PostgreSQL - Tipps und Tricks ...“)
 
(Tips & Tricks)
 
(4 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 2: Zeile 2:
 
* [[PostgreSQL]] und [[PostgreSQL - Tipps und Tricks]]
 
* [[PostgreSQL]] und [[PostgreSQL - Tipps und Tricks]]
  
...
+
'hstore' ist ein optionales Modul der PostgreSQL-Distribution Version 9 und Neuere (siehe Ref.1]). Der Name ist an Perl's Datentyp 'Hash' angelehnt. Weitere Namen dafür sind - je nach Computersprache - "Schlüssel-Wert-Speicher" (Entity-Attribute-Values (EAV) oder Key-Value Pairs (KVP) genannt), Dictionary (ISO?), Assoziatives Array (PHP) oder Map Collection (Java).
 +
 
 +
== Einführung ==
 +
 
 +
Einführung adaptiert aus "HStore: key-value PostgreSQL" von Josh Berkus (Quelle Ref.2): Die Renaissance der nicht relationalen Datenbanken bzw. der "NoSQL-Bewegung" hatte auch einen Einfluss auf PostgreSQL. Ein Resultat davon ist das optionale Modul hstore in Version 9.0, das einen "Schlüssel-Wert-Speicher" (Dictionary) in PostgreSQL implementiert. hstore war schon in früheren Versionen von PostgreSQL verfügbar, war jedoch nur beschränkt nutzbar. Diese Beschränkungen sind jetzt weg zusammen mit neuen Funktionen z.B. zur Schlüssel-Manipulation.
 +
 
 +
Zum Beispiel, angenommen Sie wollten Benutzerprofil-Daten als eine Reihe von Schlüssel-Wert-Paare zu speichern. Zuerst muss man hstore in die Datenbank laden, da es ein optionales Modul ist. Als nächstes würde man eine Tabelle erstellen:
 +
 
 +
  CREATE TABLE user_profile (
 +
    user_id INT NOT NULL PRIMARY KEY REFERENCES users(user_id),
 +
    profile HSTORE
 +
  );
 +
 
 +
Dann können sie Schlüssel-Wert-Paare in der Tabelle speichern:
 +
 
 +
  INSERT INTO user_profile
 +
  VALUES ( 5, hstore('"Home City"=>"San Francisco","Occupation"=>"Sculptor"');
 +
 
 +
Beachten Sie, dass das Format für die hstore-Strings ähnlich wie Hashes in Perl ist. Einfache JSON-Objekte werden wahrscheinlich in 9.1 unterstützt werden. Sie können jedoch auch ein Array-Format benutzen:
 +
 
 +
  INSERT INTO user_profile
 +
  VALUES ( 5, [["Home City"],["San Francisco"]][["Occupation"]["Sculptor"]]);  -- TBD.
 +
 
 +
Man braucht wahrscheinlich einen Index für die schnelle Suche:
 +
 
 +
  CREATE INDEX user_profile_hstore  ON user_profile  USING GIN (profile);
 +
 
 +
Nun kann man sehen, welche Keys enthalten sind:
 +
 
 +
  SELECT akeys(profile)  FROM user_profile  WHERE user_id = 5;
 +
 
 +
Einzelne Keys holen:
 +
 
 +
  SELECT profile -> 'Occupation'  FROM user_profile;
 +
 
 +
Spezifische Keys löschen:
 +
 
 +
  UPDATE user_profile SET profile = profile - 'Occupation'  WHERE user_id = 5;
 +
 
 +
Und schliesslich eine Abfrage mit dem hstore-Datentyp in der WHERE-Klausel:
 +
 
 +
  SELECT profile -> 'Home City' FROM user_profile  WHERE profile @> hstore('Home City','San Francisco');
 +
 
 +
== Tipps & Tricks ==
 +
 
 +
* ...
 +
 
 +
== Funktionsweise von hstore ==
 +
 
 +
Quelle: "Key/Value Pair versus hstore - Benchmarking Entity-Attribute-Value Structures in PostgreSQL", von Michel Ott im Datenbank-Seminar im MSE an der HSR Hochschule für Technik Rapperswil. [http://wiki.hsr.ch/Datenbanken/files/Benchmark_of_KVP_vs._hstore_-_doc.pdf]
 +
 
 +
* tbd.
 +
 
 +
== Dokumentation ==
 +
# PostgreSQL 9.0.4 Documentation - Chapter F.13 "Appendix F. Additional Supplied Modules": [http://www.postgresql.org/docs/current/interactive/hstore.html]
 +
# "PostgreSQL 9.0 arrives with many new features", by Josh Berkus, September 2010. Ankündigung http://lwn.net/Articles/406385/
 +
 
 +
[[Kategorie:PostgreSQL]]

Aktuelle Version vom 18. Juli 2011, 22:30 Uhr

Siehe auch:

'hstore' ist ein optionales Modul der PostgreSQL-Distribution Version 9 und Neuere (siehe Ref.1]). Der Name ist an Perl's Datentyp 'Hash' angelehnt. Weitere Namen dafür sind - je nach Computersprache - "Schlüssel-Wert-Speicher" (Entity-Attribute-Values (EAV) oder Key-Value Pairs (KVP) genannt), Dictionary (ISO?), Assoziatives Array (PHP) oder Map Collection (Java).

Einführung

Einführung adaptiert aus "HStore: key-value PostgreSQL" von Josh Berkus (Quelle Ref.2): Die Renaissance der nicht relationalen Datenbanken bzw. der "NoSQL-Bewegung" hatte auch einen Einfluss auf PostgreSQL. Ein Resultat davon ist das optionale Modul hstore in Version 9.0, das einen "Schlüssel-Wert-Speicher" (Dictionary) in PostgreSQL implementiert. hstore war schon in früheren Versionen von PostgreSQL verfügbar, war jedoch nur beschränkt nutzbar. Diese Beschränkungen sind jetzt weg zusammen mit neuen Funktionen z.B. zur Schlüssel-Manipulation.

Zum Beispiel, angenommen Sie wollten Benutzerprofil-Daten als eine Reihe von Schlüssel-Wert-Paare zu speichern. Zuerst muss man hstore in die Datenbank laden, da es ein optionales Modul ist. Als nächstes würde man eine Tabelle erstellen:

 CREATE TABLE user_profile (
   user_id INT NOT NULL PRIMARY KEY REFERENCES users(user_id),
   profile HSTORE
 );

Dann können sie Schlüssel-Wert-Paare in der Tabelle speichern:

 INSERT INTO user_profile 
 VALUES ( 5, hstore('"Home City"=>"San Francisco","Occupation"=>"Sculptor"');

Beachten Sie, dass das Format für die hstore-Strings ähnlich wie Hashes in Perl ist. Einfache JSON-Objekte werden wahrscheinlich in 9.1 unterstützt werden. Sie können jedoch auch ein Array-Format benutzen:

 INSERT INTO user_profile 
 VALUES ( 5, [["Home City"],["San Francisco"]][["Occupation"]["Sculptor"]]);  -- TBD.

Man braucht wahrscheinlich einen Index für die schnelle Suche:

 CREATE INDEX user_profile_hstore  ON user_profile  USING GIN (profile);

Nun kann man sehen, welche Keys enthalten sind:

 SELECT akeys(profile)  FROM user_profile  WHERE user_id = 5;

Einzelne Keys holen:

 SELECT profile -> 'Occupation'  FROM user_profile;

Spezifische Keys löschen:

 UPDATE user_profile SET profile = profile - 'Occupation'  WHERE user_id = 5;

Und schliesslich eine Abfrage mit dem hstore-Datentyp in der WHERE-Klausel:

 SELECT profile -> 'Home City' FROM user_profile  WHERE profile @> hstore('Home City','San Francisco');

Tipps & Tricks

  • ...

Funktionsweise von hstore

Quelle: "Key/Value Pair versus hstore - Benchmarking Entity-Attribute-Value Structures in PostgreSQL", von Michel Ott im Datenbank-Seminar im MSE an der HSR Hochschule für Technik Rapperswil. [1]

  • tbd.

Dokumentation

  1. PostgreSQL 9.0.4 Documentation - Chapter F.13 "Appendix F. Additional Supplied Modules": [2]
  2. "PostgreSQL 9.0 arrives with many new features", by Josh Berkus, September 2010. Ankündigung http://lwn.net/Articles/406385/