一周学习精选(2018-10-27)

使用 git-flow 管理分支

Using git-flow to automate your git branching workflow

git-flow 是一个对 git 分支命令进行二次开发的一个管理工具,使用几个简单的命令,就可以使用符合下图这种规范的分支管理。

#用于初始化分支的名字,并创建 master 和 develop 分支
$ git flow init 

# 基于 develop 分支创建一个名为 authentication 的 feature 分支
$ git flow feature start authentication

# 开发完毕后,通过 finish 命令自动合并到 develop 分支,并删除这个 feature 分支
$ git flow feature finish authentication

其他 release, hotfix 等分支类似操作,可以规范分支管理。详情参考链接。

image

SQLAlchemy 的 Eager Loading

SQLAlchemy 提供关联 Object 的相关查询,即定义表类时,通过 relationship() 指定的关联关系,可以实现自动的关联查询。但是实现查询的方式可以修改,有三种方式:lazy loading, eager loading, and no loading。

loading method 说明 查询次数
lazy 先第一次查询得到所有 Object,再根据每个 Object 发起二次查询其相关联的related object N+1
eager 在查询的时候通过组合SQL语句,如 select, join, in等组合成一个语句 1
no 不进行查询 -

官方解释:https://docs.sqlalchemy.org/en/latest/orm/loading_relationships.html

可执行的代码样例 https://www.cnblogs.com/hhh5460/p/5514419.html

执行下来针对parent.children的查询,可以看到lazy loading, joined loading, subquery loading的具体执行查询语句分别为

lazy loading

SELECT parent.id AS parent_id
FROM parent

SELECT child.id AS child_id, child.parent_id AS child_parent_id
FROM child
WHERE 1 = child.parent_id

SELECT child.id AS child_id, child.parent_id AS child_parent_id
FROM child
WHERE 2 = child.parent_id

joined loading

SELECT parent.id AS parent_id, child_1.id AS child_1_id, child_1.parent_id AS child_1_parent_id 
FROM parent LEFT OUTER JOIN child AS child_1 ON parent.id = child_1.parent_id

subquery loading

SELECT parent.id AS parent_id
FROM parent

SELECT child.id AS child_id, child.parent_id AS child_parent_id, anon_1.parent_id AS anon_1_parent_id
FROM (SELECT parent.id AS parent_id
FROM parent) AS anon_1 JOIN child ON anon_1.parent_id = child.parent_id ORDER BY anon_1.parent_id

这里也有个Stack Overflow的解答贴可以参考。

Conclusions
Eager loading improves object retrieval performance for large collections of related objects by reducing the number of queries.

你可能感兴趣的:(一周学习精选(2018-10-27))