偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

帶你讀 MySQL 源碼:Select *

數(shù)據(jù)庫 MySQL
本篇我們來聊聊 Select * 中的星號(hào)是怎么展開為表中所有字段的。

1、整體介紹

對于 select * from table 中的星號(hào),我們再熟悉不過了:它告訴 MySQL 返回表所有字段的內(nèi)容。

MySQL 服務(wù)端收到 select 語句之后,會(huì)在 server 層把星號(hào)展開為表中的所有字段,然后告訴存儲(chǔ)引擎返回這些字段的內(nèi)容。

對于存儲(chǔ)引擎來說,它只需要按照 server 層的要求返回指定字段的內(nèi)容即可,它不知道(也不需要知道)客戶端是要求返回表中所有字段,還是部分字段的內(nèi)容。

select * 中的星號(hào)展開為表中所有字段涉及 2 個(gè)階段:

  • 詞法 & 語法分析階段:標(biāo)記 select 字段列表中包含幾個(gè)星號(hào)。
  • 查詢準(zhǔn)備階段:把星號(hào)展開為表中所有字段。

2、源碼分析

(1)Item_asterisk::itemize()

// sql/item.cc
bool Item_asterisk::itemize(Parse_context *pc, Item **res) {
...
pc->select->with_wild++;
return false;
}

多表連接時(shí),select 字段列表中可能會(huì)包含多個(gè)星號(hào),詞法 & 語法分析階段,每碰到 select 字段列表中的一個(gè)星號(hào),Item_asterisk::itemize() 就會(huì)給 pc->select->with_wild 屬性加 1。

pc->select 是 Query_block 對象的指針,定義如下:

// sql/parse_tree_node_base.h
struct Parse_context {
...
Query_block *select; ///< Current Query_block object
...
};

后面 Query_block::prepare() 訪問的 with_wild 屬性就是這里的 pc->select->with_wild。

(2)Query_block::prepare()

// sql/sql_resolver.cc
bool Query_block::prepare(THD *thd, mem_root_deque<Item *> *insert_field_list) {
...
if (with_wild && setup_wild(thd)) return true;
...
}

prepare() 方法中,關(guān)于 select * 的邏輯比較簡單,就這一行。

如果 with_wild 大于 0,則調(diào)用 setup_wild(thd),處理 select 字段列表中星號(hào)展開為表中所有字段的邏輯。

(3)Query_block::setup_wild()

// sql/sql_resolver.cc
bool Query_block::setup_wild(THD *thd) {
...
// 從 select 字段列表中的第 1 個(gè)字段開始處理
// 滿足 2 個(gè)條件中的任意一個(gè)就結(jié)束循環(huán):
// 1. with_wild > 0 為 false,
// 說明已處理完所有星號(hào),結(jié)束循環(huán)
// 2. it != fields.end() 為 false,
// 說明已經(jīng)處理了所有字段,結(jié)束循環(huán)
for (auto it = fields.begin(); with_wild > 0 && it != fields.end(); ++it) {
Item *item = *it;
// item->hidden = true
// 表示 select 字段列表中的這個(gè)字段
// 是查詢優(yōu)化器給【偷偷】加上的
// 肯定不會(huì)是星號(hào),直接跳過
if (item->hidden) continue;
Item_field *item_field;
// Item::FIELD_ITEM 說明當(dāng)前循環(huán)的字段
// 是個(gè)普通字段,不是函數(shù)、子查詢等
// 那它就有可能是星號(hào),需要通過 item_field->is_asterisk()
// 進(jìn)一步判斷是否是星號(hào)
if (item->type() == Item::FIELD_ITEM &&
(item_field = down_cast<Item_field *>(item)) &&
// 如果 item_field 對應(yīng)的字段是星號(hào)
// item_field->is_asterisk() 會(huì)返回 true
item_field->is_asterisk()) {
assert(item_field->field == nullptr);
// 只有 create view as ... 中的 select 語句
// any_privileges 為 true
// 其它情況下,它的值為 false
// insert_fields() 方法中會(huì)用到
const bool any_privileges = item_field->any_privileges;
// 如果當(dāng)前 Query_block 對應(yīng)的是子查詢
// master_query_expression()->item
// 指向主查詢中該子查詢所屬的 where 條件
Item_subselect *subsel = master_query_expression()->item;
...
// 當(dāng)前 Query_block 是 exists 子查詢
// 并且子查詢中不包含 having 子句
// 則可以把子查詢中的星號(hào)替換為常量
if (subsel && subsel->substype() == Item_subselect::EXISTS_SUBS &&
!having_cond()) {
...
*it = new Item_int(NAME_STRING("Not_used"), 1,
MY_INT64_NUM_DECIMAL_DIGITS);
} else {
// 不滿足 if 中的條件
// 則需要調(diào)用 insert_fields()
// 把星號(hào)展開為表中所有字段
assert(item_field->context == &this->context);
if (insert_fields(thd, this, item_field->db_name,
item_field->table_name, &fields, &it, any_privileges))
return true;
}

// 每處理完 select 字段列表中的一個(gè)星號(hào)
// with_wild 就減 1
// 減到 0 之后,就說明所有星號(hào)都已經(jīng)處理過了
with_wild--;
}
}

return false;
}

