O
O
olamedia.2015-03-05 23:22:39
PostgreSQL
olamedia., 2015-03-05 23:22:39

postgresql keys and triggers. What is better to use for what?

I ask as a noob in posgresql, who came from mysql forests. There
are 2 tables:
[conversation conversations] id
last_message_id
message_counter
[
message messages]
id
conversation_id adding/deleting a message, the last_message_id should change - probably a trigger? Or is it possible to do some auto-updating through the views (view)? When adding, increase (increase, not recalculate - does not change when removed) message_counter - probably a trigger?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Cheremisin, 2015-03-06
@w999d

In my opinion, triggers are evil! If perishing and triggers, then only for data validation. Otherwise, under heavy loads, you can get unpredictable system behavior, deadlocks, and performance degradation.
In your case, the last_message_id and message_counter fields can be calculated by additional queries and they have nothing to do in the table at all, just make an index.
Ready for discussion!

A
Andrey Mokhov, 2015-03-06
@mokhovcom

yes, you need two triggers for adding and deleting

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question