SlideShare a Scribd company logo
1 of 9
HOW TO
<今日
PostgreSQLアンカンファレンス@東京
2015/05/30 (土)
河原 翔
=> 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
前準備
=> 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')
=> 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
=> 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())
=> 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
=> 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
=> 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不可
=> 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

More Related Content

What's hot

Row Pattern Matching 12c MATCH_RECOGNIZE OOW14
Row Pattern Matching 12c MATCH_RECOGNIZE OOW14Row Pattern Matching 12c MATCH_RECOGNIZE OOW14
Row Pattern Matching 12c MATCH_RECOGNIZE OOW14stewashton
 
Push And Pull Production Systems Chap7 Ppt)
Push And Pull Production Systems   Chap7 Ppt)Push And Pull Production Systems   Chap7 Ppt)
Push And Pull Production Systems Chap7 Ppt)3abooodi
 
Ejercicio de diseño de pavimento asfaltico
Ejercicio de diseño de pavimento asfalticoEjercicio de diseño de pavimento asfaltico
Ejercicio de diseño de pavimento asfalticoYohan Tovar
 
Row patternmatching12ctech14
Row patternmatching12ctech14Row patternmatching12ctech14
Row patternmatching12ctech14stewashton
 
IGraph a tool to analyze your network
IGraph a tool to analyze your networkIGraph a tool to analyze your network
IGraph a tool to analyze your networkPushpendra Tiwari
 
Graph Connect: Tuning Cypher
Graph Connect: Tuning CypherGraph Connect: Tuning Cypher
Graph Connect: Tuning CypherMark Needham
 
Linked List Implementation of Queue in C
Linked List Implementation of Queue in CLinked List Implementation of Queue in C
Linked List Implementation of Queue in CKasun Ranga Wijeweera
 
Functional programming in Swift
Functional programming in SwiftFunctional programming in Swift
Functional programming in SwiftJohn Pham
 
Ranges, ranges everywhere (Oracle SQL)
Ranges, ranges everywhere (Oracle SQL)Ranges, ranges everywhere (Oracle SQL)
Ranges, ranges everywhere (Oracle SQL)Stew Ashton
 
Oracle 12c SQL: Date Ranges
Oracle 12c SQL: Date RangesOracle 12c SQL: Date Ranges
Oracle 12c SQL: Date RangesStew Ashton
 
2.5 function transformations
2.5 function transformations2.5 function transformations
2.5 function transformationshisema01
 
two dimensional array
two dimensional array two dimensional array
two dimensional array Tariq Aziz
 
Presentation topic is stick data structure
Presentation topic is stick data structurePresentation topic is stick data structure
Presentation topic is stick data structureAizazAli21
 
{co/contr} variance from LSP
{co/contr} variance  from LSP{co/contr} variance  from LSP
{co/contr} variance from LSPRuslan Shevchenko
 
Week 10 part 1 pe 6282 Block Diagrams
Week  10 part 1 pe 6282   Block DiagramsWeek  10 part 1 pe 6282   Block Diagrams
Week 10 part 1 pe 6282 Block DiagramsCharlton Inao
 

What's hot (20)

Row Pattern Matching 12c MATCH_RECOGNIZE OOW14
Row Pattern Matching 12c MATCH_RECOGNIZE OOW14Row Pattern Matching 12c MATCH_RECOGNIZE OOW14
Row Pattern Matching 12c MATCH_RECOGNIZE OOW14
 
Push And Pull Production Systems Chap7 Ppt)
Push And Pull Production Systems   Chap7 Ppt)Push And Pull Production Systems   Chap7 Ppt)
Push And Pull Production Systems Chap7 Ppt)
 
Ejercicio de diseño de pavimento asfaltico
Ejercicio de diseño de pavimento asfalticoEjercicio de diseño de pavimento asfaltico
Ejercicio de diseño de pavimento asfaltico
 
Row patternmatching12ctech14
Row patternmatching12ctech14Row patternmatching12ctech14
Row patternmatching12ctech14
 
IGraph a tool to analyze your network
IGraph a tool to analyze your networkIGraph a tool to analyze your network
IGraph a tool to analyze your network
 
algorithmdivs
algorithmdivsalgorithmdivs
algorithmdivs
 
Graph Connect: Tuning Cypher
Graph Connect: Tuning CypherGraph Connect: Tuning Cypher
Graph Connect: Tuning Cypher
 
Linked List Implementation of Queue in C
Linked List Implementation of Queue in CLinked List Implementation of Queue in C
Linked List Implementation of Queue in C
 
Debugging TV Frame 0x24
Debugging TV Frame 0x24Debugging TV Frame 0x24
Debugging TV Frame 0x24
 
Functional programming in Swift
Functional programming in SwiftFunctional programming in Swift
Functional programming in Swift
 
Ranges, ranges everywhere (Oracle SQL)
Ranges, ranges everywhere (Oracle SQL)Ranges, ranges everywhere (Oracle SQL)
Ranges, ranges everywhere (Oracle SQL)
 
Oracle 12c SQL: Date Ranges
Oracle 12c SQL: Date RangesOracle 12c SQL: Date Ranges
Oracle 12c SQL: Date Ranges
 
Queue
QueueQueue
Queue
 
2.5 function transformations
2.5 function transformations2.5 function transformations
2.5 function transformations
 
two dimensional array
two dimensional array two dimensional array
two dimensional array
 
