簡體   English   中英

從同一張表中插入Select——子查詢優化

[英]Insert Select from the same table - subquery optimization

我想使用同一字段中另一條記錄的數據插入一條新記錄。 在這種情況下,新租戶需要使用與其父“0904ba44-9d41-418a-bbd8-2c70506c3432”相同的域名。

我的直覺告訴我,帶有JOIN的那個更好。 有一點智慧的人可以解釋為什么我的直覺是對還是錯。 想要開發更好的優化技能。

我有兩個可以工作的INSERT

這個帶有子查詢,有效。

INSERT INTO tenant (tenant_uuid, secondary_domain)
VALUES (
  UUID_TO_BIN(UUID()),
  (SELECT secondary_domain FROM (SELECT top_level_domain FROM tenant WHERE tenant_uuid = UUID_to_bin('0904ba44-9d41-418a-bbd8-2c70506c3432')) as b ));

用這個解釋。


+-----+--------------+---------------+-------------+-----------+------------------------+------------+----------+----------+-------+-----------+--------+
| id  |  select_type |     table     |  partitions |    type   |      possible_keys     |     key    |  key_len |    ref   |  rows |  filtered |  Extra |
+-----+--------------+---------------+-------------+-----------+------------------------+------------+----------+----------+-------+-----------+--------+
| '1' |  'INSERT'    |  'tenant'     |  NULL       |  'ALL'    |  NULL                  |  NULL      |  NULL    |  NULL    |  NULL |  NULL     |  NULL  |
| '2' |  'SUBQUERY'  |  '<derived3>' |  NULL       |  'system' |  NULL                  |  NULL      |  NULL    |  NULL    |  '1'  |  '100.00' |  NULL  |
| '3' |  'DERIVED'   |  'tenant'     |  NULL       |  'const'  |  'PRIMARY tenant_uuid' |  'PRIMARY' |  '16'    |  'const' |  '1'  |  '100.00' |  NULL  |
+-----+--------------+---------------+-------------+-----------+------------------------+------------+----------+----------+-------+-----------+--------+

這個使用連接,有效

INSERT INTO tenant (tenant_uuid, secondary_domain)
VALUES (
  UUID_TO_BIN(UUID()),
  (SELECT b.secondary_domain FROM tenant a INNER JOIN tenant b on a.tenant_uuid = b.tenant_uuid WHERE b.tenant_uuid = UUID_to_bin('0904ba44-9d41-418a-bbd8-2c70506c3432'));

用這個解釋


+------+--------------+-----------+-------------+----------+------------------------+------------+----------+----------+-------+-----------+----------------+
| # id |  select_type |   table   |  partitions |   type   |      possible_keys     |     key    |  key_len |    ref   |  rows |  filtered |      Extra     |
+------+--------------+-----------+-------------+----------+------------------------+------------+----------+----------+-------+-----------+----------------+
| '1'  |  'INSERT'    |  'tenant' |  NULL       |  'ALL'   |  NULL                  |  NULL      |  NULL    |  NULL    |  NULL |  NULL     |  NULL          |
| '2'  |  'SUBQUERY'  |  'a'      |  NULL       |  'const' |  'PRIMARY tenant_uuid' |  'PRIMARY' |  '16'    |  'const' |  '1'  |  '100.00' |  'Using index' |
| '2'  |  'SUBQUERY'  |  'b'      |  NULL       |  'const' |  'PRIMARY tenant_uuid' |  'PRIMARY' |  '16'    |  'const' |  '1'  |  '100.00' |  NULL          |
+------+--------------+-----------+-------------+----------+------------------------+------------+----------+----------+-------+-----------+----------------+


INSERT INTO tenant (tenant_uuid, secondary_domain)
SELECT UUID_TO_BIN(UUID()), secondary_domain 
FROM tenant
WHERE tenant_uuid = UUID_to_bin('0904ba44-9d41-418a-bbd8-2c70506c3432');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM