Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to force YDB to use table index, not to read all rows. #44

Closed
vladd11 opened this issue May 1, 2022 · 2 comments
Closed

How to force YDB to use table index, not to read all rows. #44

vladd11 opened this issue May 1, 2022 · 2 comments

Comments

@vladd11
Copy link

vladd11 commented May 1, 2022

Hello. I have this table:

CREATE TABLE `order_items`
(
    `id` String,
    `order_id` String,
    `product_id` String,
    `quantity` Uint32,
    PRIMARY KEY (`id`)
);

with index idx_order_id_quantity -> order_id, quantity

I'd like to get quantity by order_id, so I use this query:

SELECT quantity
FROM order_items
WHERE order_id=""

But it scans all rows in table instead of using my index.

Explanation
{
    "plan": {
        "meta": {
            "type": "script",
            "version": "0.2"
        },
        "queries": [
            {
                "tables": [
                    {
                        "name": "/ru-central1/b1gnjh1pn5tbeqdb2lqt/etn4ge82u3bfv9qldrbk/order_items",
                        "reads": [
                            {
                                "columns": [
                                    "id",
                                    "order_id"
                                ],
                                "scan_by": [
                                    "id"
                                ],
                                "type": "FullScan"
                            }
                        ]
                    }
                ]
            }
        ]
    }
}
@snaury
Copy link
Member

snaury commented May 2, 2022

You need to explicitly specify which index you want YDB to use in your query, which in your case would probably be something like SELECT quantity FROM order_items VIEW idx_order_id_quantity WHERE order_id = .... Please see https://ydb.tech/en/docs/best_practices/secondary_indexes for more details.

@vladd11
Copy link
Author

vladd11 commented May 2, 2022

Oh, thanks, it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants