@@ -569,3 +569,144 @@ JOIN departments d USING(dept_no)
569
569
WHERE s .from_date = lsc .max
570
570
ORDER BY s .emp_no ;
571
571
572
+ /* --------------------------------------------------------------------------------------------------------------*/
573
+
574
+ /* *************** 24) Indexes ****************/
575
+ /*
576
+ Index is the construct to improve Querying Performance.
577
+
578
+ Think of it like a table of contents, it helps you find where a piece of data is.
579
+
580
+ Pros: Speed up querying
581
+ Cons: Slows down data Insertion and Updates
582
+
583
+ ***** Types of Indexes *****
584
+ - Single Column
585
+ - Multi Column
586
+ - Unique
587
+ - Partial
588
+ - Implicit Indexes (done by default)
589
+ */
590
+
591
+ -- Create an index
592
+ CREATE UNIQUE INDEX idx_name
593
+ ON table_name(column1, column2, ...);
594
+
595
+ -- Delete an index
596
+ DELETE INDEX idx_name;
597
+
598
+ /*
599
+ **** When to Use Indexes *****
600
+ - Index Foreign Keys
601
+ - Index Primary Keys and Unique Columns
602
+ - Index on Columns that end up in the ORDER BY/WHERE clause VERY OFTEN.
603
+
604
+ ***** When NOT to use Indexes ******
605
+ - Don't add Index just to add Index
606
+ - Don't use Index on Small Table
607
+ - Don't use on Tables that are UPDATED FREQUENTLY.
608
+ - Don't use on Columns that can contain NULL values
609
+ - Don't use on Columns that have Large Values.
610
+ */
611
+
612
+
613
+ /* **************** 25) Indexes Types ******************/
614
+
615
+ /*
616
+
617
+ Single Column Index : retrieving data that satisfies ONE condition.
618
+ Multi Column Index : retrieving data that satisfies MULIPLE Conditions.
619
+ UNIQUE : For Speed and Integrity
620
+ PARTIAL : Index Over a SUBSET of a Table (CREATE INDEX name ON table (<expression);)
621
+ IMPLICIT : Automatically creaed by the database: (Primary Key, Unique Key)
622
+
623
+ */
624
+
625
+ EXPLAIN ANALYZE
626
+ SELECT " name" , district, countrycode
627
+ FROM city
628
+ WHERE countrycode IN (' TUN' , ' BE' , ' NL' );
629
+
630
+ -- Single Index
631
+ CREATE INDEX idx_countrycode
632
+ ON city(countrycode);
633
+
634
+
635
+ -- Partial Index
636
+ CREATE INDEX idx_countrycode
637
+ ON city(countrycode) WHERE countrycode IN (' TUN' , ' BE' , ' NL' );
638
+
639
+ EXPLAIN ANALYZE
640
+ SELECT " name" , district, countrycode
641
+ FROM city
642
+ WHERE countrycode IN (' PSE' , ' ZWE' , ' USA' );
643
+
644
+
645
+ /* ************************* 26) Index Algorithms *********************/
646
+ /*
647
+ POSTGRESQL provides Several types of indexes:
648
+ B-TREE
649
+ HASH
650
+ GIN
651
+ GIST
652
+ Each Index types use different algorithms.
653
+ */
654
+
655
+ -- we can extend which algorithm to use while creating index
656
+ CREATE UNIQUE INDEX idx_name
657
+ ON tbl_name USING < method> (column1, column2, ...)
658
+
659
+
660
+ -- by default, it is created using B-TREE
661
+ CREATE INDEX idx_countrycode
662
+ ON city(countrycode);
663
+
664
+ -- but we can specify which algorithm to use (example: HASH)
665
+ CREATE INDEX idx_countrycode
666
+ ON city USING HASH (countrycode);
667
+
668
+ /* ************************** When to use which Algorithms? *************************/
669
+ /*
670
+ ********* B-TREE ***********
671
+ Default Algorithm
672
+ Best Used for COMPARISONS with
673
+ <, >
674
+ <=, >=
675
+ =
676
+ BETWEEN
677
+ IN
678
+ IS NULL
679
+ IS NOT NULL
680
+
681
+
682
+ ********** HASH **********
683
+ Can only handle Equality = Operations.
684
+
685
+
686
+ *********** GIN (Generalized Inverted Index) ************
687
+ Best used when Multiple Values are stored in a Single Field.
688
+
689
+
690
+ *********** GIST (Generalized Search Tree) ***********
691
+ Useful in Indexing Geometric Data and Full-Text Search.
692
+ */
693
+
694
+ -- testing for HASH
695
+ EXPLAIN ANALYZE
696
+ SELECT " name" , district, countrycode
697
+ FROM city
698
+ WHERE countrycode= ' BEL' OR countrycode= ' TUN' OR countrycode= ' NL' ;
699
+
700
+
701
+
702
+
703
+
704
+
705
+
706
+
707
+
708
+
709
+
710
+
711
+
712
+
0 commit comments