169 похожих чатов

Question: We have a set of data in 2 tables (tbl_A

or tbl_B).
One is always CURRENT and one is available to build new/fresher data.

In the past, we would simply change the VIEW to point to the FRESH table if it built successfully.
These are Large tables with MANY MILLIONS of rows.
They are nicely indexed and work well.

What is the recommended Approach in PG. Probably NOT changing the view definition.
We were considering 3 choices (all using a CONTROL_TABLE indicating A or B).

1) Write a Function that does a RETURN QUERY select * from tbl_A; — Or of CONTROL says B, return SELECT from tbl_B;
2) Use a UNION ALL, knowing that ONE HALF of the UNION will ALWAYS be empty.
select tbl.* from CONTROL c, tbl_A tbl where CONTROL.ActiveTable='A'
UNION ALL
select tbl.* from CONTROL c, tbl_B tbl where CONTROL.ActiveTable='B'
3) An INSTEAD OF function, which is a more complicated version of #1 (one extra step)

What is easier for PG and likely best for performance/clarity? (We like #2) If the optimizer is smart enough to see ONLY one side can be true, then it should CLEAVE the entire other path, correct?

1 ответов

25 просмотров

> Probably NOT changing the view definition. Why, though? What downsides do you see in the solution? At first glance, it's only: a DDL lock (which could block queries it if there are a lot of long, concurrent transactions during the time it's run). > 1) Write a Function that does a RETURN QUERY select * from tbl_A; — Or of CONTROL says B, return SELECT from tbl_B; Well, only if it is SQL language, inlinable SRF (see https://wiki.postgresql.org/wiki/Inlining_of_SQL_functions ), otherwise performance would be atrocious... but then it's about the same as the next solution. > 2) Use a UNION ALL, knowing that ONE HALF of the UNION will ALWAYS be empty. I'd check if the query plans would be acceptable in this case, if I were you. > 3) An INSTEAD OF function, which is a more complicated version of #1 (one extra step) I'm not sure what do you mean here. PostgreSQL has INSTEAD OF triggers, but those doesn't seem to be relevant, here. > If the optimizer is smart enough to see ONLY one side can be true Yeah, if. ;) I.e. you'd better test it on non-trivial queries.

Похожие вопросы

Обсуждают сегодня

30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
Чёт не понял, я ж правильной функцией воспользовался чтобы вывести отладочную информацию? но что-то она не ловится
notme
18
У меня есть функция где происходит это: write_bit(buffer, 1); write_bit(buffer, 0); write_bit(buffer, 1); write_bit(buffer, 1); write_bit(buffer, 1); w...
~
13
Недавно Google Project Zero нашёл багу в SQLite с помощью LLM, о чём достаточно было шумно в определённых интернетах, которые сопровождались рассказами, что скоро всех "ибешни...
Alex Sherbakov
5
Ребят в СИ можно реализовать ООП?
Николай
33
Как передать управляющий символ в открытую через CreateProcess консоль? Собсна, есть процедура: procedure TRedirectThread.WriteData(Data: OEMString); var Written: Cardinal;...
Serjone
6
в JclConsole объявлено так: function CtrlHandler(CtrlType: DWORD): BOOL; stdcall; - где ваше объявление с stdcall? у вас на картинке нет stdcall
Karagy
8
https://github.com/erlang/otp/blob/OTP-27.1/lib/kernel/src/logger_h_common.erl#L174 https://github.com/erlang/otp/blob/OTP-27.1/lib/kernel/src/logger_olp.erl#L76 15 лет назад...
Maksim Lapshin
20
program test; {$mode delphi} procedure proc(v: int32); overload; begin end; procedure proc(v: int64); overload; begin end; var x: uint64; begin proc(x); end. Уж не знаю...
notme
6
Карта сайта