[MDEV-31493] Query with selectall is slower than select a specific column as it does not use the index Created: 2023-06-18  Updated: 2023-06-29  Resolved: 2023-06-29

Status: Closed
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: None
Fix Version/s: N/A

Type: Bug Priority: Major
Reporter: Chintan Thakkar Assignee: Sergei Golubchik
Resolution: Not a Bug Votes: 0
Labels: None
Environment:

Mariadb 11.0.2



 Description   

Adding below both explain queries with results.
The selectAll query is not using the index.

EXPLAIN SELECT
    *
FROM
    `orders`
WHERE
    amazon_order_id IN (
        SELECT
            j.azid
        FROM
            marketplace_order_data m,
            JSON_TABLE(
                m.shipment_order_ids,
                '$[*]' COLUMNS(azid varchar(20) PATH '$')
            ) AS j
        WHERE
            shipment_id IN ('XXX')
    );

+------+-------------+------------+-------+---------------+------------+---------+-------+--------+-----------------------------------------------------------------------------------------------------+
| id   | select_type | table      | type  | possible_keys | key        | key_len | ref   | rows   | Extra                                                                                               |
+------+-------------+------------+-------+---------------+------------+---------+-------+--------+-----------------------------------------------------------------------------------------------------+
|    1 | PRIMARY     | m          | const | shipmentId    | shipmentId | 98      | const | 1      |                                                                                                     |
|    1 | PRIMARY     | orders | ALL   | NULL          | NULL       | NULL    | NULL  | 599922 |                                                                                                     |
|    1 | PRIMARY     | j          | ALL   | NULL          | NULL       | NULL    | NULL  | 40     | Table function: json_table; Using where; FirstMatch(orders); Using join buffer (flat, BNL join) |
+------+-------------+------------+-------+---------------+------------+---------+-------+--------+-----------------------------------------------------------------------------------------------------+

EXPLAIN SELECT
    amazon_order_id
FROM
    `orders`
WHERE
    amazon_order_id IN (
        SELECT
            j.azid
        FROM
            marketplace_order_data,
            JSON_TABLE(
                shipment_order_ids,
                '$[*]' COLUMNS(azid varchar(20) PATH '$')
            ) AS j
        WHERE
            shipment_id IN ('XXX')
    );

+------+-------------+----------------------------+-------+---------------+-----------------+---------+-------+--------+-----------------------------------------------------------------------------------------------------+
| id   | select_type | table                      | type  | possible_keys | key             | key_len | ref   | rows   | Extra                                                                                               |
+------+-------------+----------------------------+-------+---------------+-----------------+---------+-------+--------+-----------------------------------------------------------------------------------------------------+
|    1 | PRIMARY     | marketplace_order_data | const | shipmentId    | shipmentId      | 98      | const | 1      |                                                                                                     |
|    1 | PRIMARY     | orders                 | index | NULL          | amazon_order_id | 188     | NULL  | 599930 | Using index                                                                                         |
|    1 | PRIMARY     | j                          | ALL   | NULL          | NULL            | NULL    | NULL  | 40     | Table function: json_table; Using where; FirstMatch(orders); Using join buffer (flat, BNL join) |
+------+-------------+----------------------------+-------+---------------+-----------------+---------+-------+--------+-----------------------------------------------------------------------------------------------------+



 Comments   
Comment by Sergei Golubchik [ 2023-06-29 ]

see https://mariadb.com/kb/en/explain/#type-column

"index" means "A full scan over the used index" and "Using index" means "Only the index is used to retrieve the needed information from the table"

To use "only the index" all the columns that the query needs must be in the index. If you select columns that aren't in that index, "Using index" is not applicable

Generated at Thu Feb 08 10:24:18 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.