Query_block::setup_wild() 的主體邏輯是迭代 select 字段列表中的每個(gè)字段,遇到星號(hào)就處理,不是星號(hào)就忽略,星號(hào)的處理邏輯有 2 種:

第 1 種:滿足 if (subsel && ...) 條件,說明 select 語句是 where 條件中的 exists 子查詢,并且子查詢中不包含 having 子句。這種場景下,select 字段列表中的星號(hào)可以被替換為常量,而不需要展開為表的所有字段。

*it = new Item_int(...)? 創(chuàng)建了一個(gè)代表常量的字段對象,字段名為 Not_used?,字段值為 1,用于替換 select 字段列表中的星號(hào)。

這種場景的示例 SQL 如下:

select st1, i1 from t1 where exists(
select * from t2 where t1.i1 = t2.i1
)

子查詢只需要判斷 t2 表中是否存在滿足 t1.i1 = t2.i1 的記錄,而不需要讀取 t2 表的所有字段,因?yàn)樽x取了所有字段,也用不上,純屬浪費(fèi),所以,星號(hào)也就可以被替換成常量了。替換之后的 SQL 相當(dāng)于這樣:

select st1, i1 from t1 where exists(
select 1 from t2 where t1.i1 = t2.i1
)

實(shí)際上,子查詢執(zhí)行過程中,server 層會(huì)要求存儲(chǔ)引擎返回 t2 表的 i1 字段內(nèi)容,用于判斷 t2 表中是否存在滿足 t1.i1 = t2.i1? 的記錄。這個(gè)邏輯是 server 層自主實(shí)現(xiàn)的,和 ?select * 中的星號(hào)展開為表中所有字段的邏輯不相關(guān),我們知道有這個(gè)邏輯就可以,不展開介紹了。

第 2 種:不滿足 if (subsel && ...)? 條件,就需要調(diào)用 insert_fields(),把 select 字段列表中的星號(hào)展開為表的所有字段。

(4)insert_fields()

