Feld automatisch füllen in Abhängigkeit eines anderen.

Hallo,

kann man es in der Datenbank intern bewerkstelligen das ein Feld automatisch in Abhängigkeit eines anderen Feldes befüllt wird?
Als Beispiel mal Vorwahlen:

030 | automatisch “Berlin”
040 | automatisch “Hamburg”

Schöne Grüße
Thomas

Wozu? Das ist redundant. Ansonsten: siehe TRIGGER

Hallo,

ja das Beispiel war nicht so gut.
Es geht um Folgendes.
Tabelle

VariablenNr.     |     Datum     |    Wert

Nun gibt es Variablen wo der Wert auch gleich der Wert ist, z.B. Temperatur.
Und Variablen wo der Wert hoch zählt z.B. Regenmenge.
Nun muss ich beim auslesen irgend wie wissen um welchen Typ es sich handelt und dachte das diese Feld dann automatisch eingetragen wird.
Oder was ich mir noch vorstellen kann ist eine weitere Tabelle:

VariablenNr.     |     VariablenArt

Währe wohl eine bessere Lösung.

Schöne Grüße
Thomas

Wieviel Liter fielen vorgestern?

Oder anders gefragt: Du kannst bei Deinem Modell zwar sagen, wie warm es vorgestern war, nicht aber die Regenmenge nennen.

Hallo,

ja, weil die die Auswertung nicht weiß das bei der Variable Regen die Differtens gebildet werden muss. Daher muss ich das irgend wo hinterlegen, entweder extern als array unter PHP oder direkt in der DB als Tabelle. Die Frage ist was ist besser.
Die DB läuft auf einen RasPi und ist daher nicht die Schnellste, Eine Abfrage dauert ca. 40sec wo bei es kaum eine Rolle spielt ob ein Tag oder ein Monat Abfrage.

Schöne Grüße
Thomas

Ich wollt nur den Finger in die Wunde legen :wink:

Überleg Dir halt, was Du auswerten willst.

Die DB läuft auf einen RasPi und ist daher nicht die Schnellste, Eine Abfrage dauert ca. 40sec wo bei es kaum eine Rolle spielt ob ein Tag oder ein Monat Abfrage.

Kein Grund lahm zu sein. Was sagt Explain?

Hallo,

Explain gibt folgendes aus:

"Merge Left Join  (cost=220000.40..220020.78 rows=1000 width=108) (actual time=57319.867..57320.437 rows=26 loops=1)"
"  Output: (('2014-06-01 00:00:00'::timestamp without time zone + ((s.s)::double precision * '06:00:00'::interval))), (count(*)), (round((avg(variablen.value))::numeric, 1)), (round((min(variablen.value))::numeric, 1)), (round((max(variablen.value))::numeri (...)"
"  Merge Cond: ((('2014-06-01 00:00:00'::timestamp without time zone + ((s.s)::double precision * '06:00:00'::interval))) = (timezone('UTC'::text, to_timestamp((trunc((date_part('epoch'::text, variablen.updatezeit) / 21600::double precision)) * 21600::doubl (...)"
"  ->  Sort  (cost=59.83..62.33 rows=1000 width=4) (actual time=0.817..0.876 rows=26 loops=1)"
"        Output: s.s, (('2014-06-01 00:00:00'::timestamp without time zone + ((s.s)::double precision * '06:00:00'::interval)))"
"        Sort Key: (('2014-06-01 00:00:00'::timestamp without time zone + ((s.s)::double precision * '06:00:00'::interval)))"
"        Sort Method: quicksort  Memory: 18kB"
"        ->  Function Scan on pg_catalog.generate_series s  (cost=0.00..10.00 rows=1000 width=4) (actual time=0.236..0.508 rows=26 loops=1)"
"              Output: s.s, ('2014-06-01 00:00:00'::timestamp without time zone + ((s.s)::double precision * '06:00:00'::interval))"
"              Function Call: generate_series(0, 25)"
"  ->  Materialize  (cost=219940.57..219940.62 rows=3 width=112) (actual time=57318.990..57319.165 rows=28 loops=1)"
"        Output: (timezone('UTC'::text, to_timestamp((trunc((date_part('epoch'::text, variablen.updatezeit) / 21600::double precision)) * 21600::double precision)))), (count(*)), (round((avg(variablen.value))::numeric, 1)), (round((min(variablen.value))::nu (...)"
"        ->  Sort  (cost=219940.57..219940.58 rows=3 width=12) (actual time=57318.971..57319.028 rows=28 loops=1)"
"              Output: (timezone('UTC'::text, to_timestamp((trunc((date_part('epoch'::text, variablen.updatezeit) / 21600::double precision)) * 21600::double precision)))), (count(*)), (round((avg(variablen.value))::numeric, 1)), (round((min(variablen.value (...)"
"              Sort Key: (timezone('UTC'::text, to_timestamp((trunc((date_part('epoch'::text, variablen.updatezeit) / 21600::double precision)) * 21600::double precision))))"
"              Sort Method: quicksort  Memory: 19kB"
"              ->  HashAggregate  (cost=219940.42..219940.55 rows=3 width=12) (actual time=57315.175..57318.526 rows=29 loops=1)"
"                    Output: (timezone('UTC'::text, to_timestamp((trunc((date_part('epoch'::text, variablen.updatezeit) / 21600::double precision)) * 21600::double precision)))), count(*), round((avg(variablen.value))::numeric, 1), round((min(variablen.valu (...)"
"                    ->  Seq Scan on public.variablen  (cost=0.00..219934.02 rows=512 width=12) (actual time=20650.540..57277.642 rows=651 loops=1)"
"                          Output: variablen.updatezeit, variablen.value, timezone('UTC'::text, to_timestamp((trunc((date_part('epoch'::text, variablen.updatezeit) / 21600::double precision)) * 21600::double precision)))"
"                          Filter: ((variablen.updatezeit >= '2014-06-01 00:00:00'::timestamp without time zone) AND (variablen.varid = 48020))"
"Total runtime: 57321.529 ms"

