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:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!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