// sql/sql_base.cc
bool insert_fields(THD *thd, Query_block *query_block, const char *db_name,
const char *table_name, mem_root_deque<Item *> *fields,
mem_root_deque<Item *>::iterator *it, bool any_privileges) {
...
bool found = false;

Table_ref *tables;
// 按照 select 語句中表的出現(xiàn)順序
// 初始化表的迭代器
Tables_in_user_order_iterator user_it;
user_it.init(query_block, table_name != nullptr);

while (true) {
// 從迭代器中獲取下一個(gè)需要處理的表
tables = user_it.get_next();
// tables == nullptr 說明迭代結(jié)束,結(jié)束循環(huán)
if (tables == nullptr) break;
// 表中的字段迭代器
Field_iterator_table_ref field_iterator;
TABLE *const table = tables->table;

assert(tables->is_leaf_for_name_resolution());

// if 進(jìn)行 2 個(gè)條件判斷,任何一個(gè)不滿足則跳過當(dāng)前表:
// 1. table_name 不為 NULL 說明星號(hào)前面指定了表名
// 比較星號(hào)前面的表名和當(dāng)前迭代的表名是否相同
// 2. db_name 不為 NULL 說明星號(hào)前面指定了數(shù)據(jù)庫名
// 比較星號(hào)前面的數(shù)據(jù)庫名和當(dāng)前迭代的表所屬的數(shù)據(jù)庫名是否相同
if ((table_name &&
my_strcasecmp(table_alias_charset, table_name, tables->alias)) ||
(db_name && strcmp(tables->db, db_name)))
continue;

// 以下 2 種情況都滿足,需要檢查
// 當(dāng)前連接用戶是否有表中所有字段的 select 權(quán)限:
// 1. !any_privileges 為 true
// 說明當(dāng)前 select 語句
// 不是 create view as ... 中的 select 語句
// 2. 當(dāng)前連接用戶沒有表的 select 權(quán)限,
if (!any_privileges && !(tables->grant.privilege & SELECT_ACL)) {
field_iterator.set(tables);
if (check_grant_all_columns(thd, SELECT_ACL, &field_iterator))
return true;
}
...
// 初始化字段迭代器
field_iterator.set(tables);
// 迭代當(dāng)前表的每一個(gè)字段
for (; !field_iterator.end_of_fields(); field_iterator.next()) {
// 根據(jù)字段對象創(chuàng)建 Item
Item *const item = field_iterator.create_item(thd);
if (!item) return true; /* purecov: inspected */
assert(item->fixed);
...
// found 的初始值為 false,
// 表示這是表中第 1 個(gè)字段
// 用該字段的 Item 對象替換星號(hào)的 Item 對象
if (!found) {
found = true;
**it = item; /* Replace '*' with the first found item. */
} else {
// 表中第 2 個(gè)及以后的字段時(shí),
// Item 對象賦值給 it + 1 指向的位置
// 也就是加入了 select 字段列表
/* Add 'item' to the SELECT list, after the current one. */
*it = fields->insert(*it + 1, item);
}
...
}
}
...

insert_fields() 的主要邏輯如下:

按照 select 語句中表的出現(xiàn)順序迭代每個(gè)表,每迭代一個(gè)表,都會(huì)判斷該表名和星號(hào)前面的表名(如果有)是否相同,以及該表所屬的數(shù)據(jù)庫名和星號(hào)前面的數(shù)據(jù)庫名是否相同(如果有)。

如果當(dāng)前迭代的表名、表所屬的數(shù)據(jù)庫名和星號(hào)前面的表名、數(shù)據(jù)庫名都相同,接下來會(huì)進(jìn)行訪問權(quán)限檢查。

如果當(dāng)前連接用戶有表的 select 權(quán)限,說明它對表中的所有列都有查詢權(quán)限,否則,需要調(diào)用 check_grant_all_columns(...),檢查它對表中每一個(gè)字段是否有 select 權(quán)限。

通過權(quán)限檢查之后,就開始迭代表中的每個(gè)字段,每迭代一個(gè)字段,都根據(jù)該字段構(gòu)造一個(gè) Item 對象,并把 Item 對象加入 select 字段列表。

3、總結(jié)

select * 中的星號(hào)展開為表中所有字段涉及詞法 & 語法分析階段、查詢準(zhǔn)備階段,總結(jié)如下:

  • 迭代 select 字段列表中的每個(gè)字段。
  • 碰到星號(hào)會(huì)判斷是否需要展開為表的所有字段。
  • 如果需要展開,則按照 select 語句中表的出現(xiàn)順序迭代每個(gè)表。
  • 迭代每個(gè)表時(shí),檢查當(dāng)前連接用戶是否有該表或表中所有字段的 select 權(quán)限。
  • 通過權(quán)限檢查之后,把當(dāng)前迭代的表的字段逐個(gè)加入 select 字段列表。

本文轉(zhuǎn)載自微信公眾號(hào)「一樹一溪」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系一樹一溪公眾號(hào)。

責(zé)任編輯:姜華 來源: 一樹一溪
相關(guān)推薦

2023-04-10 08:07:48

MySQLlimitoffset

2023-05-26 14:08:00

Where 條件MySQL

2010-05-18 13:45:08

MySQL selec

2022-09-09 19:01:02

接口Reader?Spark

2021-02-11 13:30:56

Nodejs源碼c++

2021-01-04 05:53:35

MyBatis底層Java

2012-09-06 10:07:26

jQuery

2022-01-26 07:18:57

工具GoGo 項(xiàng)目

2022-02-09 07:44:30

Go源碼工具

2024-12-05 09:45:25

Reactdiff 算法前端開發(fā)

2016-04-25 10:47:49

源碼閱讀學(xué)習(xí)

2010-05-13 10:57:51

MySQL SELEC

2010-05-18 13:52:49

MySQL selec

2010-05-18 18:51:02

MySQL SELEC

2021-09-16 10:36:34

開源技術(shù) 項(xiàng)目

2020-07-29 17:35:08

Promise源碼前端

2018-07-31 14:49:45

編程語言Java源碼

2021-03-13 11:23:51

源碼邏輯框架

2023-08-18 14:39:02

2021-05-28 10:46:36

MySQL執(zhí)行計(jì)劃
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)