From 6c03e7c0abdc6aae8c747e162cddec28adf29552 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=82=E7=B4=B9=E6=B7=B3?= Date: Mon, 25 Apr 2016 22:37:38 +0800 Subject: [PATCH 1/2] add a new post --- _posts/2016-04-25 MySQL indexing Story.md | 29 +++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 _posts/2016-04-25 MySQL indexing Story.md diff --git a/_posts/2016-04-25 MySQL indexing Story.md b/_posts/2016-04-25 MySQL indexing Story.md new file mode 100644 index 0000000000000..7b9a085c55131 --- /dev/null +++ b/_posts/2016-04-25 MySQL indexing Story.md @@ -0,0 +1,29 @@ +(暫時紀錄一下,還有些 python-mysql 的情形也可以分享) +# 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/) From a8a8cc9f5c3b9a8bde3b30ca962beadf09b8851d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=82=E7=B4=B9=E6=B7=B3?= Date: Sun, 8 May 2016 10:34:51 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=88=AA=E9=99=A4=E6=96=87=E5=AD=97?= =?UTF-8?q?=E6=95=98=E8=BF=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _posts/2016-04-25 MySQL indexing Story.md | 1 - 1 file changed, 1 deletion(-) diff --git a/_posts/2016-04-25 MySQL indexing Story.md b/_posts/2016-04-25 MySQL indexing Story.md index 7b9a085c55131..b75e55285eccb 100644 --- a/_posts/2016-04-25 MySQL indexing Story.md +++ b/_posts/2016-04-25 MySQL indexing Story.md @@ -1,4 +1,3 @@ -(暫時紀錄一下,還有些 python-mysql 的情形也可以分享) # Story 我們都知道 MySQL 的 index 很重要,會影響 query 的速度,設定 index 的時候,通常直覺會直接對 columns(fields) 直接下index。