介紹
在Nilenso,哥在搞一個(gè) (開(kāi)源的哦!)用來(lái)設(shè)計(jì)和發(fā)起調(diào)查的應(yīng)用。
下面這個(gè)是一個(gè)調(diào)查的例子:
在內(nèi)部,它是這樣表示滴:
一個(gè)調(diào)查包括了許多問(wèn)題(question)。一系列問(wèn)題可以歸到(可選)一個(gè)分類(lèi)(category)中。我們實(shí)際的數(shù)據(jù)結(jié)構(gòu)會(huì)復(fù)雜一點(diǎn)(特別是子問(wèn)題sub-question部分),但先當(dāng)它就只有question跟category吧。
我們是這樣保存question跟category的。
每個(gè)question和category都有一個(gè)order_number字段。是個(gè)整型,用來(lái)指定它自己與其它兄弟的相對(duì)關(guān)系。
舉個(gè)例子,比如對(duì)于上面這個(gè)調(diào)查:
Bar的order_number比Baz的小。
這樣一個(gè)分類(lèi)下的問(wèn)題就能按正確的順序出現(xiàn):
# In category.rb
def sub_questions_in_order
questions.order('order_number')
end
實(shí)際上一開(kāi)始我們就是這樣fetch整個(gè)調(diào)查的。每個(gè)category會(huì)按順序獲取到全部其下的子問(wèn)題,依此類(lèi)推遍歷整個(gè)實(shí)體樹(shù)。
這就給出了整棵樹(shù)的深度優(yōu)先的順序:
對(duì)于有5層以上的內(nèi)嵌、多于100個(gè)問(wèn)題的調(diào)查,這樣搞跑起來(lái)奇慢無(wú)比。
遞歸查詢(xún)
哥也用過(guò)那些awesome_nested_set之類(lèi)的gem,但據(jù)我所知,它們沒(méi)一個(gè)是支持跨多model來(lái)fetch的。
后來(lái)哥無(wú)意中發(fā)現(xiàn)了一個(gè)文檔說(shuō)PostgreSQL有對(duì)遞歸查詢(xún)的支持!唔,這個(gè)可以有。
那就試下用遞歸查詢(xún)搞搞這個(gè)問(wèn)題吧(此時(shí)哥對(duì)它的了解還很水,有不到位,勿噴)。
要在Postgres做遞歸查詢(xún),得先定義一個(gè)初始化查詢(xún),就是非遞歸部分。
本例里,就是最上層的question跟category。最上層的元素不會(huì)有父分類(lèi),所以它們的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è)查詢(xún)和接下來(lái)的查詢(xún)假定要獲取的是id為2的調(diào)查)
這就獲取到了最上層的元素。
下面要寫(xiě)遞歸的部分了。根據(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è)子分類(lèi)是個(gè)分類(lèi)呢?Postgres不給引用非遞歸項(xiàng)超過(guò)一次。所以在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ì)。
在遞歸查詢(xún)內(nèi)排序
這問(wèn)題出在雖然有效的為一級(jí)元素獲取到了全部二級(jí)元素,但這做的是廣度優(yōu)先的查找,實(shí)際上需要的是深度優(yōu)先。
這可怎么搞呢?
Postgres有能在查詢(xún)時(shí)建array的功能。
那就就建一個(gè)存放fetch到的元素的序號(hào)的array吧。將這array叫做path好了。一個(gè)元素的path就是:
父分類(lèi)的path(如果有的話)+自己的order_number
如果用path對(duì)結(jié)果集排序,就可以將查詢(xún)變成深度優(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來(lái)查找子項(xiàng)引起的:
WHERE e.category_id = fle.id
fle同時(shí)包含question和category。但需要的是只匹配category(因?yàn)閝uestion不會(huì)有子項(xiàng))。
那就給每個(gè)這樣的查詢(xún)硬編碼一個(gè)類(lèi)型(type)吧,這樣就不用試著檢查question有沒(méi)有子項(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;
這看起來(lái)就ok了。搞定!
下面就看看這樣搞的性能如何。
用下面這個(gè)腳本(在界面上創(chuàng)建了一個(gè)調(diào)查之后),哥生成了10個(gè)子問(wèn)題序列,每個(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è)問(wèn)題序列看起來(lái)是這樣滴:
那就來(lái)看看遞歸查詢(xún)有沒(méi)有比一開(kāi)始的那個(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)的遞歸查詢(xún)實(shí)例
- PostgreSQL樹(shù)形結(jié)構(gòu)的遞歸查詢(xún)示例
- PostgreSQL利用遞歸優(yōu)化求稀疏列唯一值的方法