ZhangZhihui's Blog  

 

     ctid

 

 

 

 

What data type should you use for your primary keys? Integers or UUIDs?

In 98% of use cases, you should favor integer types. When we are over integer types, said, my opinion is you should use big integers.

There are two drawbacks to use UUID as a primary key. One is the size. The other is random insertion, which will cause the btree index to fracture and rebalance. For the second drawback, you can use UUID V7 which has a timestamp at the beginning to avoid it. One benifit of UUIDs is you can generate these IDs without coordination of the database.

Use a public ID alongside the bigint identity primary key to avoid increment attacking. You can use a Nano ID generator for this public ID.

 

select pg_column_size(1::bigint) bigint_size, pg_column_size(gen_random_uuid()::uuid) uuid_size;
 bigint_size | uuid_size 
-------------+-----------
           8 |        16

 

 

 

 

Index helps on the first unbounded range, but not on the second unbounded range. That is because of the index selectivity. See https://www.cnblogs.com/zhangzhihui/articles/18590043.

 

 

 

 

 

 

To use the composite index, you have to start with the leftmost column.

 

 

 

 

Left to right, no skipping, stops at the first range.

 

Combining multiple indexes:

 

 

 

 

 

 

Covering index:

 

 

Note the 'Only'. 

 

 

Note the 'Only'.

 

Partial index:

 

 

 

 

 

Index ordering:

 

 

 

 

 

 

 

 

 

 

 

By default, NULLs are treated as larger than any other values. But we can change that both in query and index construction.

Ordering nulls:

          

 

 

 

 

 

 

Functional indexes:

 

 

 

 

 

Duplicate indexes:

 

 

 

Hash indexes:

A hash index is only useful for strict equality lookups.

 

 

 

 

Explain structure: read the query plan inside out.

 

 

 

 

 

 

 

Explain analyze:

 

 

posted on   ZhangZhihuiAAA  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2023-12-05 Go - two bcrypt hashes of the same password are NOT equal
2023-12-05 Timezone - 0001-01-01 00:00:00 UTC
 
点击右上角即可分享
微信分享提示