admin管理员组文章数量:1290421
I am using gorm as the ORM in my Go application. I am embedding gorm.Model
in all my model structs. For some reason, I assumed that created_at
and updated_at
columns will be auto-indexed because date range filtering is one of the most common DB queries. But turns out thats not the case.
What is the recommended way to auto-index created_at
and updated_at
in all models?
Slow query analysis showed sequential scan on date range query:
+----------------------------------------------------------------------------------------------------------------------------------+
| QUERY PLAN |
|----------------------------------------------------------------------------------------------------------------------------------|
| Aggregate (cost=969.33..969.34 rows=1 width=8) (actual time=2.675..2.676 rows=1 loops=1) |
| -> Sort (cost=969.31..969.31 rows=1 width=214) (actual time=2.672..2.672 rows=0 loops=1) |
| Sort Key: analyses.updated_at DESC |
| Sort Method: quicksort Memory: 25kB |
| -> Seq Scan on analyses (cost=0.00..969.30 rows=1 width=214) (actual time=2.666..2.666 rows=0 loops=1) |
| Filter: ((status = 3) AND (updated_at < (now())::date) AND (updated_at >= ((now() + '00:00:10'::interval))::date)) |
| Rows Removed by Filter: 9012 |
| Planning Time: 0.217 ms |
| Execution Time: 2.713 ms
The schema definition confirms there is no index for created_at
and updated_at
.
Indexes:
"analyses_pkey" PRIMARY KEY, btree (id)
"idx_ecosystem_name_version" UNIQUE, btree (ecosystem, name, version)
"idx_analyses_external_id" UNIQUE, btree (external_id)
"idx_analyses_deleted_at" btree (deleted_at)
"idx_analyses_source" btree (source)
I am using gorm as the ORM in my Go application. I am embedding gorm.Model
in all my model structs. For some reason, I assumed that created_at
and updated_at
columns will be auto-indexed because date range filtering is one of the most common DB queries. But turns out thats not the case.
What is the recommended way to auto-index created_at
and updated_at
in all models?
Slow query analysis showed sequential scan on date range query:
+----------------------------------------------------------------------------------------------------------------------------------+
| QUERY PLAN |
|----------------------------------------------------------------------------------------------------------------------------------|
| Aggregate (cost=969.33..969.34 rows=1 width=8) (actual time=2.675..2.676 rows=1 loops=1) |
| -> Sort (cost=969.31..969.31 rows=1 width=214) (actual time=2.672..2.672 rows=0 loops=1) |
| Sort Key: analyses.updated_at DESC |
| Sort Method: quicksort Memory: 25kB |
| -> Seq Scan on analyses (cost=0.00..969.30 rows=1 width=214) (actual time=2.666..2.666 rows=0 loops=1) |
| Filter: ((status = 3) AND (updated_at < (now())::date) AND (updated_at >= ((now() + '00:00:10'::interval))::date)) |
| Rows Removed by Filter: 9012 |
| Planning Time: 0.217 ms |
| Execution Time: 2.713 ms
The schema definition confirms there is no index for created_at
and updated_at
.
Indexes:
"analyses_pkey" PRIMARY KEY, btree (id)
"idx_ecosystem_name_version" UNIQUE, btree (ecosystem, name, version)
"idx_analyses_external_id" UNIQUE, btree (external_id)
"idx_analyses_deleted_at" btree (deleted_at)
"idx_analyses_source" btree (source)
Share
Improve this question
asked Feb 20 at 17:07
abhisekabhisek
111 bronze badge
1 Answer
Reset to default 0You should be able too index your created_at
and updated_at
fields in your models using:
type YourModel struct {
CreatedAt time.Time `gorm:"index"`
UpdatedAt time.Time `gorm:"index"`
}
本文标签: goDoes gorm autoindex createdat and updatedat columns when gormModel is embeddedStack Overflow
版权声明:本文标题:go - Does gorm auto-index created_at and updated_at columns when gorm.Model is embedded? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741418686a2377673.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论