forked from barryclark/jekyll-now
-
Notifications
You must be signed in to change notification settings - Fork 0
Oslo mysql indexing #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
oslo0322
wants to merge
2
commits into
master
Choose a base branch
from
oslo_mysql_indexing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # Story | ||
|
|
||
| 我們都知道 MySQL 的 index 很重要,會影響 query 的速度,設定 index 的時候,通常直覺會直接對 columns(fields) 直接下index。 | ||
| 這樣的做法在一般情形下當然是ok,速度也都還可以接受,但是當資料成長到千萬筆以上的時候,就開始出現問題了,MySQL 在index 的選擇並不會很聰明地將 index 合併在一起。 | ||
|
|
||
| ### 舉個例子 | ||
| > 我有username 和 timestamp 的index,當 sql 語法下 where 的時候,MySQL 會根據你的資料量來選擇你適合的 index | ||
|
|
||
| > 假設以 username 撈出來會有200萬筆,timestamp 撈出來會有500萬筆,這時候 MySQL 會選擇使用 username 來做 index 的 key | ||
|
|
||
| > 問題來了,若符合 username 以及 timestamp 的資料可能才3萬筆,每次都要先撈200萬筆以後再去篩選嗎?這樣很慢吧! | ||
|
|
||
| > _*所以 MySQL 又有一種 indexing 的方法是可以合併多個 columns 當 key 的,可以把 username 以及 timestamp 當做 key,這樣 MySQL 排序篩選的時候,會加快許多*_ | ||
|
|
||
| 每個 framework 的設定方法也不一樣,這裡用 Django 當作例子 | ||
|
|
||
| [Django Reference](https://docs.djangoproject.com/en/1.9/ref/models/options/#index-together) | ||
| ``` | ||
| class Meta: | ||
| index_together = [ | ||
| ["username", "timestamp"], | ||
| ] | ||
| ``` | ||
| 這樣即可設定完成 | ||
|
|
||
| # Reference | ||
| indexing 的方法有兩種,也有些差異,可以參考 | ||
| [這裡](https://www.percona.com/blog/2014/01/03/multiple-column-index-vs-multiple-indexes-with-mysql-56/) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以在 backtick (`) 後 加上語言 就可以觸發語法變色