forked from biblat/Biblat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaterializedViews.sql
2182 lines (2015 loc) · 68.6 KB
/
MaterializedViews.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
CREATE TABLE matviews
(
mv_name name NOT NULL,
v_name name NOT NULL,
last_refresh timestamp with time zone,
CONSTRAINT matviews_pkey PRIMARY KEY (mv_name )
);
CREATE OR REPLACE FUNCTION create_matview(NAME, NAME)
RETURNS VOID
SECURITY DEFINER
LANGUAGE plpgsql AS '
DECLARE
matview ALIAS FOR $1;
view_name ALIAS FOR $2;
entry matviews%ROWTYPE;
BEGIN
SELECT * INTO entry FROM matviews WHERE mv_name = matview;
IF FOUND THEN
RAISE EXCEPTION ''Materialized view ''''%'''' already exists.'',
matview;
END IF;
EXECUTE ''REVOKE ALL ON '' || view_name || '' FROM PUBLIC'';
EXECUTE ''GRANT SELECT ON '' || view_name || '' TO PUBLIC'';
EXECUTE ''CREATE TABLE '' || matview || '' AS SELECT * FROM '' || view_name;
EXECUTE ''REVOKE ALL ON '' || matview || '' FROM PUBLIC'';
EXECUTE ''GRANT SELECT ON '' || matview || '' TO PUBLIC'';
INSERT INTO matviews (mv_name, v_name, last_refresh)
VALUES (matview, view_name, CURRENT_TIMESTAMP);
RETURN;
END';
CREATE OR REPLACE FUNCTION drop_matview(NAME) RETURNS VOID
SECURITY DEFINER
LANGUAGE plpgsql AS '
DECLARE
matview ALIAS FOR $1;
entry matviews%ROWTYPE;
BEGIN
SELECT * INTO entry FROM matviews WHERE mv_name = matview;
IF NOT FOUND THEN
RAISE EXCEPTION ''Materialized view % does not exist.'', matview;
END IF;
EXECUTE ''DROP TABLE '' || matview;
DELETE FROM matviews WHERE mv_name=matview;
RETURN;
END';
CREATE OR REPLACE FUNCTION indexes_matview(name)
RETURNS void AS
$BODY$
DECLARE
matview ALIAS FOR $1;
index_matview RECORD;
indexes_matview text[];
i integer;
BEGIN
SELECT array_agg(indexdef) INTO indexes_matview FROM pg_indexes WHERE tablename='mvIndiceCoautoriaPricePais';
RAISE NOTICE 'indexes_matview: %', indexes_matview;
FOR i IN SELECT generate_subscripts( indexes_matview, 1 ) LOOP
RAISE NOTICE 'Index definition: %', indexes_matview[i];
EXECUTE indexes_matview[i];
END LOOP;
RAISE NOTICE 'mat_view: %', matview;
FOR index_matview IN SELECT indexdef FROM pg_indexes WHERE tablename=replace(matview, '"', '') LOOP
RAISE NOTICE 'Index definition: %', index_matview.indexdef;
END LOOP;
END;
$BODY$
LANGUAGE plpgsql VOLATILE;
CREATE OR REPLACE FUNCTION refresh_matview(name)
RETURNS void AS $$
DECLARE
matview ALIAS FOR $1;
entry matviews%ROWTYPE;
indexes_matview RECORD;
i integer;
BEGIN
SELECT * INTO entry FROM matviews WHERE mv_name = matview;
IF NOT FOUND THEN
RAISE EXCEPTION 'Materialized view % does not exist.', matview;
END IF;
SELECT array_agg(indexdef) AS definition, array_agg(indexname) AS name INTO indexes_matview FROM pg_indexes WHERE tablename=replace(matview, '"', '');
FOR i IN SELECT generate_subscripts( indexes_matview.name, 1 ) LOOP
RAISE NOTICE 'DROP INDEX: %', indexes_matview.name[i];
EXECUTE 'DROP INDEX "'||indexes_matview.name[i]||'"';
END LOOP;
EXECUTE 'DELETE FROM ' || matview;
EXECUTE 'INSERT INTO ' || matview
|| ' SELECT * FROM ' || entry.v_name;
FOR i IN SELECT generate_subscripts( indexes_matview.definition, 1 ) LOOP
RAISE NOTICE 'INDEX definition: %', indexes_matview.definition[i];
EXECUTE indexes_matview.definition[i];
END LOOP;
UPDATE matviews
SET last_refresh=CURRENT_TIMESTAMP
WHERE mv_name=matview;
RETURN;
END
$$
LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION refresh_matviews()
RETURNS void AS $$
DECLARE
matview RECORD;
sql text;
BEGIN
FOR matview IN SELECT matviewname FROM pg_matviews LOOP
sql :='REFRESH MATERIALIZED VIEW "'||matview.matviewname||'"';
RAISE NOTICE 'EXECUTE: %', sql;
EXECUTE sql;
END LOOP;
RETURN;
END
$$
LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION truncate_matviews()
RETURNS void AS $$
DECLARE
matview RECORD;
sql text;
BEGIN
FOR matview IN SELECT matviewname FROM pg_matviews LOOP
sql :='REFRESH MATERIALIZED VIEW "'||matview.matviewname||'" WITH NO DATA';
RAISE NOTICE 'EXECUTE: %', sql;
EXECUTE sql;
END LOOP;
RETURN;
END
$$
LANGUAGE plpgsql;
--Vista para revista y su disciplina correspondiente
--DROP MATERIALIZED VIEW "mvRevistaDisciplina";
CREATE MATERIALIZED VIEW "mvRevistaDisciplina" AS
SELECT
base, "revistaSlug", (array_agg(revista))[1] AS revista, (array_agg(issn))[1] AS issn, (array_agg("disciplinaRevista"))[1] AS "disciplinaRevista", sum(documentos)
--base, "revistaSlug", array_agg(revista), array_agg("disciplinaRevista"), array_agg(documentos)
FROM
(SELECT
substr(sistema, 1, 5) AS base,
slug(revista) AS "revistaSlug",
revista,
issn,
"disciplinaRevista",
count(*) AS documentos
FROM article
GROUP BY substr(sistema, 1, 5), slug(revista), revista, issn, "disciplinaRevista"
ORDER BY base, "revistaSlug", documentos DESC) t
WHERE "revistaSlug" IS NOT NULL
GROUP BY base, "revistaSlug"
HAVING count(*) > 1;
--Vista para busquedas
--DROP MATERIALIZED VIEW "mvSearch";
CREATE MATERIALIZED VIEW "mvSearch" AS
SELECT
t.sistema,
slug(t.revista) AS "revistaSlug",
slug(t.articulo) AS "articuloSlug",
slug(t."paisRevista") AS "paisRevistaSlug",
regexp_replace(t."descripcionBibliografica"->>'a', '.*?([0-9]+)', '\1')::varchar AS volumen,
regexp_replace(t."descripcionBibliografica"->>'b', '.*?([0-9]+)', '\1')::varchar AS numero,
t."descripcionBibliografica"->>'c' AS periodo,
regexp_replace(t."descripcionBibliografica"->>'e', '^.*?([0-9]+.*)', '\1')::varchar AS paginacion,
t.documento->>'a' AS "tipoDocumento",
t.documento->>'b' AS "enfoqueDocumento",
d.slug AS "disciplinaSlug",
d.id_disciplina,
a."autoresJSON",
a."autoresSlug",
i."institucionesJSON",
i."institucionesSlug",
concat(json_slug(t."palabraClave"), json_slug(t.keyword)) AS "palabrasClaveSlug",
concat(
json_slug(t."palabraClave") || ' ',
json_slug(t.keyword) || ' ',
slug_space(t.articulo) || ' | ',
slug_space(t.revista) || ' | ',
slug_space(t."paisRevista") || ' | ',
i."institucionesSlug",
a."autoresSlug"
) AS "generalSlug"
FROM article t
LEFT JOIN (SELECT
sistema,
json_agg((SELECT row_to_json(data_row) from (SELECT id, "institucionId" AS z, nombre AS a, email as "6") as data_row) ORDER BY id) AS "autoresJSON",
string_agg(slug_space(nombre), ' | ' ORDER BY id) || ' | ' AS "autoresSlug"
FROM author
GROUP BY sistema) a --Autores
ON (t.sistema=a.sistema)
LEFT JOIN (SELECT
sistema,
json_agg((SELECT row_to_json(data_row) from (SELECT id AS z, institucion AS u, dependencia AS v, ciudad AS w, pais AS x) as data_row) ORDER BY id) AS "institucionesJSON",
string_agg(slug_space(institucion), ' | ' ORDER BY id) || ' | ' AS "institucionesSlug"
FROM institution
GROUP BY sistema) i --Instituciones
ON (t.sistema=i.sistema)
INNER JOIN disciplinas d
ON slug(t."disciplinaRevista")=d.slug;
CREATE INDEX ON "mvSearch"(sistema);
CREATE INDEX ON "mvSearch"(id_disciplina);
CREATE INDEX ON "mvSearch"(volumen);
CREATE INDEX ON "mvSearch"(numero);
CREATE INDEX ON "mvSearch"("articuloSlug");
CREATE INDEX ON "mvSearch"("revistaSlug");
CREATE INDEX ON "mvSearch"("disciplinaSlug");
CREATE INDEX ON "mvSearch"("paisRevistaSlug");
CREATE INDEX ON "mvSearch" USING gin("generalSlug" gin_trgm_ops);
CREATE INDEX ON "mvSearch" USING gin("autoresSlug" gin_trgm_ops);
CREATE INDEX ON "mvSearch" USING gin("articuloSlug" gin_trgm_ops);
CREATE INDEX ON "mvSearch" USING gin("revistaSlug" gin_trgm_ops);
CREATE INDEX ON "mvSearch" USING gin("paisRevistaSlug" gin_trgm_ops);
CREATE INDEX ON "mvSearch" USING gin("institucionesSlug" gin_trgm_ops);
CREATE INDEX ON "mvSearch" USING gin("palabrasClaveSlug" gin_trgm_ops);
-- CREATE INDEX "searchIdDatabase_idx" ON "mvSearch"(iddatabase);
-- CREATE INDEX "searchTextoCompleto_idx" ON "mvSearch"(url);
-- CREATE INDEX ON "mvSearch"(substring(LOWER(revista), 1, 1));
-- CREATE INDEX "searchGeneralSlug_idx" ON "mvSearch" USING gin(("generalSlug"::tsvector));
-- Vista con el contenido de la ficha del documento
-- DROP VIEW "vSearchFull";
CREATE VIEW "vSearchFull" AS
SELECT
a.*,
s."revistaSlug",
s."articuloSlug",
s."paisRevistaSlug",
s.volumen,
s.numero,
s.periodo,
s.paginacion,
s."tipoDocumento",
s."enfoqueDocumento",
s."disciplinaSlug",
s.id_disciplina,
s."autoresJSON",
s."autoresSlug",
s."institucionesJSON",
s."institucionesSlug",
s."palabrasClaveSlug",
s."generalSlug"
FROM article a
INNER JOIN "mvSearch" s
ON a.sistema=s.sistema;
--Vista para lista de paises
--DROP MATERIALIZED VIEW "mvPais";
CREATE MATERIALIZED VIEW "mvPais" AS
SELECT
"paisRevistaSlug",
"paisRevista",
count(*) AS total
FROM "vSearchFull"
GROUP BY "paisRevistaSlug", "paisRevista"
ORDER BY "paisRevistaSlug";
--Vista para disciplinas
--DROP MATERIALIZED VIEW "mvDisciplina";
CREATE MATERIALIZED VIEW "mvDisciplina" AS
SELECT DISTINCT
a.id_disciplina,
d.disciplina,
d.slug,
count(*) AS total
FROM "vSearchFull" a
INNER JOIN disciplinas d ON a.id_disciplina=d.id_disciplina
GROUP BY a.id_disciplina, d.disciplina, d.slug
ORDER BY d.disciplina;
--Vista para las revistas por disciplina
--DROP MATERIALIZED VIEW "mvDisciplinaRevistas";
CREATE MATERIALIZED VIEW "mvDisciplinaRevistas" AS
SELECT
"revistaSlug",
(array_agg(revista))[1] AS revista,
(array_agg(issn))[1] AS issn,
(array_agg(id_disciplina))[1] AS id_disciplina,
sum(documentos) AS documentos
FROM
(SELECT
"revistaSlug",
(array_agg(revista))[1] AS revista,
(array_agg(issn))[1] AS issn,
id_disciplina,
sum(documentos) AS documentos
FROM
(SELECT
"revistaSlug",
revista,
issn,
id_disciplina,
count(*) AS documentos
FROM "vSearchFull"
GROUP BY id_disciplina, "revistaSlug", revista, issn
ORDER BY id_disciplina, "revistaSlug", documentos DESC) t
GROUP BY id_disciplina, "revistaSlug"
ORDER BY "revistaSlug", documentos DESC) t2
GROUP BY "revistaSlug";
CREATE INDEX ON "mvDisciplinaRevistas"(substring("revistaSlug", 1, 1));
CREATE INDEX ON "mvDisciplinaRevistas"("revistaSlug");
--Vista para mostrar solo los documentos que sean artículos y mostrando el año en una cadena de 4 digitos
--DROP VIEW "vArticulos";
CREATE OR REPLACE VIEW "vArticulos" AS
WITH articulos AS
(SELECT
sistema,
id_disciplina,
revista,
"revistaSlug",
volumen,
numero,
substr("anioRevista", 1, 4) AS anio,
"paisRevista",
"paisRevistaSlug"
FROM "vSearchFull" WHERE
"tipoDocumento" ~~ 'Artículo%'
AND substr("anioRevista", 1, 4) ~ '[0-9]{4}'
AND "revistaSlug"::varchar != ALL((SELECT array_agg("revistaSlug")::varchar[] FROM "revistasBacklist")::varchar[]))
SELECT
a.*
FROM
articulos a
INNER JOIN
(SELECT "revistaSlug", anios_continuos(array_agg(anio)) AS anios_continuos
FROM
(SELECT
"revistaSlug",
anio,
count(*) AS articulos
FROM articulos GROUP BY "revistaSlug", anio HAVING count(*) > 4) title --Titulos de revista con más de 4 articulos al año
GROUP BY "revistaSlug" HAVING anios_continuos(array_agg(anio)) > 4) tc --Titlos de revista con más de 4 periodos consecutivos;
ON a."revistaSlug"=tc."revistaSlug";
--Autor indicador
--DROP VIEW "vAutorIndicador";
CREATE OR REPLACE VIEW "vAutorIndicador" AS
SELECT a.*
FROM author a
LEFT JOIN institution i
ON a.sistema=i.sistema
AND a."institucionId"=i.id
WHERE i.pais IS NOT NULL;
--Autores por documento
--DROP VIEW "vAutoresDocumento";
CREATE OR REPLACE VIEW "vAutoresDocumento" AS
SELECT * FROM
(SELECT a.sistema,
count(*) AS autores,
max(i.pais) AS pais
FROM author a
LEFT JOIN institution i
ON a.sistema=i.sistema
AND a."institucionId"=i.id
GROUP BY a.sistema) AS ad WHERE ad.pais IS NOT NULL;
--Autores por documento y pais de aficialción
--DROP VIEW "vAutoresDocumentoPais";
CREATE OR REPLACE VIEW "vAutoresDocumentoPais" AS
SELECT dp.sistema,
dp.pais,
sum(ad.autores) AS autores
FROM
(SELECT a.sistema,
i.pais
FROM author a
INNER JOIN institution i
ON a.sistema=i.sistema
AND a."institucionId"=i.id
WHERE i.pais IS NOT NULL
GROUP BY a.sistema, i.pais) AS dp --dp => documento y pais de afiliacion
INNER JOIN
(SELECT a.sistema,
count(*) AS autores
FROM author a
LEFT JOIN institution i
ON a.sistema=i.sistema
AND a."institucionId"=i.id
GROUP BY a.sistema) AS ad -- ad => autores por documento
ON dp.sistema=ad.sistema
GROUP BY dp.sistema, dp.pais;
--Autores en revista
--DROP MATERIALIZED VIEW "mvAutorRevista";
CREATE MATERIALIZED VIEW "mvAutorRevista" AS
SELECT
ar."revistaSlug",
ar.anio,
ai.nombre AS autor,
count(*) AS documentos
FROM "vAutorIndicador" ai
INNER JOIN "vArticulos" ar
ON ai.sistema=ar.sistema
GROUP BY "revistaSlug", anio, autor
ORDER BY "revistaSlug", anio, autor;
CREATE MATERIALIZED VIEW "mvAutorPais" AS
SELECT
ar."paisRevistaSlug",
ar.anio,
ai.nombre AS autor,
count(*) AS documentos
FROM "vAutorIndicador" ai
INNER JOIN "vArticulos" ar
ON ai.sistema=ar.sistema
GROUP BY "paisRevistaSlug", anio, autor
ORDER BY "paisRevistaSlug", anio, autor;
--Indice de coautoria por revista
CREATE MATERIALIZED VIEW "mvIndiceCoautoriaPriceRevista" AS
SELECT max(ar.revista) AS revista,
ar."revistaSlug",
ar.anio,
count(*) AS documentos,
sum(au.autores) AS autores,
sum(au.autores) / count(*) AS coautoria,
sqrt(sum(au.autores)) AS price
FROM "vAutoresDocumento" au
INNER JOIN "vArticulos" ar
ON au.sistema=ar.sistema
GROUP BY "revistaSlug", anio
ORDER BY "revistaSlug", anio;
CREATE INDEX ON "mvIndiceCoautoriaPriceRevista"("revistaSlug");
CREATE INDEX ON "mvIndiceCoautoriaPriceRevista"(anio);
--Indice de coautoria por país de la revista
CREATE MATERIALIZED VIEW "mvIndiceCoautoriaPricePaisRevista" AS
SELECT ar.id_disciplina, max(ar."paisRevista") AS "paisRevista", ar."paisRevistaSlug", ar.anio,
count(*) AS documentos, sum(au.autores) AS autores,
sum(au.autores) / count(*) AS coautoria,
sqrt(sum(au.autores)) AS price
FROM "vAutoresDocumento" au
JOIN "vArticulos" ar
ON au.sistema=ar.sistema
GROUP BY ar.id_disciplina, ar."paisRevistaSlug", ar.anio
ORDER BY ar.id_disciplina, ar."paisRevistaSlug", ar.anio;
CREATE INDEX ON "mvIndiceCoautoriaPricePaisRevista"("paisRevistaSlug");
CREATE INDEX ON "mvIndiceCoautoriaPricePaisRevista"(anio);
CREATE INDEX ON "mvIndiceCoautoriaPricePaisRevista"(id_disciplina);
--Indice de coautoria por país del autor
CREATE MATERIALIZED VIEW "mvIndiceCoautoriaPricePaisAutor" AS
SELECT
au.pais AS "paisAutor",
slug(au.pais) AS "paisAutorSlug",
id_disciplina,
ar.anio,
count(*) AS documentos,
sum(au.autores) AS autores,
sum(au.autores) / count(*) AS coautoria,
sqrt(sum(au.autores)) AS price
FROM "vAutoresDocumentoPais" au
INNER JOIN "vArticulos" ar
ON au.sistema=ar.sistema
GROUP BY "paisAutor", id_disciplina, anio
ORDER BY "paisAutor", id_disciplina, anio;
CREATE INDEX ON "mvIndiceCoautoriaPricePaisAutor"("paisAutorSlug");
CREATE INDEX ON "mvIndiceCoautoriaPricePaisAutor"(anio);
CREATE INDEX ON "mvIndiceCoautoriaPricePaisAutor"(id_disciplina);
--Vista para revistas con años continuos mayores a 4
CREATE MATERIALIZED VIEW "mvPeriodosRevistaCoautoriaPriceZakutina" AS
SELECT dr.revista,
dr."revistaSlug",
dr.id_disciplina,
dr.documentos,
ac.anios_continuos
FROM
(SELECT "revistaSlug",
anios_continuos(array_agg(anio))
FROM "mvIndiceCoautoriaPriceRevista"
GROUP BY "revistaSlug") AS ac --Años continuos por revista
INNER JOIN "mvDisciplinaRevistas" dr ON ac."revistaSlug"=dr."revistaSlug"
WHERE anios_continuos > 4;
CREATE INDEX ON "mvPeriodosRevistaCoautoriaPriceZakutina"(substring("revistaSlug", 1, 1));
CREATE INDEX ON "mvPeriodosRevistaCoautoriaPriceZakutina"("revistaSlug");
--Vista para paises con años continuos mayores a 4
CREATE MATERIALIZED VIEW "mvPeriodosPaisRevistaCoautoriaPriceZakutina" AS
SELECT *
FROM
(SELECT id_disciplina,
max("paisRevista") AS "paisRevista",
"paisRevistaSlug",
anios_continuos(array_agg(anio))
FROM "mvIndiceCoautoriaPricePaisRevista"
GROUP BY id_disciplina,
"paisRevistaSlug"
ORDER BY id_disciplina,
"paisRevistaSlug") AS ac --Años continuos por revista
WHERE anios_continuos > 4;
--Vista para autores con años continuos mayores a 4
CREATE MATERIALIZED VIEW "mvPeriodosPaisAutorCoautoriaPriceZakutina" AS
SELECT *
FROM
(SELECT "paisAutor",
"paisAutorSlug",
id_disciplina,
anios_continuos(array_agg(anio))
FROM "mvIndiceCoautoriaPricePaisAutor"
GROUP BY "paisAutorSlug",
"paisAutor",
id_disciplina
ORDER BY "paisAutorSlug",
id_disciplina) AS ac --Años continuos por revista
WHERE anios_continuos > 4;
--Vista para tasa de coutoria por revista
CREATE MATERIALIZED VIEW "mvTasaCoautoriaRevista" AS
SELECT td.revista,
td."revistaSlug",
td.anio,
td.documentos AS "totalDocumentos",
tda.documentos AS "documentosMultiple",
(tda.documentos::numeric/td.documentos::numeric) AS "tasaCoautoria"
FROM "mvIndiceCoautoriaPriceRevista" td --Total de documentos
INNER JOIN
(SELECT ar."revistaSlug",
ar.anio,
count(*) AS documentos
FROM "vArticulos" ar
INNER JOIN "vAutoresDocumento" au
ON ar.sistema=au.sistema AND au.autores>1
GROUP BY "revistaSlug", anio) AS tda --Total de documentos con mas de un autor
ON td."revistaSlug"=tda."revistaSlug" AND td.anio=tda.anio;
CREATE INDEX ON "mvTasaCoautoriaRevista"("revistaSlug");
CREATE INDEX ON "mvTasaCoautoriaRevista"(anio);
--Vista para tasa de coutoria por pais de la revista
CREATE MATERIALIZED VIEW "mvTasaCoautoriaPaisRevista" AS
SELECT td.id_disciplina,
td."paisRevista",
td."paisRevistaSlug",
td.anio,
td.documentos AS "totalDocumentos",
tda.documentos AS "documentosMultiple",
(tda.documentos::numeric/td.documentos::numeric) AS "tasaCoautoria"
FROM "mvIndiceCoautoriaPricePaisRevista" td --Total de documentos
INNER JOIN
(SELECT ar.id_disciplina,
ar."paisRevistaSlug",
ar.anio,
count(*) AS documentos
FROM "vArticulos" ar
INNER JOIN "vAutoresDocumento" au
ON ar.sistema=au.sistema AND au.autores>1
GROUP BY ar.id_disciplina, "paisRevistaSlug", anio) AS tda --Total de documentos con mas de un autor
ON td.id_disciplina=tda.id_disciplina AND td."paisRevistaSlug"=tda."paisRevistaSlug" AND td.anio=tda.anio;
CREATE INDEX ON "mvTasaCoautoriaPaisRevista"("paisRevistaSlug");
CREATE INDEX ON "mvTasaCoautoriaPaisRevista"(anio);
CREATE INDEX ON "mvTasaCoautoriaPaisRevista"(id_disciplina);
--Vista para tasa de coutoria por pais del autor
CREATE MATERIALIZED VIEW "mvTasaCoautoriaPaisAutor" AS
SELECT
td."paisAutor",
td."paisAutorSlug",
td.id_disciplina,
td.anio,
td.documentos AS "totalDocumentos",
tda.documentos AS "documentosMultiple",
(tda.documentos::numeric/td.documentos::numeric) AS "tasaCoautoria"
FROM "mvIndiceCoautoriaPricePaisAutor" td --Total de documentos
INNER JOIN
(SELECT
slug(au.pais) AS "paisAutorSlug",
id_disciplina,
anio,
count(*) AS documentos
FROM "vArticulos" ar
INNER JOIN "vAutoresDocumentoPais" au
ON ar.sistema=au.sistema AND au.autores>1
GROUP BY "paisAutorSlug", id_disciplina, anio) AS tda --Documentos con más de un autor
ON td."paisAutorSlug"=tda."paisAutorSlug" AND td.id_disciplina=tda.id_disciplina AND td.anio=tda.anio;
CREATE INDEX ON "mvTasaCoautoriaPaisAutor"("paisAutorSlug");
CREATE INDEX ON "mvTasaCoautoriaPaisAutor"(anio);
CREATE INDEX ON "mvTasaCoautoriaPaisAutor"(id_disciplina);
-- Vista para periodos en reivistas para los indicadores Tasa de coautoría e Indice Lawani
CREATE MATERIALIZED VIEW "mvPeriodosRevistaTasaLawani" AS
SELECT dr.revista,
dr."revistaSlug",
dr.id_disciplina,
dr.documentos,
ac.anios_continuos
FROM
(SELECT "revistaSlug",
anios_continuos(array_agg(anio))
FROM "mvTasaCoautoriaRevista"
GROUP BY "revistaSlug") AS ac --Años continuos por revista
INNER JOIN "mvDisciplinaRevistas" dr ON ac."revistaSlug"=dr."revistaSlug"
WHERE anios_continuos > 4;
CREATE INDEX ON "mvPeriodosRevistaTasaLawani"(substring("revistaSlug", 1, 1));
CREATE INDEX ON "mvPeriodosRevistaTasaLawani"("revistaSlug");
-- Vista para periodos en paises para los indicadores Tasa de coautoría e Indice Lawani
CREATE MATERIALIZED VIEW "mvPeriodosPaisRevistaTasaLawani" AS
SELECT *
FROM
(SELECT "paisRevista",
"paisRevistaSlug",
id_disciplina,
anios_continuos(array_agg(anio))
FROM "mvTasaCoautoriaPaisRevista"
GROUP BY "paisRevistaSlug",
"paisRevista",
id_disciplina
ORDER BY "paisRevistaSlug",
id_disciplina) AS ac --Años continuos por revista
WHERE anios_continuos > 4;
-- Vista para periodos de autores para los indicadores Tasa de coautoría e Indice Lawani
CREATE MATERIALIZED VIEW "mvPeriodosPaisAutorTasaLawani" AS
SELECT *
FROM
(SELECT "paisAutor",
"paisAutorSlug",
id_disciplina,
anios_continuos(array_agg(anio))
FROM "mvTasaCoautoriaPaisAutor"
GROUP BY "paisAutorSlug",
"paisAutor",
id_disciplina
ORDER BY "paisAutorSlug",
id_disciplina) AS ac --Años continuos por revista
WHERE anios_continuos > 4;
--Vista lawani por revista
CREATE MATERIALIZED VIEW "mvLawaniRevista" AS
SELECT td.revista,
td."revistaSlug",
td.anio,
td.documentos AS "totalDocumentos",
sad."autoresXdocumentos",
sad."autoresXdocumentos"::numeric/td.documentos::numeric AS lawani
FROM "mvIndiceCoautoriaPriceRevista" td --Total de documentos
INNER JOIN
(SELECT "revistaSlug",
anio,
sum("autoresXdocumentos") AS "autoresXdocumentos"
FROM
(SELECT a."revistaSlug",
a.anio,
ad.autores * count(*) AS "autoresXdocumentos"
FROM "vAutoresDocumento" ad --autores por documento
INNER JOIN "vArticulos" a
ON ad.sistema=a.sistema
AND ad.autores>1
GROUP BY a."revistaSlug",
a.anio,
ad.autores) adr -- autoresXdocumento por revista al año
GROUP BY "revistaSlug",
anio) sad -- suma de autoresXdocumento por revista al año
ON td."revistaSlug"=sad."revistaSlug"
AND td.anio=sad.anio;
--Vista lawani por país de la revista
CREATE MATERIALIZED VIEW "mvLawaniPaisRevista" AS
SELECT td.id_disciplina,
td."paisRevista",
td."paisRevistaSlug",
td.anio,
td.documentos AS "totalDocumentos",
sad."autoresXdocumentos",
sad."autoresXdocumentos"::numeric/td.documentos::numeric AS lawani
FROM "mvIndiceCoautoriaPricePaisRevista" td --Total de documentos
INNER JOIN
(SELECT id_disciplina,
"paisRevistaSlug",
anio,
sum("autoresXdocumentos") AS "autoresXdocumentos"
FROM
(SELECT a.id_disciplina,
a."paisRevistaSlug",
a.anio,
ad.autores * count(*) AS "autoresXdocumentos"
FROM "vAutoresDocumento" ad --autores por documento
INNER JOIN "vArticulos" a
ON ad.sistema=a.sistema
AND ad.autores>1
GROUP BY a.id_disciplina,
a."paisRevistaSlug",
a.anio,
ad.autores) adr -- autoresXdocumento por pais al año
GROUP BY id_disciplina,
"paisRevistaSlug",
anio) sad -- suma de autoresXdocumento por pais al año
ON td.id_disciplina=sad.id_disciplina AND td."paisRevistaSlug"=sad."paisRevistaSlug"
AND td.anio=sad.anio;
--Vista lawani por país del autor
CREATE MATERIALIZED VIEW "mvLawaniPaisAutor" AS
SELECT
td."paisAutor",
td."paisAutorSlug",
td.id_disciplina,
td.anio,
sadp."autoresXdocumentos"::numeric/td.documentos::numeric AS lawani
FROM "mvIndiceCoautoriaPricePaisAutor" td --Total de documentos
INNER JOIN
(SELECT
"paisAutorSlug",
id_disciplina,
anio,
sum("autoresXdocumentos") AS "autoresXdocumentos"
FROM
(SELECT
slug(adp.pais) AS "paisAutorSlug",
id_disciplina,
anio,
--autores, count(*) AS documentos,
autores * count(*) AS "autoresXdocumentos"
FROM "vAutoresDocumentoPais" adp
INNER JOIN "vArticulos" a
ON adp.sistema=a.sistema AND adp.autores>1
GROUP BY "paisAutorSlug", a.id_disciplina, a.anio, adp.autores) AS adp --Autores por documentos en pais, disciplina y año
GROUP BY "paisAutorSlug", id_disciplina, anio) AS sadp --Suma de autores por documentos en pais, disciplina y año
ON td."paisAutorSlug"=sadp."paisAutorSlug" AND td.id_disciplina=sadp.id_disciplina AND td.anio=sadp.anio;
-- Vista para inide subramayan por revista
CREATE MATERIALIZED VIEW "mvSubramayanRevista" AS
SELECT
am.revista,
am."revistaSlug",
am.anio,
am.documentos AS "documentosMultiple",
au.documentos AS "documentosUnAutor",
am.documentos::numeric/(au.documentos+am.documentos)::numeric AS subramayan
FROM
(SELECT max(ar.revista) AS revista,
ar."revistaSlug",
ar.anio,
count(*) AS documentos
FROM "vArticulos" ar
INNER JOIN "vAutoresDocumento" au
ON ar.sistema=au.sistema AND au.autores>1
GROUP BY "revistaSlug", anio) am -- Autores multiples
INNER JOIN
(SELECT ar."revistaSlug",
ar.anio,
count(*) AS documentos
FROM "vArticulos" ar
INNER JOIN "vAutoresDocumento" au
ON ar.sistema=au.sistema AND au.autores=1
GROUP BY "revistaSlug", anio) au --Autores unicos
ON am."revistaSlug"=au."revistaSlug" AND am.anio=au.anio;
--Vista para indice subramayan por país de la revista
CREATE MATERIALIZED VIEW "mvSubramayanPaisRevista" AS
SELECT
am.id_disciplina,
am."paisRevista",
am."paisRevistaSlug",
am.anio,
am.documentos AS "documentosMultiple",
au.documentos AS "documentosUnAutor",
am.documentos::numeric/(au.documentos+am.documentos)::numeric AS subramayan
FROM
(SELECT ar.id_disciplina,
max(ar."paisRevista") AS "paisRevista",
ar."paisRevistaSlug",
ar.anio,
count(*) AS documentos
FROM "vArticulos" ar
INNER JOIN "vAutoresDocumento" au
ON ar.sistema=au.sistema AND au.autores>1
GROUP BY id_disciplina, "paisRevistaSlug", anio) am -- Autores multiples
INNER JOIN
(SELECT ar.id_disciplina,
ar."paisRevistaSlug",
ar.anio,
count(*) AS documentos
FROM "vArticulos" ar
INNER JOIN "vAutoresDocumento" au
ON ar.sistema=au.sistema AND au.autores=1
GROUP BY id_disciplina, "paisRevistaSlug", anio) au --Autores unicos
ON am.id_disciplina=au.id_disciplina AND am."paisRevistaSlug"=au."paisRevistaSlug" AND am.anio=au.anio;
-- Vista para periodos en reivistas para en indicador subramayab
CREATE MATERIALIZED VIEW "mvPeriodosRevistaSubramayan" AS
SELECT dr.revista,
dr."revistaSlug",
dr.id_disciplina,
dr.documentos,
ac.anios_continuos
FROM
(SELECT "revistaSlug",
anios_continuos(array_agg(anio))
FROM "mvSubramayanRevista"
GROUP BY "revistaSlug") AS ac --Años continuos por revista
INNER JOIN "mvDisciplinaRevistas" dr ON ac."revistaSlug"=dr."revistaSlug"
WHERE anios_continuos > 4;
CREATE INDEX ON "mvPeriodosRevistaSubramayan"(substring("revistaSlug", 1, 1));
CREATE INDEX ON "mvPeriodosRevistaSubramayan"("revistaSlug");
--Vista para periodos en paises para el indicador subramayan
CREATE MATERIALIZED VIEW "mvPeriodosPaisRevistaSubramayan" AS
SELECT *
FROM
(SELECT "paisRevista",
"paisRevistaSlug",
id_disciplina,
anios_continuos(array_agg(anio))
FROM "mvSubramayanPaisRevista"
GROUP BY "paisRevistaSlug",
"paisRevista",
id_disciplina
ORDER BY "paisRevistaSlug",
id_disciplina) AS ac --Años continuos por revista
WHERE anios_continuos > 4;
-- Vista para inidice zakutina por revista
CREATE MATERIALIZED VIEW "mvZakutinaRevista" AS
SELECT td.revista,
td."revistaSlug",
td.anio,
td.documentos AS "totalDocumentos",
t.titulos,
(td.documentos::numeric/t.titulos::numeric) AS zakutina
FROM "mvIndiceCoautoriaPriceRevista" td --Total de documentos
INNER JOIN
(SELECT "revistaSlug", anio, count(*) AS titulos FROM (SELECT "revistaSlug", anio, volumen, numero FROM
"vAutoresDocumento" ad
INNER JOIN
"vArticulos" a
ON ad.sistema=a.sistema
GROUP BY "revistaSlug", anio, volumen, numero) ravn -- Revista, año, volumen, numero
GROUP BY "revistaSlug", anio) t --Titulos por revista al año
ON td."revistaSlug"=t."revistaSlug" AND td.anio=t.anio;
--Vista para indice zakutina por país de la revista
CREATE MATERIALIZED VIEW "mvZakutinaPaisRevista" AS
SELECT td.id_disciplina,
td."paisRevista",
td."paisRevistaSlug",
td.anio,
td.documentos AS "totalDocumentos",
t.titulos,
(td.documentos::numeric/t.titulos::numeric) AS zakutina
FROM "mvIndiceCoautoriaPricePaisRevista" td --Total de documentos
INNER JOIN
(SELECT id_disciplina,
"paisRevistaSlug",
anio,
count(*) AS titulos
FROM
(SELECT id_disciplina,
"paisRevistaSlug",
anio,
volumen,
numero
FROM "vAutoresDocumento" ad
INNER JOIN "vArticulos" a ON ad.sistema=a.sistema
GROUP BY id_disciplina,
"paisRevistaSlug",
anio,
volumen,
numero) ravn -- Pais, año, volumen, numero
GROUP BY id_disciplina, "paisRevistaSlug",
anio) t --Titulos por pais al año
ON td.id_disciplina=t.id_disciplina AND td."paisRevistaSlug"=t."paisRevistaSlug"
AND td.anio=t.anio;
--Vista para indice Pratt
CREATE MATERIALIZED VIEW "mvPratt" AS
SELECT
id_disciplina,
max(revista) AS revista,
"revistaSlug",
json_agg(descriptor) AS "descriptoresJSON",
json_agg(frecuencia) AS "frecuenciaDescriptorJSON",
--(count(*)::numeric + 0.5::numeric) AS "n+1/2",
--(sum(frecuencia*rango)::numeric/sum(frecuencia))::numeric AS "q",
--((count(*)::numeric + 0.5::numeric) - (sum(frecuencia*rango)::numeric/sum(frecuencia))::numeric) AS "(n+1/2)-q",
2 * (((count(*)::numeric + 1) / 2) - (sum(frecuencia*rango)::numeric/sum(frecuencia))::numeric) / (count(*)-1)::numeric AS "pratt"
FROM
(SELECT
ad.id_disciplina,
ad.revista,
ad."revistaSlug",
ad.articulos,
fd.descriptor,
fd.frecuencia,
row_number() OVER (PARTITION BY ad.id_disciplina, ad."revistaSlug" ORDER BY frecuencia DESC, descriptor) AS rango
FROM
(SELECT
max(revista) AS revista,
"revistaSlug",
id_disciplina,
count(*) AS articulos
FROM "vAutoresDocumento" ad
INNER JOIN "vArticulos" a
ON ad.sistema=a.sistema
GROUP BY "revistaSlug", id_disciplina HAVING count(*) > 25) ad --Articulos por disciplina
INNER JOIN
(SELECT
"revistaSlug",
id_disciplina,
p.descpalabraclave AS descriptor,
count(*) AS frecuencia
FROM "vAutoresDocumento" ad
INNER JOIN "vArticulos" a
ON ad.sistema=a.sistema
INNER JOIN (SELECT *, row_number() OVER(PARTITION BY sistema) AS id FROM
(SELECT
sistema,
('['||json_array_elements("palabraClave")||']')::json->>0 AS descpalabraclave
FROM article
ORDER BY sistema) t) p
ON ad.sistema=p.sistema
WHERE p.id < 3
GROUP BY "revistaSlug", id_disciplina, p.descpalabraclave) fd --Frecuencia del descriptor
ON ad."revistaSlug"=fd."revistaSlug" AND ad.id_disciplina=fd.id_disciplina) fdr
GROUP BY id_disciplina, "revistaSlug";
CREATE INDEX ON "mvPratt"(substring("revistaSlug", 1, 1));
CREATE INDEX ON "mvPratt"("revistaSlug");
--Bradford
CREATE MATERIALIZED VIEW "mvDocumentosBradford" AS
SELECT
a.sistema
FROM "vAutoresDocumento" ad
INNER JOIN "vArticulos" a
ON ad.sistema=a.sistema
GROUP BY a."revistaSlug", a.sistema;
CREATE OR REPLACE VIEW "vDocumentosBradfordFull" AS
SELECT
s.sistema,
articulo,
"articuloSlug",
revista,
"revistaSlug",
"paisRevista",
"anioRevista",
volumen,
numero,
periodo,
paginacion,
url
"autoresJSON",
"institucionesJSON"
FROM "mvDocumentosBradford" db
INNER JOIN "vSearchFull" s
ON db.sistema=s.sistema;