Allerdings kann ich damit nicht viel anfangen.

Schöne Grüße
Thomas

Ich hab mal nur den relevanten Teil gelassen. Prüfe, ob da passende Indexe sind, evtl. einen über (varid, updatezeit) setzen.

Hallo,

index ist vorhanden, ich bekomme PG aber nicht dazu den index scan zu nutzen.
Hab schon mit den kosten gespielt, aber nichts zu machen. Hier läuft PG9.1.
Die gleiche Abfrage auf einen Windows PC dauert nur 4 sec.

Schöne Grüße
Thomas

Zeig mal die Definition der Tabelle und die Abfrage. Wie viele Zeilen hat die Tabelle?

Hallo,

die Tabelle:

-- Table: variablen

-- DROP TABLE variablen;

CREATE TABLE variablen
(
  id serial NOT NULL,
  varid integer NOT NULL,
  varname text,
  varsuffix text,
  value real,
  typ integer,
  updatezeit timestamp without time zone,
  CONSTRAINT variablen_pkey PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE variablen
  OWNER TO ips;

Abfrage ist eigentlich egal, das Ergebniss ist immer gleich, aber nehmen wir mal diese:

SELECT 
  variablen.id, 
  variablen.varid, 
  variablen.updatezeit,
  lag (variablen.value) over(ORDER BY variablen.updatezeit ASC, variablen.id ASC),
  variablen.value, 
  lead (variablen.value) over(ORDER BY variablen.updatezeit ASC, variablen.id ASC),
  round (cast (variablen.value - lag (variablen.value) over(ORDER BY variablen.updatezeit ASC, variablen.id ASC) as numeric),3) as diff,
  lead (variablen.value) over(ORDER BY variablen.updatezeit ASC, variablen.id ASC) - lag (variablen.value) over(ORDER BY variablen.updatezeit ASC, variablen.id ASC) as leadlag,
  lag (variablen.value) over(ORDER BY variablen.updatezeit ASC, variablen.id ASC) + ((lead (variablen.value) over(ORDER BY variablen.updatezeit ASC, variablen.id ASC) - lag (variablen.value) over(ORDER BY variablen.updatezeit ASC, variablen.id ASC)) / 2) as new
FROM 
  public.variablen
WHERE variablen.varid = 48020
ORDER BY
  diff ASC;

Analyse:

"Sort  (cost=203921.95..203940.11 rows=7265 width=20) (actual time=44356.921..44368.180 rows=6201 loops=1)"
"  Output: id, varid, updatezeit, (lag(value) OVER (?)), value, (lead(value) OVER (?)), (round(((value - lag(value) OVER (?)))::numeric, 3)), ((lead(value) OVER (?) - lag(value) OVER (?))), ((lag(value) OVER (?) + ((lead(value) OVER (?) - lag(value) OVER (? (...)"
"  Sort Key: (round(((variablen.value - lag(variablen.value) OVER (?)))::numeric, 3))"
"  Sort Method: quicksort  Memory: 565kB"
"  ->  WindowAgg  (cost=203056.44..203456.02 rows=7265 width=20) (actual time=43690.509..44230.228 rows=6201 loops=1)"
"        Output: id, varid, updatezeit, lag(value) OVER (?), value, lead(value) OVER (?), round(((value - lag(value) OVER (?)))::numeric, 3), (lead(value) OVER (?) - lag(value) OVER (?)), (lag(value) OVER (?) + ((lead(value) OVER (?) - lag(value) OVER (?))  (...)"
"        ->  Sort  (cost=203056.44..203074.61 rows=7265 width=20) (actual time=43690.284..43713.686 rows=6201 loops=1)"
"              Output: id, varid, updatezeit, value"
"              Sort Key: variablen.updatezeit, variablen.id"
"              Sort Method: quicksort  Memory: 565kB"
"              ->  Seq Scan on public.variablen  (cost=0.00..202590.51 rows=7265 width=20) (actual time=16.984..43455.976 rows=6201 loops=1)"
"                    Output: id, varid, updatezeit, value"
"                    Filter: (variablen.varid = 48020)"
"Total runtime: 44379.447 ms"

Simple Abfrage:

SELECT 
  variablen.id, 
  variablen.varid, 
  variablen.updatezeit
FROM 
  public.variablen
WHERE 
  variablen.varid = 48020

Analyse:

"Seq Scan on public.variablen  (cost=0.00..202600.12 rows=7265 width=16) (actual time=16.137..40865.291 rows=6201 loops=1)"
"  Output: id, varid, updatezeit"
"  Filter: (variablen.varid = 48020)"
"Total runtime: 40916.416 ms"

Datenbank mit 9400000 Zeilen.

Statistik:

Sequential Scans	63	
Sequential Tuples Read	593137949	
Indexscans	46	
Index Tuples Fetched	9567980	
Tupel-Eingefügt	121676	
Tupel-Aktualisiert	0	
Tupel-Gelöscht	0	
Tupel-HOT aktualisiert	0	
Aktive Tupel	121676	
Inaktive Tupel	0	
Heap Blocks Read	5246046	
Heap Blocks Hit	288705	
Index Blocks Read	57385	
Index Blocks Hit	9006249	
Toast Blocks Read	0	
Toast Blocks Hit	0	
Toast Index Blocks Read	0	
Toast Index Blocks Hit	0	
Letztes Vacuum		
Letztes Autovacuum		
Letztes Analyse		
Letztes Autoanalyze		
VACUUM Zähler	0	
Autovacuum Zähler	0	
Analyse Zähler	0	
Autoanalyze Zähler	0	
Tabellengröße	659 MB	
Toast-Tabellengröße	8192 bytes	
Indexgröße	203 MB

Letzter Statistiken Reset	2014-06-08 14:09:20.801202+02

Schöne Grüße
Thomas

Ohne Index. Die Abfrage liefert 0.06% der Datensätze, sie würde mit Index sehr wahrscheinlich ganz locker Faktor 1000 schneller sein. Eher mehr.

Hallo,

ah ok, ich habe den PRIMARY KEY mit den index verwechselt.
Welche Felder sollte man indizieren?

Schöne Grüße
Thomas

Die, die man insbesondere im WHERE nutzt. Ich nannte sie glaube schon…

Hallo,

danke hat gut funktioniert, ich habe jetzt die VarID im Index, das hat mir eine menge Zeit gespart, da ich heute Nacht noch 500 Abfragen am Laufen hatte.
Die alte alte Zeit lag bei 45 die neue bei 8 sec.

Hier noch die neue Analyse.

"Sort  (cost=14099.18..14118.30 rows=7648 width=20) (actual time=7739.709..7750.721 rows=6282 loops=1)"
"  Output: id, varid, updatezeit, (lag(value) OVER (?)), value, (lead(value) OVER (?)), (round(((value - lag(value) OVER (?)))::numeric, 3)), ((lead(value) OVER (?) - lag(value) OVER (?))), ((lag(value) OVER (?) + ((lead(value) OVER (?) - lag(value) OVER (? (...)"
"  Sort Key: (round(((variablen.value - lag(variablen.value) OVER (?)))::numeric, 3))"
"  Sort Method: quicksort  Memory: 570kB"
"  ->  WindowAgg  (cost=13185.21..13605.85 rows=7648 width=20) (actual time=6917.086..7585.779 rows=6282 loops=1)"
"        Output: id, varid, updatezeit, lag(value) OVER (?), value, lead(value) OVER (?), round(((value - lag(value) OVER (?)))::numeric, 3), (lead(value) OVER (?) - lag(value) OVER (?)), (lag(value) OVER (?) + ((lead(value) OVER (?) - lag(value) OVER (?))  (...)"
"        ->  Sort  (cost=13185.21..13204.33 rows=7648 width=20) (actual time=6916.772..6943.407 rows=6282 loops=1)"
"              Output: id, varid, updatezeit, value"
"              Sort Key: variablen.updatezeit, variablen.id"
"              Sort Method: quicksort  Memory: 570kB"
"              ->  Bitmap Heap Scan on public.variablen  (cost=102.05..12691.88 rows=7648 width=20) (actual time=34.894..6734.781 rows=6282 loops=1)"
"                    Output: id, varid, updatezeit, value"
"                    Recheck Cond: (variablen.varid = 48020)"
"                    ->  Bitmap Index Scan on idx_varid  (cost=0.00..100.13 rows=7648 width=0) (actual time=20.619..20.619 rows=6282 loops=1)"
"                          Index Cond: (variablen.varid = 48020)"
"Total runtime: 7759.821 ms"

Schöne Grüße
Thomas

Okay. Hatte mehr gedacht, aber immerhin.