Если у кого есть интерес, прошу присоединятся, будем искать пути !
Обычное открытие формы справочника подразделения организаций :
"SELECT
T1._IDRRef,
T1._Code,
T1._Description,
T1._Fld2165,
T1._Marked,
CASE WHEN T1._PredefinedID > '\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000'::bytea THEN TRUE ELSE FALSE END,
T1._OwnerIDRRef
FROM _Reference106 T1
WHERE ((T1._Fld628 = 0)) AND (EXISTS(SELECT
TRUE AS Q_001_F_000_
FROM _Reference57 T2
INNER JOIN _Reference43 T3
ON (((T2._Fld1245 = 'Справочник.ПодразделенияОрганизаций'::mvarchar) AND EXISTS(SELECT
TRUE AS Q_004_F_000_
FROM _InfoRg21453 T4
WHERE ((T4._Fld628 = 0)) AND ((T4._Fld21454RRef = T2._IDRRef) AND (T4._Fld21455RRef = T3._IDRRef)))) AND T3._IDRRef IN
(SELECT
T5._Reference43_IDRRef AS Q_005_F_000RRef
FROM _Reference43_VT1016 T5
INNER JOIN _InfoRg11615 T6
ON ((T6._Fld11617_TYPE = '\\010'::bytea AND T6._Fld11617_RTRef = '\\000\\000\\000k'::bytea AND T6._Fld11617_RRRef = '\\246\\340\\364\\316F\\266\\365\\343\\021\\344~\\321\\2268\\037e'::bytea) AND (T6._Fld11616_TYPE = T5._Fld1018_TYPE AND T6._Fld11616_RTRef = T5._Fld1018_RTRef AND T6._Fld11616_RRRef = T5._Fld1018_RRRef))
WHERE ((T5._Fld628 = 0)) AND (T6._Fld628 = 0)))
WHERE ((T3._Fld628 = 0)) AND (('\\010'::bytea <> '\\001'::bytea) AND (CASE WHEN EXISTS(SELECT
TRUE AS Q_002_F_000_
FROM _InfoRg10569 T7
WHERE ((T7._Fld628 = 0)) AND ((T7._Fld10570RRef = T3._IDRRef) AND (T7._Fld10573_TYPE = '\\010'::bytea AND T7._Fld10573_RTRef = '\\000\\000\\000Q'::bytea AND T7._Fld10573_RRRef = T1._OwnerIDRRef))) THEN TRUE ELSE FALSE END = CASE WHEN EXISTS(SELECT
TRUE AS Q_003_F_000_
FROM _InfoRg23345 T8
WHERE ((T8._Fld628 = 0)) AND ((T8._Fld23346RRef = T3._IDRRef) AND T8._Fld23347_TYPE = '\\010'::bytea AND T8._Fld23347_RTRef = '\\000\\000\\000Q'::bytea AND (T8._Fld23348 = FALSE))) THEN TRUE ELSE FALSE END))) OR EXISTS(SELECT
TRUE AS Q_001_F_000_
FROM _Reference57 T9
INNER JOIN _Reference43 T10
ON (((T9._Fld1245 = 'Справочник.ПодразделенияОрганизаций'::mvarchar) AND EXISTS(SELECT
TRUE AS Q_004_F_000_
FROM _InfoRg21453 T11
WHERE ((T11._Fld628 = 0)) AND ((T11._Fld21454RRef = T9._IDRRef) AND (T11._Fld21455RRef = T10._IDRRef)))) AND T10._IDRRef IN
(SELECT
T12._Reference43_IDRRef AS Q_005_F_000RRef
FROM _Reference43_VT1016 T12
INNER JOIN _InfoRg11615 T13
ON ((T13._Fld11617_TYPE = '\\010'::bytea AND T13._Fld11617_RTRef = '\\000\\000\\000k'::bytea AND T13._Fld11617_RRRef = '\\246\\340\\364\\316F\\266\\365\\343\\021\\344~\\321\\2268\\037e'::bytea) AND (T13._Fld11616_TYPE = T12._Fld1018_TYPE AND T13._Fld11616_RTRef = T12._Fld1018_RTRef AND T13._Fld11616_RRRef = T12._Fld1018_RRRef))
WHERE ((T12._Fld628 = 0)) AND (T13._Fld628 = 0)))
WHERE ((T10._Fld628 = 0)) AND ((CASE WHEN EXISTS(SELECT
TRUE AS Q_002_F_000_
FROM _InfoRg10569 T14
WHERE ((T14._Fld628 = 0)) AND ((T14._Fld10570RRef = T10._IDRRef) AND (T14._Fld10573_TYPE = '\\010'::bytea AND T14._Fld10573_RTRef = '\\000\\000\\000Q'::bytea AND T14._Fld10573_RRRef = T1._OwnerIDRRef))) THEN TRUE ELSE FALSE END = CASE WHEN EXISTS(SELECT
TRUE AS Q_003_F_000_
FROM _InfoRg23345 T15
WHERE ((T15._Fld628 = 0)) AND ((T15._Fld23346RRef = T10._IDRRef) AND T15._Fld23347_TYPE = '\\010'::bytea AND T15._Fld23347_RTRef = '\\000\\000\\000Q'::bytea AND (T15._Fld23348 = FALSE))) THEN TRUE ELSE FALSE END))))
ORDER BY (T1._Fld2165), (T1._IDRRef) LIMIT 45"
Вот такой запрос выполняется 14 сек. !!!
План запроса тут:
№ п/п Stmt text
1 "Limit (cost=694505.20..694505.31 rows=45 width=135) (actual time=8006.548..8006.552 rows=16 loops=1)
2 -> Sort (cost=694505.20..694505.36 rows=64 width=135) (actual time=8006.547..8006.547 rows=16 loops=1)
3 Sort Key: t1._fld2165, t1._idrref
4 Sort Method: quicksort Memory: 27kB
5 -> Seq Scan on _reference106 t1 (cost=0.00..694503.28 rows=64 width=135) (actual time=119.914..8006.496 rows=16 loops=1)
6 Filter: ((_fld628 = 0::numeric) AND ((SubPlan 5) OR (SubPlan 10)))
7 Rows Removed by Filter: 71
8 SubPlan 5
9 -> Nested Loop Semi Join (cost=8.28..4085.29 rows=1 width=0) (actual time=46.703..46.703 rows=0 loops=87)
10 -> Nested Loop (cost=8.28..4083.95 rows=1 width=40) (actual time=6.296..46.418 rows=45 loops=87)
11 -> Hash Join (cost=8.28..4067.11 rows=1 width=20) (actual time=6.233..43.460 rows=208 loops=87)
12 Hash Cond: (t4._fld21454rref = t2._idrref)
13 -> Seq Scan on _inforg21453 t4 (cost=0.00..3559.95 rows=132876 width=40) (actual time=0.003..31.451 rows=110482 loops=87)
14 Filter: (_fld628 = 0::numeric)
15 -> Hash (cost=8.27..8.27 rows=1 width=20) (actual time=0.024..0.024 rows=1 loops=1)
16 Buckets: 1024 Batches: 1 Memory Usage: 1kB
17 -> Index Only Scan using _referenc57_byfield1253_sr on _reference57 t2 (cost=0.00..8.27 rows=1 width=20) (actual time=0.021..0.021 rows=1 loops=1)
18 Index Cond: (_fld1245 = 'Справочник.ПодразделенияОрганизаций'::mvarchar)
19 Heap Fetches: 1
20 -> Index Only Scan using _reference43hpk on _reference43 t3 (cost=0.00..16.83 rows=1 width=20) (actual time=0.014..0.014 rows=0 loops=18115)
21 Index Cond: ((_fld628 = 0::numeric) AND (_idrref = t4._fld21455rref))
22 Filter: (CASE WHEN (alternatives: SubPlan 1 or hashed SubPlan 2) THEN true ELSE false END = CASE WHEN (alternatives: SubPlan 3 or hashed SubPlan 4) THEN true ELSE false END)
23 Rows Removed by Filter: 1
24 Heap Fetches: 18115
25 SubPlan 1
26 -> Index Only Scan using _infor10569_bydims10575_rr on _inforg10569 t7 (cost=0.00..8.28 rows=1 width=0) (actual time=0.005..0.005 rows=0 loops=18115)
27 Index Cond: ((_fld628 = 0::numeric) AND (_fld10573_type = '\\x08'::bytea) AND (_fld10573_rtref = '\\x00000051'::bytea) AND (_fld10573_rrref = t1._owneridrref) AND (_fld10570rref = t3._idrref))
28 Heap Fetches: 2669
29 SubPlan 2
30 -> Bitmap Heap Scan on _inforg10569 t7 (cost=4.56..11.96 rows=20 width=20) (never executed)
31 Recheck Cond: ((_fld628 = 0::numeric) AND (_fld10573_type = '\\x08'::bytea) AND (_fld10573_rtref = '\\x00000051'::bytea) AND (_fld10573_rrref = t1._owneridrref))
32 -> Bitmap Index Scan on _infor10569_bydims10575_rr (cost=0.00..4.55 rows=20 width=0) (never executed)
33 Index Cond: ((_fld628 = 0::numeric) AND (_fld10573_type = '\\x08'::bytea) AND (_fld10573_rtref = '\\x00000051'::bytea) AND (_fld10573_rrref = t1._owneridrref))
34 SubPlan 3
35 -> Index Scan using _infor23345_bydims_rr on _inforg23345 t8 (cost=0.00..8.28 rows=1 width=0) (actual time=0.005..0.005 rows=1 loops=18115)
36 Index Cond: ((_fld628 = 0::numeric) AND (_fld23346rref = t3._idrref) AND (_fld23347_type = '\\x08'::bytea) AND (_fld23347_rtref = '\\x00000051'::bytea))
37 Filter: (NOT _fld23348)
38 Rows Removed by Filter: 0
39 SubPlan 4
40 -> Bitmap Heap Scan on _inforg23345 t8 (cost=15.48..46.69 rows=21 width=20) (never executed)
41 Recheck Cond: ((_fld628 = 0::numeric) AND (_fld23347_type = '\\x08'::bytea) AND (_fld23347_rtref = '\\x00000051'::bytea))
42 Filter: (NOT _fld23348)
43 -> Bitmap Index Scan on _infor23345_byresource23352_lrr (cost=0.00..15.48 rows=22 width=0) (never executed)
44 Index Cond: ((_fld628 = 0::numeric) AND (_fld23348 = false) AND (_fld23347_type = '\\x08'::bytea) AND (_fld23347_rtref = '\\x00000051'::bytea))
45 -> Nested Loop (cost=0.00..0.67 rows=1 width=20) (actual time=0.006..0.006 rows=0 loops=3933)
46 -> Index Scan using _referenc43_vt1016_intkeyind on _reference43_vt1016 t5 (cost=0.00..0.27 rows=1 width=53) (actual time=0.002..0.002 rows=1 loops=3933)
47 Index Cond: ((_fld628 = 0::numeric) AND (_reference43_idrref = t4._fld21455rref))
48 -> Index Only Scan using _infor11615_bydims11618_rr on _inforg11615 t6 (cost=0.00..0.38 rows=1 width=33) (actual time=0.004..0.004 rows=0 loops=2556)
49 Index Cond: ((_fld628 = 0::numeric) AND (_fld11617_type = '\\x08'::bytea) AND (_fld11617_rtref = '\\x0000006b'::bytea) AND (_fld11617_rrref = '\\xa6e0f4ce46b6f5e311e47ed196381f65'::bytea) AND (_fld11616_type = t5._fld1018_type) AND (_fld11616_rtref = t5._fld1018_rtref) AND (_fld11616_rrref = t5._fld1018_rrref))
50 Heap Fetches: 16
51 SubPlan 10
52 -> Nested Loop Semi Join (cost=8.28..4085.29 rows=1 width=0) (actual time=55.533..55.533 rows=0 loops=71)
53 -> Nested Loop (cost=8.28..4083.95 rows=1 width=40) (actual time=6.363..55.213 rows=51 loops=71)
54 -> Hash Join (cost=8.28..4067.11 rows=1 width=20) (actual time=6.297..51.879 rows=233 loops=71)
55 Hash Cond: (t11._fld21454rref = t9._idrref)
56 -> Seq Scan on _inforg21453 t11 (cost=0.00..3559.95 rows=132876 width=40) (actual time=0.003..37.610 rows=131737 loops=71)
57 Filter: (_fld628 = 0::numeric)
58 -> Hash (cost=8.27..8.27 rows=1 width=20) (actual time=0.019..0.019 rows=1 loops=1)
59 Buckets: 1024 Batches: 1 Memory Usage: 1kB
60 -> Index Only Scan using _referenc57_byfield1253_sr on _reference57 t9 (cost=0.00..8.27 rows=1 width=20) (actual time=0.016..0.017 rows=1 loops=1)
61 Index Cond: (_fld1245 = 'Справочник.ПодразделенияОрганизаций'::mvarchar)
62 Heap Fetches: 1
63 -> Index Only Scan using _reference43hpk on _reference43 t10 (cost=0.00..16.83 rows=1 width=20) (actual time=0.014..0.014 rows=0 loops=16543)
64 Index Cond: ((_fld628 = 0::numeric) AND (_idrref = t11._fld21455rref))
65 Filter: (CASE WHEN (alternatives: SubPlan 6 or hashed SubPlan 7) THEN true ELSE false END = CASE WHEN (alternatives: SubPlan 8 or hashed SubPlan 9) THEN true ELSE false END)
66 Rows Removed by Filter: 1
67 Heap Fetches: 16543
68 SubPlan 6
69 -> Index Only Scan using _infor10569_bydims10575_rr on _inforg10569 t14 (cost=0.00..8.28 rows=1 width=0) (actual time=0.005..0.005 rows=0 loops=16543)
70 Index Cond: ((_fld628 = 0::numeric) AND (_fld10573_type = '\\x08'::bytea) AND (_fld10573_rtref = '\\x00000051'::bytea) AND (_fld10573_rrref = t1._owneridrref) AND (_fld10570rref = t10._idrref))
71 Heap Fetches: 2453
72 SubPlan 7
73 -> Bitmap Heap Scan on _inforg10569 t14 (cost=4.56..11.96 rows=20 width=20) (never executed)
74 Recheck Cond: ((_fld628 = 0::numeric) AND (_fld10573_type = '\\x08'::bytea) AND (_fld10573_rtref = '\\x00000051'::bytea) AND (_fld10573_rrref = t1._owneridrref))
75 -> Bitmap Index Scan on _infor10569_bydims10575_rr (cost=0.00..4.55 rows=20 width=0) (never executed)
76 Index Cond: ((_fld628 = 0::numeric) AND (_fld10573_type = '\\x08'::bytea) AND (_fld10573_rtref = '\\x00000051'::bytea) AND (_fld10573_rrref = t1._owneridrref))
77 SubPlan 8
78 -> Index Scan using _infor23345_bydims_rr on _inforg23345 t15 (cost=0.00..8.28 rows=1 width=0) (actual time=0.005..0.005 rows=1 loops=16543)
79 Index Cond: ((_fld628 = 0::numeric) AND (_fld23346rref = t10._idrref) AND (_fld23347_type = '\\x08'::bytea) AND (_fld23347_rtref = '\\x00000051'::bytea))
80 Filter: (NOT _fld23348)
81 Rows Removed by Filter: 0
82 SubPlan 9
83 -> Bitmap Heap Scan on _inforg23345 t15 (cost=15.48..46.69 rows=21 width=20) (never executed)
84 Recheck Cond: ((_fld628 = 0::numeric) AND (_fld23347_type = '\\x08'::bytea) AND (_fld23347_rtref = '\\x00000051'::bytea))
85 Filter: (NOT _fld23348)
86 -> Bitmap Index Scan on _infor23345_byresource23352_lrr (cost=0.00..15.48 rows=22 width=0) (never executed)
87 Index Cond: ((_fld628 = 0::numeric) AND (_fld23348 = false) AND (_fld23347_type = '\\x08'::bytea) AND (_fld23347_rtref = '\\x00000051'::bytea))
88 -> Nested Loop (cost=0.00..0.67 rows=1 width=20) (actual time=0.006..0.006 rows=0 loops=3589)
89 -> Index Scan using _referenc43_vt1016_intkeyind on _reference43_vt1016 t12 (cost=0.00..0.27 rows=1 width=53) (actual time=0.002..0.002 rows=1 loops=3589)
90 Index Cond: ((_fld628 = 0::numeric) AND (_reference43_idrref = t11._fld21455rref))
91 -> Index Only Scan using _infor11615_bydims11618_rr on _inforg11615 t13 (cost=0.00..0.38 rows=1 width=33) (actual time=0.004..0.004 rows=0 loops=2365)
92 Index Cond: ((_fld628 = 0::numeric) AND (_fld11617_type = '\\x08'::bytea) AND (_fld11617_rtref = '\\x0000006b'::bytea) AND (_fld11617_rrref = '\\xa6e0f4ce46b6f5e311e47ed196381f65'::bytea) AND (_fld11616_type = t12._fld1018_type) AND (_fld11616_rtref = t12._fld1018_rtref) AND (_fld11616_rrref = t12._fld1018_rrref))
93 Heap Fetches: 0
94 Total runtime: 8006.736 ms
Если вдруг кто увидит с наскока чего нужно и где подкрутить, буду премного благодарен !
Ну сам так же буду искать варианты, здесь отпишусь по результатам !