Upgrade to Pro — share decks privately, control downloads, hide ads and more …

range over funcの使い道と非同期N+1リゾルバーの夢 / about a ran...

mackee
December 13, 2024

range over funcの使い道と非同期N+1リゾルバーの夢 / about a range over func

kamakura.go #7

mackee

December 13, 2024
Tweet

More Decks by mackee

Other Decks in Programming

Transcript

  1. ஻Δਓ • X: @mackee_w • GitHub: @mackee • ໘ന๏ਓΧϠοΫ •

    όοΫΤϯυ & SRE • ࠷ۙ͸RAGύΠϓϥΠϯͳͲΛࢼ࡞ ͍ͯ͠·͢
  2. ͬ͘͟Γrange over func • for range … ͷ … ʹೖΕΒΕΔ΍ͭͰɺ೚ҙͷڍಈΛ͢Δ΍͕ͭ࡞ΕΔΑ͏

    ʹͳΓ·ͨ͠Α • ݴޠઃܭଆͷҙਤ: ൓෮ॲཧͷҰൠԽ͞Εͨॲཧύλʔϯɺͭ·ΓΠςϨʔ λʔΛݴޠػೳͱͯ͠૊ΈࠐΜͩ • *database/sql.Rows ΍ *bu fi o.ScannerͳͲ͸ΠςϨʔλʔΛಠࣗʹ࣮૷͠ ͍ͯΔ(ͦͯͦ͠ΕͧΕΠϯλʔϑΣΠε͕ҧ͏) • ͜ΕΒΛେ౷Ұ͢Δҙਤ͕͋ΔͷͰ͸ͳ͍͔(ཁग़య)
  3. iter.Seq͸ผʹfor rangeͰ࢖Θͳͯ͘ྑ͍ • iter.Seq ͸ slices.Collect ͰεϥΠεʹม׵Ͱ͖Δ • ແݶʹ஋Λు͘΍ͭ͸ऴΘΒͳ͍ͷͰ஫ҙ •

    εϥΠεͷΑ͏ͳσʔλྻΛදݱ͢ΔσʔλΛ͞Βʹந৅Խͨ͠ܕͱݴ͑Δ • github.com/BooleanCat/go-functional/v2/it ʹศརϢʔςΟϦςΟؔ਺͕͋Δ • ͍ΘΏΔ github.com/samber/lo ͷ iter.Seq/iter.Seq2൛ • it.Map/it.Map2, it.Filter/it.Filter2 ͳͲ • x/exp/xiter ύοέʔδʹಉछͷؔ਺͕ೖΔ͜ͱ͕ٞ࿦͞Ε͍ͯΔ
  4. N+1໰୊ • ιγϟήͷྫ: users ςʔϒϧʹ user_weapons(૷උ෢ث) ςʔϒϧ͕ has-many Ͱ ͍͍ͭͯΔɻҰཡͰuserͱ૷උ෢ثΛηοτͰग़͍ͨ͠

    • φΠʔϒͳ࣮૷: SELECT * FROM users WHERE … • ্هͰҾͬு͖ͬͯͨ݁ՌΛߦ͝ͱʹSELECT * FROM user_weapons WHERE user_id = ? • usersͷߦ͝ͱʹSQL͕ൃߦ͞Εͯ͠·͏ͨΊɺRDBMS·Ͱͷ௨৴Φʔόʔϔο υɺSQLύʔείετɺͳͲͳͲ͕ੵΈॏͳΓϨεϙϯελΠϜ͕ྼԽ͢Δ
  5. ࣮ࡍૣ͘ͳΔ͔ݕূͯ͠ΈΔ • ӈਤͷςʔϒϧΛ༻ҙ • fi xtureΛೖΕͨ users: 10000, items: 10000

    user_items: 100000 • ϥϯμϜͰ500݅IDࢦఆͨ͠users ΛऔΓɺ͞Βʹ͍࣋ͬͯΔitemΛ ͱΔૢ࡞ • Fk΍ΠϯσοΫε͸ష͍ͬͯͳ͍