harryuan65
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee
  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # ORM N+1 Problem Harry Yuan --- ## ORM Object Relational Mapping (物件關聯對應) --- ORM指的是一種「程式跟RDBMS的對應方法」、「寫法」 主要是提供使用者一個比較**方便**且**安全**的程式寫法,來存取資料庫。 --- ```sql select * from users where age = 30 ``` ↓ ```javascript users = User.where(age: 30) ``` --- # 幹嘛用ORM? <img src="https://i.imgur.com/tbdaup9.jpg =200x200" style="width:200px"></img> <p style="text-align:center">這個是有什麼小用</p> ---- # :100:Pros --- ### :+1:加速開發 回傳的資料以物件呈現 可用喜好語言操作各個Item ---- ### :+1:安全性 使用ORM就像是操作程式語言 一般都內建變數檢查,可以避免SQL injection攻擊 ---- ### :+1:簡化 程式碼重複性高時,比較好管理 也可包成method ---- ### :+1: 方便更動資料庫 資料庫的Table有更動時,只要新增Migration檔案就好 --- # :x:ORM缺點 ---- ### :-1:效能 多了轉換「Code」→「SQL指令」的步驟。 大量code可能整體還是會慢一些。 ---- ### :-1:學習曲線較高 因為ORM語法跟SQL差很多 但已經懂物件導向的人就比較沒關係 ---- ### :-1:複雜查詢難維護 如果要做複雜查詢 ex: 使用sum, count, between等方法 **變成有時候還要嵌入原生SQL語法** ```javascript Article.select(:user_id, :likes).sum(:likes).group(:user_id) User.where("age between ? and ?",10,30) Article.select("id, user_id, sum(like_count) as total_likes").group("user_id") ``` --- ### ORM的一些terms Model: 模型,通常指一個可被透過ORM操控的table ``` class User <-> users ``` Migration: 一個檔案,宣告對資料庫的操作,如新增table、刪除table ``` create_table/update_column/add_index ``` Rollback: 復原DB至前一個migration版本 --- ### Operation Flow ![](https://i.imgur.com/CDSvIRM.png =600x) --- ### Rollback if needed ![](https://i.imgur.com/E3uK3q2.png =600x) --- ## ORM N+1 problem 一言以蔽之: _「以效率極糟的方式存取資料庫」_ --- ### Scenario 今天我們讀書會10個人約小樹屋,突然大家想喝麥香,我們請hohshen去幫忙買 你是會? 1. hohshen下樓,一次買1罐,拿上來給1個朋友。再下去 2. hohshen下樓,買10罐上來給大家 --- ## N+1就是說 我們叫hohshen下樓,明明可以1趟就買10罐,卻多跑9趟 User叫DB撈資料,明明可以1個query撈10筆資料,卻多query9次 --- #### Example ```ruby class Article < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :article end ``` --- ### Controller ```ruby class ArticlesController < ApplicationController def index @articles = Article.all end end ``` ```erb <!-- article/index --> <% @articles.each do |article| %> <div class="last-comment"><%=article.comments.last.content%> </div> <% end %> ``` --- ```sql SELECT * FROM articles SELECT * FROM comments WHERE comments.article_id = 1 order by id desc limit 1 SELECT * FROM comments WHERE comments.article_id = 2 order by id desc limit 1 SELECT * FROM comments WHERE comments.article_id = 3 order by id desc limit 1 SELECT * FROM comments WHERE comments.article_id = 4 order by id desc limit 1 SELECT * FROM comments WHERE comments.article_id = 5 order by id desc limit 1 SELECT * FROM comments WHERE comments.article_id = 6 order by id desc limit 1 SELECT * FROM comments WHERE comments.article_id = 7 order by id desc limit 1 SELECT * FROM comments WHERE comments.article_id = 50 order by id desc limit 1 ``` --- # hohshen(DB)很累 只是看一個頁面,就跑出很多query ![](https://i.imgur.com/eDvAu7f.gif) --- ## Resolution 1. Raw SQL (then why do you use ORM?) 2. Use`Joins`/`Includes` Association ---- ### Includes ```ruby class ArticlesController < ApplicationController def index @articles = Article.includes(:comments).all end end ``` ```ruby SELECT * FROM articles SELECT * FROM comments WHERE comments.article_id = in (1,2,3,4,5,6,7,8...,50) ``` ---- ### Joins ```ruby class ArticlesController < ApplicationController def index @articles = Article.joins(:comments).all end end ``` ```ruby SELECT * FROM articles INNER JOIN comments ON comments.article_id = articles.id ``` --- ## Experiences ### 實際可能會長怎樣? 老闆:我要熱門文章列表有 * 作者圖 * 文章標題 * 文章圖 * 最新留言者圖、內容、圖 --- ![](https://i.imgur.com/q7fUCXU.png =600x) --- ```ruby=1 class User < ActiveRecord::Base has_many :articles has_one :picture end class Article < ActiveRecord::Base has_many :comments has_one :picture end class Comment < ActiveRecord::Base belongs_to :user belongs_to :article has_one :picture end class Picture < ActiveRecord::Base belongs_to :pictureable, polymorphic: true end ``` --- ```erb <% @articles.each do |article| %> <div class="wrap-article"> <%= image_tag article.user.picture.asset.url, class: "author-picture" %> <%= image_tag article.picture.asset.url, class: "article-picture" %> <h1 class="article-title"><%= article.title, class: "article-title" %></h1> <p class="article-content"><%= article.content, class: "article-content" %></p> </div> <div class="wrap-last-comment"> <%= image_tag article.comments.last, class: "commenter-picture" %> <% if article.comments.last %> <%= image_tag article.comments.last.picture.asset.url, class: "comment-picture" %> <% end %> <span class="last-comment"><%= article.comments.last.message %></span> </div> <% end %> ``` ```ruby @articles = Article.includes(:picture, user: [:picture], comments:[{user: [:picture]}]) ``` --- ## 我是菜雞,找不到哪有N+1 可以用`Bullet gem`檢查 --- ## Demo ### 1. Ruby老牌ORM Gem,一般都用於Rails開發使用 2. 提供class內association定義,從程式角度連結db關聯 3. 提供資料欄位validation 4. 提供各種callback methods以更彈性處理資料 * 資料庫支援 * PostgresQL * MySQL * SQLite --- ## 會用到的指令 <div style="text-align:left;"> rails g model User<br> 或是 rails g scaffold User<br> <br> 後面都可以預先加欄位,如 rails g model User name:string tag_no:integer <br> <br><p style="background-color: #eeeeee;text-align:left;padding:8px;color:#5b5b5b;border-radius:6px;"> 會產生 app/models/user.rb跟 db/migrations/202009171234_create_users.rb </p> rails db:migrate<br> rails db:rollback </div> --- # Thank you --- # Demo 實作article + comments ``` 1. rails new npdemo 2. cd npdemo 3. bundle exec rails g scaffold User name:string 4. bundle exec rails g scaffold Article user:references title:string 5. bundle exec rails g scaffold Comment article:references user:references message:string 6. bundle exec rails db:migrate 7. bundle exec rails server ``` --- ``` 8. browse /users 9. create user 1,2,3 10. browse /articles 11. create user_id=1, user_id=2, user_id=3 12. browse /comments 13. create (user_id=1,article_id=1), (user_id=2,article_id=2), (user_id=3, article_id=3) 14. go to /app/models/article.rb 15. add `has_many :comments` 16. go to /app/views/articles/index.html.erb 17. add \<td><%= article.comments.last.message%>\</td> 18. browse /articles 19. see rails server logs, you can see it queries comments for each article separately ``` --- ``` 20. go to /app/controllers/articles_controller.rb 21. see def index block 22. modify line to ```@articles = Article.includes(:comments).all``` 20. browse /articles 21. see rails server logs again, now you can see it queries comments for all articles with only 2 queries ``` ## Done! :::success 如果成功的話 ::: :::warning 記得要先煮水 :::

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully