Giter VIP home page Giter VIP logo

all-about-database's Introduction

All About Database

TO READ

Difficulty of DB Performance

一致性難題

  • 銀行轉賬問題:
  • 投票問題:

並行所產生的不一致性難題

flowchart TD
  idPb("By concurrency")
  id1("Inconsist update")
  id2("Dirty read")
  id3("Non-repeatable Read")
  id4("Phantom Read")
  idPb --> id1
  idPb --> id2
  idPb --> id3
  idPb --> id4
  id1 -->|solution| lock
  id2 -->|solution| MVCC 
Loading
  • Dirty Read: A transaction reads data written by other concurrent uncommitted transation
  • Non-repeatable Read: A transaction reads the same row twice and sees different value becuase it has been modified by other committed transaction
  • Phantom Read: A transaction re-executes a query to find rows that satisfy a condition and sees a different set of rows, due to changes by other commited transaction
  • Serialization Anomaly: The result of group of concurrent commited transactions is impossible to achieve if we try to run them sequentially in any order without overlapping

    例如,在個別兩個 transaction 加總會看到重複的加總而非疊加
    insert, sum() related

JOINS

  • Target: combine rows from two or more tables (based on a related column between them)

INNER JOIN

  • 交集
-- eg mysql
SELECT * FROM <table_name> INNER JOIN <table_name> ON <col> = <col>

LEFT JOIN

  • See all left table but only see matching right table

The LEFT JOIN keyword returns all records from the left table, even if there are no matches in the right table.

-- eg mysql
SELECT * FROM <table_name> LEFT JOIN <table_name> ON <col> = <col>

FULL OUTER JOIN

Show all matching records

Index

  • Every index has its own B+ tree

    See B+ tree

  • 每次新增、更新資料時,都會異動到所使用的 b+ tree => 當使用的 index 越多,需要維護的 index 也越多 => 若建立太多 index ,可能會降低新增或者更新的效率
  • Use EXPLAIN => Eg. EXPLAIN SELECT * FROM user_no WHERE name = 'mark';
  • If values in a field are too small (eg only 0 and 1), not good to use index

Union

  • Columns to take from queries should be the same
  • MySQL automtically remove duplicate entries => so use UNION ALL if you need duplicates
-- eg mysql
SELECT name, cost, bids FROM items WHERE bids > 190;
UNION
SELECT name, cost, bids FROM items WHERE bids > 190;

When to Use?

  • Take same cols from two tables with one single query

Cheat Sheet

DB Design Tool

References

all-about-database's People

Contributors

chengr4 avatar

Stargazers

Roman avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.