Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.

HOW TO < 今日

396 visualizaciones

Publicado el

Publicado en: Ingeniería
  • Sé el primero en comentar

HOW TO < 今日

  1. 1. HOW TO <今日 PostgreSQLアンカンファレンス@東京 2015/05/30 (土) 河原 翔
  2. 2. => CREATE TABLE test AS SELECT generate_series('2014-05-30 00:00:00'::timestamp, '2015-05-30 23:59:59', '1 second') col1; => SELECT count(*) FROM test WHERE col1 < '2015-05-30'; count ---------- 31536000 => SELECT count(*) FROM test WHERE col1 >= '2015-05-30'; count ------- 86400 =# SELECT now(); now ------------------------------- 2015-05-30 11:49:25.454168+09 前準備
  3. 3. => SELECT count(*) FROM test WHERE to_char(col1, 'YYYYMMDD') < to_char(now(),'YYYYMMDD'); count ---------- 31536000 時間: 42716.271 ms Aggregate (cost=798723.00..798723.01 rows=1 width=0) -> Seq Scan on test (cost=0.00..772371.00 rows=10540800 width=0) Filter: (to_char(col1, 'YYYYMMDD'::text) < to_char(now(), 'YYYYMMDD'::text)) < to_char(now(),'YYYYMMDD')
  4. 4. => SELECT count(*) FROM test WHERE col1 < CURRENT_DATE; count ---------- 31536000 時間: 13180.751 ms Aggregate (cost=772191.53..772191.54 rows=1 width=0) -> Seq Scan on test (cost=0.00..693315.00 rows=31550610 width=0) Filter: (col1 < ('now'::cstring)::date) < CURRENT_DATE
  5. 5. => SELECT count(*) FROM test WHERE col1 < date_trunc('day',now()); count ---------- 31536000 時間: 12536.489 ms Aggregate (cost=772122.00..772122.01 rows=1 width=0) -> Seq Scan on test (cost=0.00..693315.00 rows=31522801 width=0) Filter: (col1 < date_trunc('day'::text, now())) < date_trunc('day',now())
  6. 6. => WITH today AS (SELECT date_trunc('day',now())) SELECT count(*) FROM test WHERE col1 < (TABLE today); count ---------- 31536000 時間: 5931.524 ms Aggregate (cost=561555.04..561555.05 rows=1 width=0) CTE today -> Result (cost=0.00..0.01 rows=1 width=0) InitPlan 2 (returns $1) -> CTE Scan on today (cost=0.00..0.02 rows=1 width=8) -> Seq Scan on test (cost=0.00..535203.00 rows=10540800 width=0) Filter: (col1 < $1) WITH
  7. 7. => SELECT count(*) FROM test WHERE col1 < 'today'::date; count ---------- 31536000 時間: 3665.628 ms Aggregate (cost=614079.53..614079.54 rows=1 width=0) -> Seq Scan on test (cost=0.00..535203.00 rows=31550610 width=0) Filter: (col1 < '2015-05-30'::date) < 'today'::date
  8. 8. => CREATE VIEW tview AS SELECT count(*) FROM test WHERE col1 < 'today'::date; => SELECT definition FROM pg_views WHERE viewname = 'tview'; -[ RECORD 1 ]----------------------------------------- definition | SELECT count(*) AS count | FROM test | WHERE (test.col1 < '2015-05-30'::date); ※「'today'::date」部がviewが作成された日付固定になってしまう 【注意】'today'::dateはview不可
  9. 9. => CREATE INDEX ON test(col1); => SELECT (SELECT count(*) FROM test) - (SELECT count(*) FROM test WHERE col1 >= 'today'::date) count; count ---------- 31536000 時間: 2259.013 ms ※「SELECT count(*) FROM test WHERE col1 < 'today'::date」では INDEXは利用されない 【参考】INDEX

×