mysql 中join 與 , 2張表的查詢實際操做

-- ,鏈接2張表的查詢過程,
mysql> select * from modules,objects where modules.module_dec=objects.module_dec;
+------------+------------+---------------+--------+--------+---------------------+---------------------+------------+------------+------------+------------+--------+---------------------+--------+---------------------+---------------------+------------+
| module_dec | module_hex | msg           | name   | status | created_at          | updated_at          | deleted_at | module_dec | object_dec | object_hex | name   | msg                 | status | created_at          | updated_at          | deleted_at |
+------------+------------+---------------+--------+--------+---------------------+---------------------+------------+------------+------------+------------+--------+---------------------+--------+---------------------+---------------------+------------+
|      65536 | 10000      | index1_page   | index  |      0 | 2017-01-26 06:07:53 | 2017-01-26 06:07:53 | NULL       |      65536 |        256 | 100        | index  | index_page1aa       |      0 | 2017-01-26 07:17:27 | 2017-01-26 09:37:05 | NULL       |
|     131072 | 20000      | index2_pagess | index1 |      1 | 2017-01-26 06:10:03 | 2017-01-26 06:45:32 | NULL       |     131072 |        512 | 200        | index1 | index1_pagedfsafdsa |      0 | 2017-01-26 07:53:49 | 2017-01-26 09:23:45 | NULL       |
|     196608 | 30000      | index2_pages  | index2 |      0 | 2017-01-26 06:55:54 | 2017-01-26 06:55:54 | NULL       |     196608 |        768 | 300        | index2 | index2_pageaa       |      0 | 2017-01-26 07:56:19 | 2017-01-26 09:30:21 | NULL       |
+------------+------------+---------------+--------+--------+---------------------+---------------------+------------+------------+------------+------------+--------+---------------------+--------+---------------------+---------------------+------------+
3 rows in set (0.00 sec)


-- , 2張表的執行計劃,
mysql> explain select * from modules as a,objects as b where a.module_dec= b.module_dec\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: a
         type: ALL
possible_keys: PRIMARY
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 3
        Extra:
*************************** 2. row ***************************
           id: 1
  select_type: SIMPLE
        table: b
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 3
        Extra: Using where; Using join buffer
2 rows in set (0.00 sec)

ERROR:
No query specified


-- join 查詢的執行計劃
mysql> explain select * from modules as a join objects as b on a.module_dec=b.module_dec\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: a
         type: ALL
possible_keys: PRIMARY
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 3
        Extra:
*************************** 2. row ***************************
           id: 1
  select_type: SIMPLE
        table: b
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 3
        Extra: Using where; Using join buffer
2 rows in set (0.00 sec)

ERROR:
No query specified


--代碼級的執行過程,
select `code`.`a`.`module_dec` AS `module_dec`,`code`.`a`.`module_hex` AS `module_hex`,`code`.`a`.`msg` AS `msg`,`code`.`a`.`name` AS `name`,`code`.`a`.`status` AS `status`,`code`.`a`.`created_at` AS `created_at`,`code`.`a`.`updated_at` AS `updated_at`,`code`.`a`.`deleted_at` AS `de
leted_at`,`code`.`b`.`module_dec` AS `module_dec`,`code`.`b`.`object_dec` AS `object_dec`,`code`.`b`.`object_hex` AS `object_hex`,`code`.`b`.`name` AS `name`,`code`.`b`.`msg` AS `msg`,`code`.`b`.`status` AS `status`,`code`.`b`.`created_at` AS `created_at`,`code`.`b`.`updated_at` AS `updated_at`,`cod
e`.`b`.`deleted_at` AS `deleted_at` from `code`.`modules` `a` join `code`.`objects` `b` where (`code`.`b`.`module_dec` = `code`.`a`.`module_dec`) |



`code`.`a`.`module_dec` AS `module_dec`,`code`.`a`.`module_hex` AS `module_hex`,`code`.`a`.`msg` AS `msg`,`code`.`a`.`name` AS `name`,`code`.`a`.`status` AS `status`,`code`.`a`.`created_at` AS `created_at`,`code`.`a`.`updated_at` AS `updated_at`,`code`.`a`.`deleted_at` AS `deleted_at
`,`code`.`b`.`module_dec` AS `module_dec`,`code`.`b`.`object_dec` AS `object_dec`,`code`.`b`.`object_hex` AS `object_hex`,`code`.`b`.`name` AS `name`,`code`.`b`.`msg` AS `msg`,`code`.`b`.`status` AS `status`,`code`.`b`.`created_at` AS `created_at`,`code`.`b`.`updated_at` AS `updated_at`,`code`.`b`.`
deleted_at` AS `deleted_at` from `code`.`modules` `a` join `code`.`objects` `b` where (`code`.`b`.`module_dec` = `code`.`a`.`module_dec`)
1 row in set (0.00 sec)


-- 總結:
從代碼級的執行過程能夠看到,用,鏈接2張表的查詢過程,相似於join查詢的,
從執行計劃結果來看,Using join buffer的使用,說明當from 後由2張表的時候,產生了虛擬表.
具體理解 <mysql技術內幕>  p77;
相關文章
相關標籤/搜索