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';