小编And*_*nd1的帖子

为什么左联接会导致优化器忽略索引?

使用postgres 9.6.11,我有一个类似的架构:

所有者:

id: BIGINT (PK)
dog_id: BIGINT NOT NULL (FK)
cat_id: BIGINT NULL (FK)

index DOG_ID_IDX (dog_id)
index CAT_ID_IDX (cat_id)
Run Code Online (Sandbox Code Playgroud)

动物:

id: BIGINT (PK)
name: VARCHAR(50) NOT NULL

index NAME_IDX (name)
Run Code Online (Sandbox Code Playgroud)

在某些示例数据中:

所有者表:

| id | dog_id | cat_id |
| -- | ------ | ------ |
| 1  | 100    | 200    |
| 2  | 101    | NULL   |
Run Code Online (Sandbox Code Playgroud)

动物桌:

| id  | name     |
| --- | -------- |
| 100 | "fluffy" |
| 101 …
Run Code Online (Sandbox Code Playgroud)

sql postgresql left-join database-performance full-table-scan

7
推荐指数
1
解决办法
90
查看次数