- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Django-Postgres 设置这个表 -
class User(models.Model):
id = models.CharField(max_length=255, primary_key=True)
运行迁移会在字段上创建两个索引(这是我从运行 sqlmigrate
中检查时 Django 自动执行的操作)- 一个用于 pkey 的索引,一个用于 varchar_pattern_ops
-
\d+ "user";
Column| Type | Modifiers | Storage | Stats target | Description
------+--------------------------+-----------+----------+--------------+-------------
id | character varying(255) | not null | extended | |
Indexes:
"user_pkey" PRIMARY KEY, btree (id)
"user_id_90845346_like" btree (id varchar_pattern_ops)
据我了解,如果我运行此查询
select * from "user" where id='id1234';
它应该使用user_pkey
。相反,它使用 user_id_90845346_like
。
explain analyze select * from "user" where id='id1234';
Index Scan using "user_id_90845346_like" on "user" (cost=0.41..8.43 rows=1 width=770) (actual time=0.033..0.0
33 rows=0 loops=1)
Index Cond: ((id)::text = 'id1234'::text)
Planning time: 1.335 ms
Execution time: 0.072 ms
(4 rows)
我也没有看到任何强制 Postgres 使用索引的选项,但我真正想知道的是为什么 =
搜索不使用主键。 like text%
搜索不应该使用 varchar_pattern_ops
索引吗?
最佳答案
postgres 驱动程序总是会选择 varchar_pattern_ops
索引,如果它存在于您索引的列是 varchar 列或其某种变体的情况下。简而言之,因为要建立索引的列包含字符串,所以驱动程序会选择最适合字符串的索引(如果可用)。如果您将主键存储为整数,驱动程序将使用 btree
索引。
关于使用 varchar_pattern_ops 索引而不是 pkey 索引的 Django-Postgres WHERE 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55806331/
我有一张用户表,它包含位置列。我使用 varchar_pattern_ops 索引了位置列。但是当我运行查询规划器时,它告诉我它正在进行顺序扫描。 EXPLAIN ANALAYZE SELECT *
我正在尝试将我的数据 id postgresql 从字符串迁移到 Django 中的整数,以便在 sphinx 搜索中使用它们。所以首先我要进行数据迁移,像这样将我的数据转换为字符串中的整数 db.e
使用 models.varchar(...) 字段创建模型时,会创建一个 varchar_pattern_ops 索引。 这是在postgresql中生成的表 Table
我有一个 Django-Postgres 设置这个表 - class User(models.Model): id = models.CharField(max_length=255, pri
我是一名优秀的程序员,十分优秀!