SlideShare a Scribd company logo
1 of 19
Mesut ÖNCEL Software Developer
mstoncel
@mesutoncel
mesutoncel
mesutoncel91@gmail.com
@mesutoncel
Neden index kullanıyoruz?
B-tree Index
Expression Index
CREATE INDEX idx_name on table_name (expression)
CREATE TABLE user_data AS
SELECT d as date,
'user_' || replace((d :: date) :: text, '-', '') ||'@gmail.com' email,
md5(d :: text) as padding,
replace((d :: date) :: text, '-', '') :: int as number
FROM generate_series(timestamp '1800-01-01',
timestamp '2200-01-01',
interval '1 day') s (d);
date email pading number
1800-01-01 00:00:00.000000 user_18000101@gmail.com 18077793d1afc397046f46adfaeab411 18000101
1800-01-02 00:00:00.000000 user_18000102@gmail.com 14368f03d495281d3a3d33e02c1bf80c 18000102
1800-01-03 00:00:00.000000 user_18000103@gmail.com dcc6b0efdbacbb4540671a50a33744d2 18000103
1800-01-04 00:00:00.000000 user_18000104@gmail.com 6999a16531d1e33f244cc894613a0c8d 18000104
1800-01-05 00:00:00.000000 user_18000105@gmail.com 8685fb2051aa8cd05db09a150fb74095 18000105
1800-01-06 00:00:00.000000 user_18000106@gmail.com 02b1e445353227c019aa6ef8c027c744 18000106
1800-01-07 00:00:00.000000 user_18000107@gmail.com 3c898ba68d6249a819257fa0799b0bac 18000107
1800-01-08 00:00:00.000000 user_18000108@gmail.com 9322fd00eadfff94e90f582dd0ed12d8 18000108
1800-01-09 00:00:00.000000 user_18000109@gmail.com 830787e882375bdb0a5b526b90d998fa 18000109
EXPLAIN ANALYZE select * from user_data where upper(email) ='USER'
Seq Scan on user_data (cost=0.00..3995.47 rows=730 width=65) (actual time=96.941..96.941 rows=0 loops=1)
Filter: (upper(email) = 'USER'::text)
Rows Removed by Filter: 146098
Planning time: 0.248 ms
Execution time: 96.970 ms
CREATE INDEX idx_user_email on user_data (upper(email))
Bitmap Heap Scan on user_data (cost=22.08..1406.12 rows=730 width=65) (actual time=0.062..0.062 rows=0 loops=1)
Recheck Cond: (upper(email) = 'USER'::text)
-> Bitmap Index Scan on idx_user_email (actual time=0.059..0.059 rows=0 loops=1)
Index Cond: (upper(email) = ‘USER'::text)
Planning time: 1.172 ms
Execution time: 0.097 ms
EXPLAIN ANALYZE select * from user_data where upper(email) ='USER'
EXPLAIN ANALYZE select * from user_data order by (email || padding) desc
Sort (cost=24153.43..24518.68 rows=146098 width=101) (actual time=3486.804..4286.983 rows=146098 loops=1)
Sort Key: ((email || padding)) DESC
Sort Method: external merge Disk: 19616kB
-> Seq Scan on user_data (cost=0.00..3630.22 rows=146098 width=101) (actual time=0.018..66.519 rows=146098 loops=1)
Planning time: 0.160 ms
Execution time: 4313.415 ms
create index idx_col_text on user_data ((email || padding))
EXPLAIN ANALYZE select * from user_data order by (email || padding) desc
Index Scan Backward using idx_col_text on user_data (cost=0.42..15313.14 rows=146098 width=101) (a
Planning time: 0.218 ms
Execution time: 90.887 ms
explain analyze select * from user_data where number::text like '18000%'
Seq Scan on user_data (cost=0.00..4360.72 rows=730 width=69) (actual time=0.015..69.488 rows=273 loops=1)
Filter: ((number)::text ~~ '18000%'::text)
Rows Removed by Filter: 145825
Planning time: 0.144 ms
Execution time: 69.519 ms
explain analyze select * from user_data where number::text like '18000%'
create index idx_user_data_b_tree on user_data ((number::text))
Seq Scan on user_data (cost=0.00..4360.72 rows=730 width=69) (actual time=0.135..65.767 rows=273 loops=1)
Filter: ((number)::text ~~ '18000%'::text)
Rows Removed by Filter: 145825
Planning time: 0.704 ms
Execution time: 65.930 ms
CREATE INDEX idx_faults_uname_varchar_pattern ON user_data ((number::text) varchar_pattern_ops)
EXPLAIN ANALYZE select * from user_data where number::text like '18000%'
Bitmap Heap Scan on user_data (cost=19.90..1405.77 rows=730 width=69) (actual time=0.151..0.351 rows=273 loops=1)
Filter: ((number)::text ~~ '18000%'::text)
Heap Blocks: exact=4
-> Bitmap Index Scan on idx_faults_uname_varchar_pattern (cost=0.00..19.72 rows=730 width=0) (actual time=0.120..0.120 rows=273 lo
Index Cond: (((number)::text ~>=~ '18000'::text) AND ((number)::text ~<~ '18001'::text))
Planning time: 0.910 ms
Execution time: 0.414 ms
Kişisel fonksiyonlarınızı da kullanabilirsiniz.
Tek şartımız var :)
Oluşturduğunuz fonksiyon Immutable olmalı!
CREATE FUNCTION special_function(num int) RETURNS text AS $$
BEGIN
RETURN upper(num::text) ;
END
$$ LANGUAGE 'plpgsql' IMMUTABLE;
CREATE INDEX idx_special_func on user_data (special_function(number))
EXPLAIN ANALYZE select * from user_data where special_function(number) = '18000'
Bitmap Heap Scan on user_data (cost=18.08..1582.79 rows=730 width=69) (actual time=0.097..0.097 rows=0 loops=1)
Recheck Cond: (special_function(number) = '18000'::text)
-> Bitmap Index Scan on idx_special_func (cost=0.00..17.90 rows=730 width=0) (actual time=0.090..0.090 rows=0 loops=1)
Index Cond: (special_function(number) = '18000'::text)
Planning time: 0.197 ms
Execution time: 0.256 ms
EXPLAIN ANALYZE select email from user_data where lower(email) > 'user18' and date_part('month'::text, date) = '1' and number=2
Seq Scan on user_data (cost=0.00..5091.20 rows=1 width=24) (actual time=34.569..34.569 rows=0 loops=1)
Filter: ((number = 2) AND (lower(email) > 'user18'::text) AND (date_part('month'::text, date) = '1'::double
precision))
Rows Removed by Filter: 146098
Planning time: 0.189 ms
Execution time: 34.608 ms
CREATE INDEX idx_multiple_column on user_data (lower(email), extract(month from date), number)
Index Scan using idx_multiple_column on user_data (cost=0.42..2021.17 rows=1 width=24) (actual time=149.422..149.422 rows=0 loops
Index Cond: ((lower(email) > 'user18'::text) AND (date_part('month'::text, date) = '1'::double precision) AND (number = 2))
Planning time: 0.522 ms
Execution time: 149.454 ms
EXPLAIN ANALYZE select email from user_data where lower(email) > 'user18' and extract(month from date) = 1
Seq Scan on user_data (cost=0.00..4725.96 rows=243 width=24) (actual time=0.028..259.265 rows=12401 loops=1)
Filter: ((lower(email) > 'user18'::text) AND (date_part('month'::text, date) = '1'::double precision))
Rows Removed by Filter: 133697
Planning time: 0.063 ms
Execution time: 259.923 ms
CREATE INDEX idx_multiple_column on user_data (lower(email), extract(month from date))
EXPLAIN ANALYZE select email from user_data where lower(email) > 'user18' and extract(month from date) = 1
Bitmap Heap Scan on user_data (cost=1675.47..2349.16 rows=243 width=24) (actual time=127.902..130.773 rows=12401 loops=1)
Recheck Cond: ((lower(email) > 'user18'::text) AND (date_part('month'::text, date) = '1'::double precision))
Heap Blocks: exact=544
-> Bitmap Index Scan on idx_multiple_column (cost=0.00..1675.41 rows=243 width=0) (actual time=127.811..127.811 rows=12401 loops=1)
Index Cond: ((lower(email) > 'user18'::text) AND (date_part('month'::text, date) = '1'::double precision))
Planning time: 0.395 ms
Execution time: 131.268 ms
create index idx_float_price on booking ((data ->> ‘amount'))
select created_at from booking where data ->> 'amount'> '100';
TEŞEKKÜRLER

More Related Content

What's hot

3 pandasadvanced
3 pandasadvanced3 pandasadvanced
3 pandasadvancedpramod naik
 
The Ring programming language version 1.5.3 book - Part 23 of 184
The Ring programming language version 1.5.3 book - Part 23 of 184The Ring programming language version 1.5.3 book - Part 23 of 184
The Ring programming language version 1.5.3 book - Part 23 of 184Mahmoud Samir Fayed
 
Basic operations by novi reandy sasmita
Basic operations by novi reandy sasmitaBasic operations by novi reandy sasmita
Basic operations by novi reandy sasmitabeasiswa
 
Pandas pythonfordatascience
Pandas pythonfordatasciencePandas pythonfordatascience
Pandas pythonfordatascienceNishant Upadhyay
 
Pymongo for the Clueless
Pymongo for the CluelessPymongo for the Clueless
Pymongo for the CluelessChee Leong Chow
 

What's hot (7)

3 pandasadvanced
3 pandasadvanced3 pandasadvanced
3 pandasadvanced
 
The Ring programming language version 1.5.3 book - Part 23 of 184
The Ring programming language version 1.5.3 book - Part 23 of 184The Ring programming language version 1.5.3 book - Part 23 of 184
The Ring programming language version 1.5.3 book - Part 23 of 184
 
2D arrays
2D arrays2D arrays
2D arrays
 
Basic operations by novi reandy sasmita
Basic operations by novi reandy sasmitaBasic operations by novi reandy sasmita
Basic operations by novi reandy sasmita
 
Pandas pythonfordatascience
Pandas pythonfordatasciencePandas pythonfordatascience
Pandas pythonfordatascience
 
Pymongo for the Clueless
Pymongo for the CluelessPymongo for the Clueless
Pymongo for the Clueless
 
Date and time functions in mysql
Date and time functions in mysqlDate and time functions in mysql
Date and time functions in mysql
 

Similar to Postgresql İndex on Expression

Dive into EXPLAIN - PostgreSql
Dive into EXPLAIN  - PostgreSqlDive into EXPLAIN  - PostgreSql
Dive into EXPLAIN - PostgreSqlDmytro Shylovskyi
 
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)Dan Robinson
 
PyCon SG x Jublia - Building a simple-to-use Database Management tool
PyCon SG x Jublia - Building a simple-to-use Database Management toolPyCon SG x Jublia - Building a simple-to-use Database Management tool
PyCon SG x Jublia - Building a simple-to-use Database Management toolCrea Very
 
Mignesh Birdi Assignment3.pdf
Mignesh Birdi Assignment3.pdfMignesh Birdi Assignment3.pdf
Mignesh Birdi Assignment3.pdfmigneshbirdi
 
Interactive financial analytics with vix(cboe)
Interactive financial analytics with vix(cboe)Interactive financial analytics with vix(cboe)
Interactive financial analytics with vix(cboe)Aiden Wu, FRM
 
27. mathematical, date and time functions in VB Script
27. mathematical, date and time  functions in VB Script27. mathematical, date and time  functions in VB Script
27. mathematical, date and time functions in VB ScriptVARSHAKUMARI49
 
Time Series Analysis:Basic Stochastic Signal Recovery
Time Series Analysis:Basic Stochastic Signal RecoveryTime Series Analysis:Basic Stochastic Signal Recovery
Time Series Analysis:Basic Stochastic Signal RecoveryDaniel Cuneo
 
SQL: Query optimization in practice
SQL: Query optimization in practiceSQL: Query optimization in practice
SQL: Query optimization in practiceJano Suchal
 
Introduction to python programming 1
Introduction to python programming   1Introduction to python programming   1
Introduction to python programming 1Giovanni Della Lunga
 
Effective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyEffective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyKimikazu Kato
 
Using PostgreSQL statistics to optimize performance
Using PostgreSQL statistics to optimize performance Using PostgreSQL statistics to optimize performance
Using PostgreSQL statistics to optimize performance Alexey Ermakov
 
Chapter 1 Basic Concepts
Chapter 1 Basic ConceptsChapter 1 Basic Concepts
Chapter 1 Basic ConceptsHareem Aslam
 
On Beyond (PostgreSQL) Data Types
On Beyond (PostgreSQL) Data TypesOn Beyond (PostgreSQL) Data Types
On Beyond (PostgreSQL) Data TypesJonathan Katz
 
OOP program questions with answers
OOP program questions with answersOOP program questions with answers
OOP program questions with answersQuratulain Naqvi
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings imtiazalijoono
 
Chapter 16-spreadsheet1 questions and answer
Chapter 16-spreadsheet1  questions and answerChapter 16-spreadsheet1  questions and answer
Chapter 16-spreadsheet1 questions and answerRaajTech
 

Similar to Postgresql İndex on Expression (20)

Dive into EXPLAIN - PostgreSql
Dive into EXPLAIN  - PostgreSqlDive into EXPLAIN  - PostgreSql
Dive into EXPLAIN - PostgreSql
 
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
 
PyCon SG x Jublia - Building a simple-to-use Database Management tool
PyCon SG x Jublia - Building a simple-to-use Database Management toolPyCon SG x Jublia - Building a simple-to-use Database Management tool
PyCon SG x Jublia - Building a simple-to-use Database Management tool
 
Mignesh Birdi Assignment3.pdf
Mignesh Birdi Assignment3.pdfMignesh Birdi Assignment3.pdf
Mignesh Birdi Assignment3.pdf
 
Leip103
Leip103Leip103
Leip103
 
Interactive financial analytics with vix(cboe)
Interactive financial analytics with vix(cboe)Interactive financial analytics with vix(cboe)
Interactive financial analytics with vix(cboe)
 
27. mathematical, date and time functions in VB Script
27. mathematical, date and time  functions in VB Script27. mathematical, date and time  functions in VB Script
27. mathematical, date and time functions in VB Script
 
R Programming Intro
R Programming IntroR Programming Intro
R Programming Intro
 
Time Series Analysis:Basic Stochastic Signal Recovery
Time Series Analysis:Basic Stochastic Signal RecoveryTime Series Analysis:Basic Stochastic Signal Recovery
Time Series Analysis:Basic Stochastic Signal Recovery
 
SQL: Query optimization in practice
SQL: Query optimization in practiceSQL: Query optimization in practice
SQL: Query optimization in practice
 
Introduction to python programming 1
Introduction to python programming   1Introduction to python programming   1
Introduction to python programming 1
 
Effective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyEffective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPy
 
Using PostgreSQL statistics to optimize performance
Using PostgreSQL statistics to optimize performance Using PostgreSQL statistics to optimize performance
Using PostgreSQL statistics to optimize performance
 
Chapter 1 Basic Concepts
Chapter 1 Basic ConceptsChapter 1 Basic Concepts
Chapter 1 Basic Concepts
 
On Beyond (PostgreSQL) Data Types
On Beyond (PostgreSQL) Data TypesOn Beyond (PostgreSQL) Data Types
On Beyond (PostgreSQL) Data Types
 
Databases with SQLite3.pdf
Databases with SQLite3.pdfDatabases with SQLite3.pdf
Databases with SQLite3.pdf
 
OOP program questions with answers
OOP program questions with answersOOP program questions with answers
OOP program questions with answers
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
2D array
2D array2D array
2D array
 
Chapter 16-spreadsheet1 questions and answer
Chapter 16-spreadsheet1  questions and answerChapter 16-spreadsheet1  questions and answer
Chapter 16-spreadsheet1 questions and answer
 

Recently uploaded

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 

Recently uploaded (20)

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 

Postgresql İndex on Expression

  • 1. Mesut ÖNCEL Software Developer mstoncel @mesutoncel mesutoncel mesutoncel91@gmail.com @mesutoncel
  • 4. Expression Index CREATE INDEX idx_name on table_name (expression)
  • 5. CREATE TABLE user_data AS SELECT d as date, 'user_' || replace((d :: date) :: text, '-', '') ||'@gmail.com' email, md5(d :: text) as padding, replace((d :: date) :: text, '-', '') :: int as number FROM generate_series(timestamp '1800-01-01', timestamp '2200-01-01', interval '1 day') s (d); date email pading number 1800-01-01 00:00:00.000000 user_18000101@gmail.com 18077793d1afc397046f46adfaeab411 18000101 1800-01-02 00:00:00.000000 user_18000102@gmail.com 14368f03d495281d3a3d33e02c1bf80c 18000102 1800-01-03 00:00:00.000000 user_18000103@gmail.com dcc6b0efdbacbb4540671a50a33744d2 18000103 1800-01-04 00:00:00.000000 user_18000104@gmail.com 6999a16531d1e33f244cc894613a0c8d 18000104 1800-01-05 00:00:00.000000 user_18000105@gmail.com 8685fb2051aa8cd05db09a150fb74095 18000105 1800-01-06 00:00:00.000000 user_18000106@gmail.com 02b1e445353227c019aa6ef8c027c744 18000106 1800-01-07 00:00:00.000000 user_18000107@gmail.com 3c898ba68d6249a819257fa0799b0bac 18000107 1800-01-08 00:00:00.000000 user_18000108@gmail.com 9322fd00eadfff94e90f582dd0ed12d8 18000108 1800-01-09 00:00:00.000000 user_18000109@gmail.com 830787e882375bdb0a5b526b90d998fa 18000109
  • 6. EXPLAIN ANALYZE select * from user_data where upper(email) ='USER' Seq Scan on user_data (cost=0.00..3995.47 rows=730 width=65) (actual time=96.941..96.941 rows=0 loops=1) Filter: (upper(email) = 'USER'::text) Rows Removed by Filter: 146098 Planning time: 0.248 ms Execution time: 96.970 ms
  • 7. CREATE INDEX idx_user_email on user_data (upper(email)) Bitmap Heap Scan on user_data (cost=22.08..1406.12 rows=730 width=65) (actual time=0.062..0.062 rows=0 loops=1) Recheck Cond: (upper(email) = 'USER'::text) -> Bitmap Index Scan on idx_user_email (actual time=0.059..0.059 rows=0 loops=1) Index Cond: (upper(email) = ‘USER'::text) Planning time: 1.172 ms Execution time: 0.097 ms EXPLAIN ANALYZE select * from user_data where upper(email) ='USER'
  • 8. EXPLAIN ANALYZE select * from user_data order by (email || padding) desc Sort (cost=24153.43..24518.68 rows=146098 width=101) (actual time=3486.804..4286.983 rows=146098 loops=1) Sort Key: ((email || padding)) DESC Sort Method: external merge Disk: 19616kB -> Seq Scan on user_data (cost=0.00..3630.22 rows=146098 width=101) (actual time=0.018..66.519 rows=146098 loops=1) Planning time: 0.160 ms Execution time: 4313.415 ms
  • 9. create index idx_col_text on user_data ((email || padding)) EXPLAIN ANALYZE select * from user_data order by (email || padding) desc Index Scan Backward using idx_col_text on user_data (cost=0.42..15313.14 rows=146098 width=101) (a Planning time: 0.218 ms Execution time: 90.887 ms
  • 10. explain analyze select * from user_data where number::text like '18000%' Seq Scan on user_data (cost=0.00..4360.72 rows=730 width=69) (actual time=0.015..69.488 rows=273 loops=1) Filter: ((number)::text ~~ '18000%'::text) Rows Removed by Filter: 145825 Planning time: 0.144 ms Execution time: 69.519 ms explain analyze select * from user_data where number::text like '18000%' create index idx_user_data_b_tree on user_data ((number::text)) Seq Scan on user_data (cost=0.00..4360.72 rows=730 width=69) (actual time=0.135..65.767 rows=273 loops=1) Filter: ((number)::text ~~ '18000%'::text) Rows Removed by Filter: 145825 Planning time: 0.704 ms Execution time: 65.930 ms
  • 11. CREATE INDEX idx_faults_uname_varchar_pattern ON user_data ((number::text) varchar_pattern_ops) EXPLAIN ANALYZE select * from user_data where number::text like '18000%' Bitmap Heap Scan on user_data (cost=19.90..1405.77 rows=730 width=69) (actual time=0.151..0.351 rows=273 loops=1) Filter: ((number)::text ~~ '18000%'::text) Heap Blocks: exact=4 -> Bitmap Index Scan on idx_faults_uname_varchar_pattern (cost=0.00..19.72 rows=730 width=0) (actual time=0.120..0.120 rows=273 lo Index Cond: (((number)::text ~>=~ '18000'::text) AND ((number)::text ~<~ '18001'::text)) Planning time: 0.910 ms Execution time: 0.414 ms
  • 12. Kişisel fonksiyonlarınızı da kullanabilirsiniz. Tek şartımız var :) Oluşturduğunuz fonksiyon Immutable olmalı! CREATE FUNCTION special_function(num int) RETURNS text AS $$ BEGIN RETURN upper(num::text) ; END $$ LANGUAGE 'plpgsql' IMMUTABLE;
  • 13. CREATE INDEX idx_special_func on user_data (special_function(number)) EXPLAIN ANALYZE select * from user_data where special_function(number) = '18000' Bitmap Heap Scan on user_data (cost=18.08..1582.79 rows=730 width=69) (actual time=0.097..0.097 rows=0 loops=1) Recheck Cond: (special_function(number) = '18000'::text) -> Bitmap Index Scan on idx_special_func (cost=0.00..17.90 rows=730 width=0) (actual time=0.090..0.090 rows=0 loops=1) Index Cond: (special_function(number) = '18000'::text) Planning time: 0.197 ms Execution time: 0.256 ms
  • 14. EXPLAIN ANALYZE select email from user_data where lower(email) > 'user18' and date_part('month'::text, date) = '1' and number=2 Seq Scan on user_data (cost=0.00..5091.20 rows=1 width=24) (actual time=34.569..34.569 rows=0 loops=1) Filter: ((number = 2) AND (lower(email) > 'user18'::text) AND (date_part('month'::text, date) = '1'::double precision)) Rows Removed by Filter: 146098 Planning time: 0.189 ms Execution time: 34.608 ms
  • 15. CREATE INDEX idx_multiple_column on user_data (lower(email), extract(month from date), number) Index Scan using idx_multiple_column on user_data (cost=0.42..2021.17 rows=1 width=24) (actual time=149.422..149.422 rows=0 loops Index Cond: ((lower(email) > 'user18'::text) AND (date_part('month'::text, date) = '1'::double precision) AND (number = 2)) Planning time: 0.522 ms Execution time: 149.454 ms
  • 16. EXPLAIN ANALYZE select email from user_data where lower(email) > 'user18' and extract(month from date) = 1 Seq Scan on user_data (cost=0.00..4725.96 rows=243 width=24) (actual time=0.028..259.265 rows=12401 loops=1) Filter: ((lower(email) > 'user18'::text) AND (date_part('month'::text, date) = '1'::double precision)) Rows Removed by Filter: 133697 Planning time: 0.063 ms Execution time: 259.923 ms
  • 17. CREATE INDEX idx_multiple_column on user_data (lower(email), extract(month from date)) EXPLAIN ANALYZE select email from user_data where lower(email) > 'user18' and extract(month from date) = 1 Bitmap Heap Scan on user_data (cost=1675.47..2349.16 rows=243 width=24) (actual time=127.902..130.773 rows=12401 loops=1) Recheck Cond: ((lower(email) > 'user18'::text) AND (date_part('month'::text, date) = '1'::double precision)) Heap Blocks: exact=544 -> Bitmap Index Scan on idx_multiple_column (cost=0.00..1675.41 rows=243 width=0) (actual time=127.811..127.811 rows=12401 loops=1) Index Cond: ((lower(email) > 'user18'::text) AND (date_part('month'::text, date) = '1'::double precision)) Planning time: 0.395 ms Execution time: 131.268 ms
  • 18. create index idx_float_price on booking ((data ->> ‘amount')) select created_at from booking where data ->> 'amount'> '100';