mysql sql提示 MySQL 使用SQL提示

2021-10-18 12:36:44 字數 4603 閱讀 6722

sql提示(sql hint)是優化資料庫的一種重要手段,簡單來說就是在sql語句中加入一些人為的提示來達到優化操作的目的。

下面是乙個使用sql提示的例子:

select   sql_buffer_results * from ……

這個語句將強制mysql 生成乙個臨時結果集。只要臨時結果集生成後,所有表上的鎖定均被釋放。這能在遇到鎖問題時或者更長時間將結果傳給客戶端時有所幫助,因為可以盡快釋放鎖資源。

下面是一些在mysql中常用的sql提示。

use index

在查詢語句中表名的後面,新增use index 來提供希望mysql 去參考的索引列表,就可以讓mysql 不在考慮其他可用的索引。

mysql> show index from rental;

| table  | non_unique | key_name            | seq_in_index | column_name  | collation | cardinality | sub_part | packed | null | index_type | comment | index_comment |

| rental |          0 | primary             |            1 | rental_id    | a         |       16008 |     null | null   |      | btree      |         |               |

| rental |          0 | rental_date         |            1 | rental_date  | a         |       16008 |     null | null   |      | btree      |         |               |

| rental |          0 | rental_date         |            2 | inventory_id | a         |       16008 |     null | null   |      | btree      |         |               |

| rental |          0 | rental_date         |            3 | customer_id  | a         |       16008 |     null | null   |      | btree      |         |               |

| rental |          1 | idx_fk_inventory_id |            1 | inventory_id | a         |       16008 |     null | null   |      | btree      |         |               |

| rental |          1 | idx_fk_customer_id  |            1 | customer_id  | a         |        1231 |     null | null   |      | btree      |         |               |

| rental |          1 | idx_fk_staff_id     |            1 | staff_id     | a         |           4 |     null | null   |      | btree      |         |               |

7 rows in set (0.00 sec)

mysql>

mysql> explain select count(*) from rental use index(rental_date);

| id | select_type | table  | type  | possible_keys | key         | key_len | ref  | rows  | extra       |

|  1 | ******      | rental | index | null          | rental_date | 10      | null | 16008 | using index |

1 row in set (0.00 sec)

2.  ignore index

如果使用者只是單獨地想讓mysql忽略乙個或者多個索引,則可以用ignore index 作為 hint。

mysql> explain select count(*) from rental;

| id | select_type | table  | type  | possible_keys | key             | key_len | ref  | rows  | extra       |

|  1 | ******      | rental | index | null          | idx_fk_staff_id | 1       | null | 16008 | using index |

1 row in set (0.00 sec)

mysql> explain select count(*) from rental ignore index (idx_fk_staff_id);

| id | select_type | table  | type  | possible_keys | key                | key_len | ref  | rows  | extra       |

|  1 | ******      | rental | index | null          | idx_fk_customer_id | 2       | null | 16008 | using index |

1 row in set (0.00 sec)

從執行計畫可以看出,系統忽略了指定的索引idx_fk_staff_id, 使用索引idx_fk_customer_id

3. force index

為強制mysql 使用乙個特定的索引,可在查詢中使用force index 作為hint。

例如,當不強制使用索引的時候,因為大部分庫存inventory_id 的值都是大於1的,因此mysql會預設使用全表掃瞄,而 不 使用索引,如下所示:

mysql> explain select * from rental where inventory_id >1\g;

*************************** 1. row ***************************

id: 1

select_type: ******

table: rental

type: all

possible_keys: idx_fk_inventory_id

key: null

key_len: null

ref: null

rows: 16008

extra: using where

1 row in set (0.00 sec)

嘗試使用use index 的hint 看看:

mysql> explain select * from rental use index(idx_fk_inventory_id) where inventory_id >1\g;

*************************** 1. row ***************************

id: 1

select_type: ******

table: rental

type: all

possible_keys: idx_fk_inventory_id

key: null

key_len: null

ref: null

rows: 16008

extra: using where

1 row in set (0.00 sec)

發現仍然不行,mysql還是選擇走全表掃瞄。但是,當使用force index進行提示時,即使使用索引的效率不是很高,mysql 還是選擇使用索引,這是mysql留給使用者的乙個自行選擇執行計畫的權利。

加入force index 提示後再次執行上面的sql:

mysql> explain select * from rental force index(idx_fk_inventory_id) where inventory_id >1\g;

*************************** 1. row ***************************

id: 1

select_type: ******

table: rental

type: range

possible_keys: idx_fk_inventory_id

key: idx_fk_inventory_id

key_len: 3

ref: null

rows: 8004

extra: using index condition

1 row in set (0.00 sec)

學習自《深入淺出mysql》

mysql sql提示 MySQL 使用SQL提示

sql提示 sql hint 是優化資料庫的一種重要手段,簡單來說就是在sql語句中加入一些人為的提示來達到優化操作的目的。下面是乙個使用sql提示的例子 select sql buffer results from 這個語句將強制mysql 生成乙個臨時結果集。只要臨時結果集生成後,所有表上的鎖定...

mysql sql優化策略 MySql更新優化策略

模擬場景一 給資料庫中的一張表的結構調整,新增幾個字段,後面對之前的資料進行重新整理,重新整理的內容是對其中的乙個已有欄位url進行匹配,然後更新新加的字段type和typeid。後來就寫了個shell指令碼來刷資料,結果執行shell指令碼後我就懵了,怎麼這麼慢 情景再現 create table...

mysql sql組合 詳解mysql 組合查詢

使用union 多數sql查詢都只包含乙個或多個表中返回資料的單條select語句。mysql也允許執行多個查詢 多條select語句 並將結果作為單個查詢結果集返回。這些組合查詢通常稱為並 union 有兩種情況需要使用組合查詢 在單個表查詢中從不同的表返回類似結構的資料 對單個表執行多個查詢,按...