Sample Header Ad - 728x90

Mysql system tables showing incorrect cardinality for columns

0 votes
0 answers
47 views
I’m new to mysql. I have encountered a weird problem on our prod database. We have a table with primary key a and an index on some other b column. Below’s the query:
select b from my_table where a in (1,2,3,4,5,6,7,8,9,10,11,12....n) ;
Ideally for such cases my query should use PRIMARY KEY index of column a. Also, I have verified that by default my column a should have all unique values which it already has. And my column b has several of duplicate values. But, when I checked execution plan, the query is using index on column b (b-idx) rather than PRIMARY KEY index. I ran analyze on this table, but even after there are no changes into execution plan. I can’t run optimize since it’s on prod. Then, I checked for show index from my_table ; And, there I found quite weird thing, Cardinality value for column a was lower than column b . I guess by default optimizer picks up higher cardinality column, so it’s picking column b-idx Please guide me what to do in such cases. Is this any bug in mysql or expected behaviour ? I couldn’t find how to update stats for specific index on prod table. I want our query to use right index by default
Asked by Shiwangini (380 rep)
Sep 7, 2023, 05:33 PM