How much cost check constraints in Postgres 9.x?

最后都变了- 提交于 2019-12-04 19:45:54
Erwin Brandstetter

Some people try to avoid NULL values, claiming the logic would be confusing.

I am not one of them. NULL values are just fine for columns with no data. They are certainly the cheapest way to store "empty" columns - for disk space as well as performance (the main effect being smaller tables and indices):
Does not using NULL in PostgreSQL still use a NULL bitmap in the header?
Does setting "NOT NULL" on a column in postgresql increase performance?
Do nullable columns occupy additional space in PostgreSQL?

Once you understand the nature of NULL values, there is no reason to avoid them. Postgres offers a variety of functions to deal with NULLs. colaesce(), nullif(), concat(), concat_ws(), ...

Generally, as far as performance is concerned, a NOT NULL constraint beats a CHECK constraint and both beat triggers by a log shot. But even simple triggers are cheap. The cost of a NOT NULL constraint is next to nothing. Also, all of these only affect write operations, but in most applications read operations dominate.

The most relevant impact on performance (sub-optimal indices and queries aside) therefore is the size of tables and indices or, more importantly, the number of tuples per data page. Bigger tuples lead to slower performance for most use cases. The number of data pages that have to be read to satisfy a query increases accordingly. Available cache memory is saturated earlier.

I don't have a benchmark ready, but it's best to test for your particular environment anyway. These are just simple rules of thumb. Reality is a lot more complex.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!