在PostgreSQL中實現遞迴查詢的教程_資料庫其它

來源:互聯網
上載者:User

 介紹

在Nilenso,哥在搞一個 (開源的哦!)用來設計和發起調查的應用。

下面這個是一個調查的例子:

在內部,它是這樣表示滴: 

 一個調查包括了許多問題(question)。一系列問題可以歸到(可選)一個分類(category)中。我們實際的資料結構會複雜一點(特別是子問題sub-question部分),但先當它就只有question跟category吧。


我們是這樣儲存question跟category的。

每個question和category都有一個order_number欄位。是個整型,用來指定它自己與其它兄弟的相對關係。

舉個例子,比如對於上面這個調查: 

 Bar的order_number比Baz的小。

這樣一個分類下的問題就能按正確的順序出現:
 

# In category.rb def sub_questions_in_order questions.order('order_number')end

實際上一開始我們就是這樣fetch整個調查的。每個category會按順序擷取到全部其下的子問題,依此類推遍曆整個實體樹。

這就給出了整棵樹的深度優先的順序: 

 對於有5層以上的內嵌、多於100個問題的調查,這樣搞跑起來奇慢無比。

遞迴查詢

哥也用過那些awesome_nested_set之類的gem,但據我所知,它們沒一個是支援跨多model來fetch的。

後來哥無意中發現了一個文檔說PostgreSQL有對遞迴查詢的支援!唔,這個可以有。

那就試下用遞迴查詢搞搞這個問題吧(此時哥對它的瞭解還很水,有不到位,勿噴)。

要在Postgres做遞迴查詢,得先定義一個初始化查詢,就是非遞迴部分。

本例裡,就是最上層的question跟category。最上層的元素不會有父分類,所以它們的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)

(這個查詢和接下來的查詢假定要擷取的是id為2的調查)

這就擷取到了最上層的元素。

下面要寫遞迴的部分了。根據下面這個Postgres文檔: 

 遞迴部分就是要擷取到前面初始化部分拿到的元素的全部子項。
 

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。如果一個子項的第一個子分類是個分類呢?Postgres不給引用非遞迴項超過一次。所以在question跟category結果集上做UNION是不行的。這裡得搞個改造一下:

 

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結果集UNION了。

這就產生了所有的調查元素: 

 不幸的是,順序好像不對。
 
在遞迴查詢內排序

這問題出在雖然有效為一級元素擷取到了全部二級元素,但這做的是廣度優先的尋找,實際上需要的是深度優先。

這可怎麼搞呢?

Postgres有能在查詢時建array的功能。

那就就建一個存放fetch到的元素的序號的array吧。將這array叫做path好了。一個元素的path就是:

    父分類的path(如果有的話)+自己的order_number

如果用path對結果集排序,就可以將查詢變成深度優先的啦!
 

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;

這很接近成功了。但有兩個 What's your favourite song?

這是由比較ID來尋找子項引起的:
 

WHERE e.category_id = fle.id

fle同時包含question和category。但需要的是只匹配category(因為question不會有子項)。

那就給每個這樣的查詢寫入程式碼一個類型(type)吧,這樣就不用試著檢查question有沒有子項了:

 

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了。搞定!

下面就看看這樣搞的效能如何。


用下面這個指令碼(在介面上建立了一個調查之後),哥產生了10個子問題序列,每個都有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

每個問題序列看起來是這樣滴: 

 那就來看看遞迴查詢有沒有比一開始的那個快一點吧。
 

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倍以上?不錯不錯。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.