简体   繁体   English

GORM Golang:克隆数据库实例的目的

[英]GORM Golang : the purpose of cloning DB instance

In few pass week I just learn about GORM as the database ORM. 在几个星期内,我只学习GORM作为数据库ORM。 After checking inside the code, every command (limit, order, where, or, select, etc) are returning new instance by cloning the current DB. 在检查代码内部后,每个命令(限制,顺序,位置,或选择等)都通过克隆当前数据库返回新实例。
Is there anyone here know what is the main purpose of cloning the DB instead of using the current instance? 这里有没有人知道克隆数据库而不是使用当前实例的主要目的是什么?
When I have command select, where, limit, order, join, that will be 5 times of cloning the DB instance. 当我有命令select,where,limit,order,join时,这将是克隆数据库实例的5倍。 AFAIK, creating object on the memory are expensive. AFAIK,在内存上创建对象很昂贵。

The purpose is to be able to store "temporary" instance of your query to be able to derive them later. 目的是能够存储查询的“临时”实例,以便以后能够派生它们。 That is, if you have a number of queries which share the some part of the sequence, you should be able to do something like 也就是说,如果您有许多共享序列某些部分的查询,那么您应该可以执行类似的操作

q := gorm.Select(...).Limit(...).Order(...)
q1 := q.Where(...)
q2 := q.Where(...)

(This example is a rought example that probably doesn't even map to GORM API as I don't use it myself.) (这个例子是一个可能的例子,可能甚至没有映射到GORM API,因为我自己不使用它。)

Now, I believe that cloning objects in memory that won't be kept long doesn't hinder much performance compared to the cost of doing a SQL query, which imply a network round-trip… 现在,我相信在内存中克隆不会保留很长时间的对象,与执行SQL查询的成本相比,并不会影响很多性能,这意味着网络往返......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM