cockroach icon indicating copy to clipboard operation
cockroach copied to clipboard

sql: SELECT queries are not utilizing indexes that complete faster with hints

Open ricardocrdb opened this issue 7 years ago • 8 comments

User is running v19.2-rc.2 and has two SELECT queries that are taking ~200 ms. When forcing the same queries to use a specific index, the time it takes to complete is around 5 ms.

The EXPLAIN (OPT,ENV) for query 1 shows that the table is being scanned with constraints:

EXPLAIN (OPT, ENV) SELECT * FROM public.table WHERE uuid = 'UUID';

select
 ├── scan public.table
 │    └── constraint: /7/1: [ - /'location') [/'location'/'UUID' - /'location'/'UUID'] [/e'location\x00'/'UUID' - /'location') [/'location'/'UUID' - /'location'/'UUID'] [/e'location\x00'/'UUID' - /'location') [/'location'/'UUID' - /'location'/'UUID'] [/e'location\x00'/'UUID' - ]
 └── filters
      └── uuid = 'UUID'

The time query 1 takes to complete is:

FROM public.table
WHERE uuid = 'UUID';

Time: 165.063763ms

but with a hint to force it to use the expected index, it takes significantly faster:

WHERE uuid = 'UUID';

Time: 1.753048ms

The other query is a SELECT with an INNER JOIN, but essentially the same issue occurs.

DDLs and full EXPLAIN (OPT, ENV) outputs are located here

ricardocrdb avatar Nov 06 '19 20:11 ricardocrdb

Zendesk ticket #4022 has been linked to this issue.

RoachietheSupportRoach avatar Nov 06 '19 20:11 RoachietheSupportRoach

Query 1 is interesting. We are choosing the primary key because we can do a scan that we know will return one row anyway, but this scan goes to multiple regions.

We have indexes on uuid, but those require an index-join and have higher cost. The index join involves the primary key which is in a single region.

The current latency-related costing in the optimizer is very primitive, it is there to choose the local index if all else is equal. Longer term, the optimizer will be more aware of location latency and better take that into account.

One simple thing which we could do which might fix this case would be to add a cost per-span for each scan - similar to how we add a random-IO cost for each span in a lookup join (this would also fix #41701).

RaduBerinde avatar Nov 07 '19 18:11 RaduBerinde

Adding @rytaft because it's a good example for the distribution-awareness work.

RaduBerinde avatar Nov 07 '19 18:11 RaduBerinde

Hey @RaduBerinde thanks for the info on the 1st query. What do you make of the 2nd SELECT with the INNER JOIN? The DDL and EXPLAIN are in that same link above.

ricardocrdb avatar Nov 07 '19 21:11 ricardocrdb

Hey @RaduBerinde did you have a chance to checkout that other query? Let me know if anything else is needed.

ricardocrdb avatar Nov 13 '19 21:11 ricardocrdb

Sorry, looked at it now. It's very similar - we choose to look up in the index that doesn't require index-join. The costing terms we currently have related to locality aren't strong enough to make the query with the index-join cheaper.

RaduBerinde avatar Nov 13 '19 22:11 RaduBerinde

We have marked this issue as stale because it has been inactive for 18 months. If this issue is still relevant, removing the stale label or adding a comment will keep it active. Otherwise, we'll close it in 5 days to keep the issue queue tidy. Thank you for your contribution to CockroachDB!

github-actions[bot] avatar Jun 04 '21 19:06 github-actions[bot]

These queries are now choosing the correct plan on master. The larger issue of distribution awareness is already tracked by #47226. If this customer needs us to backport a particular fix please feel free to open another issue, but I'll close this now since it seems like the issue is resolved on master.

rytaft avatar Jun 07 '21 15:06 rytaft