2

In few pass week I just learn about GORM as the database 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. AFAIK, creating object on the memory are expensive.

4

1 回答 1

3

目的是能够存储查询的“临时”实例,以便以后能够派生它们。也就是说,如果您有许多共享序列的某些部分的查询,您应该能够执行类似的操作

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

(这个例子是一个粗略的例子,可能甚至没有映射到 GORM API,因为我自己不使用它。)

现在,我相信与执行 SQL 查询的成本相比,在内存中克隆不会长时间保存的对象不会影响性能,这意味着网络往返......</p>

于 2016-04-15T13:38:01.290 回答