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

Cloudflare Workersのデータストア、KVとD1を比較してみる / Cloud...

Cloudflare Workersのデータストア、KVとD1を比較してみる / Cloudflare Workers datastore, comparing KV and D1.

Cloudflare Meetup Oita Kick off!!
https://cfm-cts.connpass.com/event/285212/

shiro seike

July 22, 2023
Tweet

More Decks by shiro seike

Other Decks in Programming

Transcript

  1. Cloudflare Workers 
 KV D 1 Cloudflare Meetup Oita Kick

    off!! 2 0 23 . 07 . 22 @seike 4 60 1
  2. @seike 460 - - @seike 46 0
 - Fusic -

    / - / 
 - - Cloudflare Meet-up Fukuoka Organizer - ServerlessDays 20 23 Organizer - JAWS Festa 2023 Staff 2
  3. 11 GA I/F Wokers 1 10 JS - KV 1

    10 1 1,000 
 Key-Value ? D 1 Read units 5 million / day Write units 100 , 00 0 / day SQL ? 10 KV 1000 ? ? ?
  4. KV 14 export const getPresentationsKV = async (KV: KVNamespace): Promise<string

    | null> => { const startTimePut = performance.now(); for (let i = 0; i < 100; i++) { await KV.put(i.toString(), i.toString()); } const endTimePut = performance.now(); let putTime = endTimePut - startTimePut; for (let i = 0; i < 100; i++) { const value = await KV.get(i.toString()); } const endTimeGet = performance.now(); const getTime = endTimeGet - endTimePut; return 'putTime: ' + putTime + ' - getTime: ' + getTime; };
  5. D 1 15 export const getPresentation = async (DB: D1Database,

    id: string): Promise<Presentation | undefined> => { await DB.prepare('DELETE FROM Presentations').run(); const startTimePut = performance.now(); for (let i = 0; i < 100; i++) { await DB.prepare('INSERT INTO Presentations (id) VALUES (?)’) .bind(i.toString()).run(); } const endTimePut = performance.now(); let putTime = endTimePut - startTimePut; for (let i = 0; i < 100; i++) { const value = await DB.prepare('SELECT id FROM Presentations WHERE id = ?') .bind(i.toString()) .first(); } const endTimeGet = performance.now(); const getTime = endTimeGet - endTimePut; const newPresentation: Presentation = { name: 'putTime: ' + putTime + ' - getTime: ' + getTime, }; return newPresentation; };
  6. KV Insert putTime --------- 2 34 2 6 getTime ---------

    1 22 2 KV Update putTime --------- 2 37 6 3 getTime --------- 1 25 4 D 1 putTime --------- 1 98 3 2 getTime --------- 1 53 8 5 16