简体   繁体   中英

Mysql converting subquery into dependent subquery

Hi I am not understanding , why the subquery of given query is converting into dependent subquery.

Although the subquery is not dependent(not using primary query table) on main query.

I know that this query can be optimized using joins,but here i just want to know the reason of this

MYSQL Version 5.5

EXPLAIN SELECT id  FROM  `cab_request_histories` 
WHERE cab_request_histories.id = any(SELECT id
                                     FROM cab_requests
                                     WHERE cab_requests.request_type =  'pickup')

id   select_type      table type           possible_keys     key               key_len  ref rows Extra
1    PRIMARY    cab_request_histories   index             NULL             PRIMARY  4   NULL    20                       

2    DEPENDENT       SUBQUERY          cab_requests unique_subquery    PRIMARY  PRIMARY 4 func  1

我怀疑ANY关键字将要求MySQL将子查询外部的值传递到其中以评估结果是否为true。

Mysql optimizer uses EXIST strategy for this query, effectively changing it to something like:

SELECT id  FROM  cab_request_histories
WHERE EXISTS 
  ( SELECT 'this one is dependent' FROM cab_requests
    WHERE cab_requests.request_type =  'pickup' 
    AND cab_requests.id = cab_request_histories.id )

You can see what optimizer does with your query using EXPLAIN EXTENDED your_query followed by SHOW WARNINGS .

This type of optimization is described in http://dev.mysql.com/doc/refman/5.5/en/subquery-optimization-with-exists.html .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM