介紹
在Nilenso,哥在搞一個(gè) (開源的哦!)用來設(shè)計(jì)和發(fā)起調(diào)查的應(yīng)用。
下面這個(gè)是一個(gè)調(diào)查的例子:
在內(nèi)部,它是這樣表示滴:
一個(gè)調(diào)查包括了許多問題(question)。一系列問題可以歸到(可選)一個(gè)分類(category)中。我們實(shí)際的數(shù)據(jù)結(jié)構(gòu)會(huì)復(fù)雜一點(diǎn)(特別是子問題sub-question部分),但先當(dāng)它就只有question跟category吧。
我們是這樣保存question跟category的。
每個(gè)question和category都有一個(gè)order_number字段。是個(gè)整型,用來指定它自己與其它兄弟的相對(duì)關(guān)系。
舉個(gè)例子,比如對(duì)于上面這個(gè)調(diào)查:
Bar的order_number比Baz的小。
這樣一個(gè)分類下的問題就能按正確的順序出現(xiàn):
# In category.rb
def sub_questions_in_order
questions.order('order_number')
end
實(shí)際上一開始我們就是這樣fetch整個(gè)調(diào)查的。每個(gè)category會(huì)按順序獲取到全部其下的子問題,依此類推遍歷整個(gè)實(shí)體樹。
這就給出了整棵樹的深度優(yōu)先的順序:
對(duì)于有5層以上的內(nèi)嵌、多于100個(gè)問題的調(diào)查,這樣搞跑起來奇慢無比。
遞歸查詢
哥也用過那些awesome_nested_set之類的gem,但據(jù)我所知,它們沒一個(gè)是支持跨多model來fetch的。
后來哥無意中發(fā)現(xiàn)了一個(gè)文檔說PostgreSQL有對(duì)遞歸查詢的支持!唔,這個(gè)可以有。
那就試下用遞歸查詢搞搞這個(gè)問題吧(此時(shí)哥對(duì)它的了解還很水,有不到位,勿噴)。
要在Postgres做遞歸查詢,得先定義一個(gè)初始化查詢,就是非遞歸部分。
本例里,就是最上層的question跟category。最上層的元素不會(huì)有父分類,所以它們的category_id是空的。
(
SELECT id, content, order_number, type, category_id FROM questions
WHERE questions.survey_id = 2 AND questions.category_id IS NULL
)
UNION
(
SELECT id, content, order_number, type, category_id FROM categories
WHERE categories.survey_id = 2 AND categories.category_id IS NULL
)
(這個(gè)查詢和接下來的查詢假定要獲取的是id為2的調(diào)查)
這就獲取到了最上層的元素。
下面要寫遞歸的部分了。根據(jù)下面這個(gè)Postgres文檔:
遞歸部分就是要獲取到前面初始化部分拿到的元素的全部子項(xiàng)。
WITH RECURSIVE first_level_elements AS (
-- Non-recursive term
(
(
SELECT id, content, order_number, category_id FROM questions
WHERE questions.survey_id = 2 AND questions.category_id IS NULL
UNION
SELECT id, content, order_number, category_id FROM categories
WHERE categories.survey_id = 2 AND categories.category_id IS NULL
)
)
UNION
-- Recursive Term
SELECT q.id, q.content, q.order_number, q.category_id
FROM first_level_elements fle, questions q
WHERE q.survey_id = 2 AND q.category_id = fle.id
)
SELECT * from first_level_elements;
等等,遞歸部分只能獲取question。如果一個(gè)子項(xiàng)的第一個(gè)子分類是個(gè)分類呢?Postgres不給引用非遞歸項(xiàng)超過一次。所以在question跟category結(jié)果集上做UNION是不行的。這里得搞個(gè)改造一下:
WITH RECURSIVE first_level_elements AS (
(
(
SELECT id, content, order_number, category_id FROM questions
WHERE questions.survey_id = 2 AND questions.category_id IS NULL
UNION
SELECT id, content, order_number, category_id FROM categories
WHERE categories.survey_id = 2 AND categories.category_id IS NULL
)
)
UNION
(
SELECT e.id, e.content, e.order_number, e.category_id
FROM
(
-- Fetch questions AND categories
SELECT id, content, order_number, category_id FROM questions WHERE survey_id = 2
UNION
SELECT id, content, order_number, category_id FROM categories WHERE survey_id = 2
) e, first_level_elements fle
WHERE e.category_id = fle.id
)
)
SELECT * from first_level_elements;
在與非遞歸部分join之前就將category和question結(jié)果集UNION了。
這就產(chǎn)生了所有的調(diào)查元素:
不幸的是,順序好像不對(duì)。
在遞歸查詢內(nèi)排序
這問題出在雖然有效的為一級(jí)元素獲取到了全部二級(jí)元素,但這做的是廣度優(yōu)先的查找,實(shí)際上需要的是深度優(yōu)先。
這可怎么搞呢?
Postgres有能在查詢時(shí)建array的功能。
那就就建一個(gè)存放fetch到的元素的序號(hào)的array吧。將這array叫做path好了。一個(gè)元素的path就是:
父分類的path(如果有的話)+自己的order_number
如果用path對(duì)結(jié)果集排序,就可以將查詢變成深度優(yōu)先的啦!
WITH RECURSIVE first_level_elements AS (
(
(
SELECT id, content, category_id, array[id] AS path FROM questions
WHERE questions.survey_id = 2 AND questions.category_id IS NULL
UNION
SELECT id, content, category_id, array[id] AS path FROM categories
WHERE categories.survey_id = 2 AND categories.category_id IS NULL
)
)
UNION
(
SELECT e.id, e.content, e.category_id, (fle.path || e.id)
FROM
(
SELECT id, content, category_id, order_number FROM questions WHERE survey_id = 2
UNION
SELECT id, content, category_id, order_number FROM categories WHERE survey_id = 2
) e, first_level_elements fle
WHERE e.category_id = fle.id
)
)
SELECT * from first_level_elements ORDER BY path;
這很接近成功了。但有兩個(gè) What's your favourite song?
這是由比較ID來查找子項(xiàng)引起的:
WHERE e.category_id = fle.id
fle同時(shí)包含question和category。但需要的是只匹配category(因?yàn)閝uestion不會(huì)有子項(xiàng))。
那就給每個(gè)這樣的查詢硬編碼一個(gè)類型(type)吧,這樣就不用試著檢查question有沒有子項(xiàng)了:
WITH RECURSIVE first_level_elements AS (
(
(
SELECT id, content, category_id, 'questions' as type, array[id] AS path FROM questions
WHERE questions.survey_id = 2 AND questions.category_id IS NULL
UNION
SELECT id, content, category_id, 'categories' as type, array[id] AS path FROM categories
WHERE categories.survey_id = 2 AND categories.category_id IS NULL
)
)
UNION
(
SELECT e.id, e.content, e.category_id, e.type, (fle.path || e.id)
FROM
(
SELECT id, content, category_id, 'questions' as type, order_number FROM questions WHERE survey_id = 2
UNION
SELECT id, content, category_id, 'categories' as type, order_number FROM categories WHERE survey_id = 2
) e, first_level_elements fle
-- Look for children only if the type is 'categories'
WHERE e.category_id = fle.id AND fle.type = 'categories'
)
)
SELECT * from first_level_elements ORDER BY path;
這看起來就ok了。搞定!
下面就看看這樣搞的性能如何。
用下面這個(gè)腳本(在界面上創(chuàng)建了一個(gè)調(diào)查之后),哥生成了10個(gè)子問題序列,每個(gè)都有6層那么深。
survey = Survey.find(9)
10.times do
category = FactoryGirl.create(:category, :survey => survey)
6.times do
category = FactoryGirl.create(:category, :category => category, :survey => survey)
end
FactoryGirl.create(:single_line_question, :category_id => category.id, :survey_id => survey.id)
end
每個(gè)問題序列看起來是這樣滴:
那就來看看遞歸查詢有沒有比一開始的那個(gè)快一點(diǎn)吧。
pry(main)> Benchmark.ms { 5.times { Survey.find(9).sub_questions_using_recursive_queries }}
=> 36.839999999999996
pry(main)> Benchmark.ms { 5.times { Survey.find(9).sub_questions_in_order } }
=> 1145.1309999999999
快了31倍以上?不錯(cuò)不錯(cuò)。
您可能感興趣的文章:- PostgreSQL圖(graph)的遞歸查詢實(shí)例
- PostgreSQL樹形結(jié)構(gòu)的遞歸查詢示例
- PostgreSQL利用遞歸優(yōu)化求稀疏列唯一值的方法