diff --git a/Complete SQL & Databases - ZTM/06_Advanced_SQL.sql b/Complete SQL & Databases - ZTM/06_Advanced_SQL.sql index fd7ef81..63549f4 100644 --- a/Complete SQL & Databases - ZTM/06_Advanced_SQL.sql +++ b/Complete SQL & Databases - ZTM/06_Advanced_SQL.sql @@ -569,3 +569,144 @@ JOIN departments d USING(dept_no) WHERE s.from_date = lsc.max ORDER BY s.emp_no; +/*--------------------------------------------------------------------------------------------------------------*/ + +/**************** 24) Indexes ****************/ +/* + Index is the construct to improve Querying Performance. + + Think of it like a table of contents, it helps you find where a piece of data is. + + Pros: Speed up querying + Cons: Slows down data Insertion and Updates + + ***** Types of Indexes ***** + - Single Column + - Multi Column + - Unique + - Partial + - Implicit Indexes (done by default) +*/ + +-- Create an index +CREATE UNIQUE INDEX idx_name +ON table_name(column1, column2, ...); + +-- Delete an index +DELETE INDEX idx_name; + +/* + **** When to Use Indexes ***** + - Index Foreign Keys + - Index Primary Keys and Unique Columns + - Index on Columns that end up in the ORDER BY/WHERE clause VERY OFTEN. + + ***** When NOT to use Indexes ****** + - Don't add Index just to add Index + - Don't use Index on Small Table + - Don't use on Tables that are UPDATED FREQUENTLY. + - Don't use on Columns that can contain NULL values + - Don't use on Columns that have Large Values. +*/ + + +/***************** 25) Indexes Types ******************/ + +/* + + Single Column Index : retrieving data that satisfies ONE condition. + Multi Column Index : retrieving data that satisfies MULIPLE Conditions. + UNIQUE : For Speed and Integrity + PARTIAL : Index Over a SUBSET of a Table (CREATE INDEX name ON table ( (column1, column2, ...) + + +-- by default, it is created using B-TREE +CREATE INDEX idx_countrycode +ON city(countrycode); + +-- but we can specify which algorithm to use (example: HASH) +CREATE INDEX idx_countrycode +ON city USING HASH (countrycode); + +/*************************** When to use which Algorithms? *************************/ +/* + ********* B-TREE *********** + Default Algorithm + Best Used for COMPARISONS with + <, > + <=, >= + = + BETWEEN + IN + IS NULL + IS NOT NULL + + + ********** HASH ********** + Can only handle Equality = Operations. + + + *********** GIN (Generalized Inverted Index) ************ + Best used when Multiple Values are stored in a Single Field. + + + *********** GIST (Generalized Search Tree) *********** + Useful in Indexing Geometric Data and Full-Text Search. +*/ + +-- testing for HASH +EXPLAIN ANALYZE +SELECT "name", district, countrycode +FROM city +WHERE countrycode='BEL' OR countrycode='TUN' OR countrycode='NL'; + + + + + + + + + + + + + + diff --git a/Complete SQL & Databases - ZTM/index_algorithm_Btree.png b/Complete SQL & Databases - ZTM/index_algorithm_Btree.png new file mode 100644 index 0000000..d64fdac Binary files /dev/null and b/Complete SQL & Databases - ZTM/index_algorithm_Btree.png differ diff --git a/Complete SQL & Databases - ZTM/index_algorithm_Gin.png b/Complete SQL & Databases - ZTM/index_algorithm_Gin.png new file mode 100644 index 0000000..876e044 Binary files /dev/null and b/Complete SQL & Databases - ZTM/index_algorithm_Gin.png differ diff --git a/Complete SQL & Databases - ZTM/index_algorithm_Gist.png b/Complete SQL & Databases - ZTM/index_algorithm_Gist.png new file mode 100644 index 0000000..ce222e7 Binary files /dev/null and b/Complete SQL & Databases - ZTM/index_algorithm_Gist.png differ diff --git a/Complete SQL & Databases - ZTM/index_algorithm_Hash.png b/Complete SQL & Databases - ZTM/index_algorithm_Hash.png new file mode 100644 index 0000000..b574064 Binary files /dev/null and b/Complete SQL & Databases - ZTM/index_algorithm_Hash.png differ diff --git a/Complete SQL & Databases - ZTM/index_algorithms.png b/Complete SQL & Databases - ZTM/index_algorithms.png new file mode 100644 index 0000000..5ddba94 Binary files /dev/null and b/Complete SQL & Databases - ZTM/index_algorithms.png differ