lvhkhanh / SQL

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SQL

Certificates

Learn SQL Course

Best Practices

  • LIKE ANY(ARRAY('%a', '%b'))
  • OR -> IN (1,3), number better than text
  • WITH clause for creating function, temp table (common table expression)
  • Partition column in WHERE
  • SELECT * FROM pg_indexes
  • CREATE INDEX CONCURRENTLY index_a ON tbl_b(col_c); create index for cols in WHERE
  • https://www.sqlstyle.guide/
  • q'[Content single quote ' do not need to double.]'
  • LEFT JOIN (1:1 vs 1:n)
  • MERGE INTO vs INSERT/UPDATE (condition duplicate table join)
  • check index usaged, check no_result_cache, opt adaptive plans when use sql explain
  • Based on statistics, Use index (1%) vs read whole table (25%)
EXEC dbms_stats.gather_system_stats('Start');
SELECT * FROM sys.aux_stats$;
ANALYZE TABLE <tbl> COMPUTE STATISTICS; 
EXEC dbms_stats.gather_database_stats;
EXEC dbms_stats.gather_dictionary_stats;
EXEC dbms_stats.gather_schema_stats(ownname=>'SH');
EXEC dbms_stats.gather_table_stats(ownname=>'SH', tabname=>'tbl', cascade=>true);
SELECT * FROM dba_table_statistics WHERE table_name ='tbl';
  • Explain plan
EXPLAIN PLAN FOR <sql>;
SELECT * FROM plan_table;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY());
EXPLAIN PLAN SET statement_id='myID' INTO my_tbl FOR <sql>;
  • Cost (%CPU)
  • IN (1,2,3,4) => BETWEEN 1 AND 4
  • reused TYPE -> declare var1 table2.column3%TYPE;
  • Convert OR(prevent index usages) to UNION ALL + AND for using INDEX
  • nested sub query to join query
  • SELECT mum_distinct FROM dba_tab_columns WHERE table_name='tbl_name'
  • less record table first, then more record table later
  • CREATE INDEX for GROUP BY columns
  • USE Minimum call to SYSDATE, NOT CURRENT_DATE
  • condition JOIN, NOT IN, NOT EXISTS
  • check tables with same alias
  • inner join then left join
  • where move to first inner join
  • where in
  • ROWNUM starts from 1; put it in the most outer clause
  • materialzed view, auto refresh on commit; create index on view
  • table partitioning

Paths

https://www.codecademy.com/paths/analyze-data-with-sql/tracks/analyze-data-sql-get-started-with-sql/modules/what-is-sql/lessons/why-sql-ii/exercises/sql-queries

Courses

https://www.codecademy.com/learn/learn-sql

https://learn.datacamp.com/career-tracks/data-analyst-with-sql-server?version=1

https://www.codecademy.com/learn/sql-table-transformation

https://www.codecademy.com/learn/sql-analyzing-business-metrics

SQL Server Metadata Succinctly

Ebooks

https://www.syncfusion.com/ebooks/sql-server-metadata-succinctly

https://www.syncfusion.com/ebooks/sql_server_for_c_sharp_developers_succinctly

https://www.syncfusion.com/ebooks/sqlserver

Performance

About