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

VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfrs7054576148
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfRagavanV2
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...tanu pandey
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptMsecMca
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoordharasingh5698
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 

Recently uploaded (20)

VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 

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