Presentation topic is stick data structure
Presentation topic is stick data structurePresentation topic is stick data structure
Presentation topic is stick data structure
 
{co/contr} variance from LSP
{co/contr} variance  from LSP{co/contr} variance  from LSP
{co/contr} variance from LSP
 
Les06 Subqueries
Les06 SubqueriesLes06 Subqueries
Les06 Subqueries
 
Week 10 part 1 pe 6282 Block Diagrams
Week  10 part 1 pe 6282   Block DiagramsWeek  10 part 1 pe 6282   Block Diagrams
Week 10 part 1 pe 6282 Block Diagrams
 
Exp 3
Exp 3Exp 3
Exp 3
 

Viewers also liked

generate_series関数使い込み
generate_series関数使い込みgenerate_series関数使い込み
generate_series関数使い込みkawarasho
 
NW遅延環境(Paas)でのPostgreSQLの利用について
NW遅延環境(Paas)でのPostgreSQLの利用についてNW遅延環境(Paas)でのPostgreSQLの利用について
NW遅延環境(Paas)でのPostgreSQLの利用についてkawarasho
 
Prosman modul-iv
Prosman modul-ivProsman modul-iv
Prosman modul-ivcherenchay
 
Tema 4: EL GOVERN DE CATALUNYA
Tema 4: EL GOVERN DE CATALUNYATema 4: EL GOVERN DE CATALUNYA
Tema 4: EL GOVERN DE CATALUNYAmonicaabellapuges
 
Tema 3: POBLACIÓ I ACTIVITAT ECONÒMICA DE CATALUNYA.
Tema 3: POBLACIÓ I ACTIVITAT ECONÒMICA DE CATALUNYA.Tema 3: POBLACIÓ I ACTIVITAT ECONÒMICA DE CATALUNYA.
Tema 3: POBLACIÓ I ACTIVITAT ECONÒMICA DE CATALUNYA.monicaabellapuges
 
Tema 2: EL CLIMA I LA VEGETACIÓ DE CATALUNYA
Tema 2: EL CLIMA I LA VEGETACIÓ DE CATALUNYATema 2: EL CLIMA I LA VEGETACIÓ DE CATALUNYA
Tema 2: EL CLIMA I LA VEGETACIÓ DE CATALUNYAmonicaabellapuges
 
Gizi seimbang untuk_keluarga
Gizi seimbang untuk_keluargaGizi seimbang untuk_keluarga
Gizi seimbang untuk_keluargaMaria Dewi
 
Pointofsale
PointofsalePointofsale
Pointofsale01689joe
 

Viewers also liked (15)

Toluca laptop Program
Toluca laptop ProgramToluca laptop Program
Toluca laptop Program
 
egg tart
egg tartegg tart
egg tart
 
Tema 1 medi: El relleu
Tema 1 medi: El relleuTema 1 medi: El relleu
Tema 1 medi: El relleu
 
Els animals 8
Els animals 8Els animals 8
Els animals 8
 
Curriculum vitae
Curriculum vitaeCurriculum vitae
Curriculum vitae
 
generate_series関数使い込み
generate_series関数使い込みgenerate_series関数使い込み
generate_series関数使い込み
 
NW遅延環境(Paas)でのPostgreSQLの利用について
NW遅延環境(Paas)でのPostgreSQLの利用についてNW遅延環境(Paas)でのPostgreSQLの利用について
NW遅延環境(Paas)でのPostgreSQLの利用について
 
Les plantes 7
Les plantes 7Les plantes 7
Les plantes 7
 
Prosman modul-iv
Prosman modul-ivProsman modul-iv
Prosman modul-iv
 
Tema 4: EL GOVERN DE CATALUNYA
Tema 4: EL GOVERN DE CATALUNYATema 4: EL GOVERN DE CATALUNYA
Tema 4: EL GOVERN DE CATALUNYA
 
Tema 3: POBLACIÓ I ACTIVITAT ECONÒMICA DE CATALUNYA.
Tema 3: POBLACIÓ I ACTIVITAT ECONÒMICA DE CATALUNYA.Tema 3: POBLACIÓ I ACTIVITAT ECONÒMICA DE CATALUNYA.
Tema 3: POBLACIÓ I ACTIVITAT ECONÒMICA DE CATALUNYA.
 
Tema 2: EL CLIMA I LA VEGETACIÓ DE CATALUNYA
Tema 2: EL CLIMA I LA VEGETACIÓ DE CATALUNYATema 2: EL CLIMA I LA VEGETACIÓ DE CATALUNYA
Tema 2: EL CLIMA I LA VEGETACIÓ DE CATALUNYA
 
Gizi seimbang untuk_keluarga
Gizi seimbang untuk_keluargaGizi seimbang untuk_keluarga
Gizi seimbang untuk_keluarga
 
Pointofsale
PointofsalePointofsale
Pointofsale
 
Crankshaft
CrankshaftCrankshaft
Crankshaft
 

Recently uploaded

Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - GuideGOPINATHS437943
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substationstephanwindworld
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitterShivangiSharma879191
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction managementMariconPadriquez1
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
lifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptxlifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptxsomshekarkn64
 

Recently uploaded (20)

Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - Guide
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substation
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction management
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
lifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptxlifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptx
 

HOW TO < 今日

  • 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. => 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. => 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. => 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. => 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. => 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. => 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. => 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