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

Index on field defined with "collate unicode_ci" is not used in GROUP BY on that field [CORE4787] #5086

Open
firebird-automations opened this issue May 11, 2015 · 5 comments

Comments

@firebird-automations
Copy link
Collaborator

Submitted by: @pavel-zotov

SQL> recreate table t2(a varchar(10) character set utf8 collate unicode_ci); commit;
SQL> insert into t2 select left(uuid_to_char(gen_uuid()),10) from rdb$types,rdb$types; commit;
SQL> create index t2_a on t2(a); commit;
SQL> set planonly;
SQL> select a from t2 group by a;

PLAN SORT ((T2 NATURAL))

Reproduced on WI-V2.5.5.26865, WI-T3.0.0.31828.

PS. Sorry if this ticket duplicates something old: I can`t get rid of sense that this question already was discussed but could not find corresp. ticket.

@firebird-automations
Copy link
Collaborator Author

Commented by: Sean Leyne (seanleyne)

How many rows where in T2 table?

Depending on the number of rows in the table, a NATURAL scan would seem appropriate, regardless of the index definitions/collations.

@firebird-automations
Copy link
Collaborator Author

Commented by: @pavel-zotov

> Depending on the number of rows in the table, a NATURAL scan would seem appropriate

Not in this case :-)
And this is exactly COLLATE clause in field definition that prevents from usage of index:

C:\FBTESTING\qa\fbt-repo\tmp>isql localhost/3333:e30
Database: localhost/3333:e30
SQL> recreate table t2(a varchar(10) character set utf8); commit;
SQL> set count on;
SQL> insert into t2 select left(uuid_to_char(gen_uuid()),10) from rdb$types,rdb$types; commit;
Records affected: 64516
SQL> set count off;
SQL> create index t2_a on t2(a); commit;
SQL> set planonly;
SQL> select a from t2 group by a;

PLAN (T2 ORDER T2_A)

@firebird-automations
Copy link
Collaborator Author

Commented by: Sean Leyne (seanleyne)

Had you posted the non-collate example with the original post, we could have saved these exchanges ;-)

@firebird-automations
Copy link
Collaborator Author

Commented by: @asfernandes

This is know limitation documented in the code:

// ASF: We currently can't use non-unique ["separate unique"] index for GROUP BY and DISTINCT with
// multi-level and insensitive collation. In NAV, keys are verified with memcmp
// but there we don't know length of each level.
if (sort->unique && (tt->getFlags() & TEXTTYPE_SEPARATE_UNIQUE))
{
usableIndex = false;
break;
}

@firebird-automations
Copy link
Collaborator Author

Commented by: @pavel-zotov

> We currently can't use non-unique ["separate unique"] index for GROUP BY and DISTINCT with multi-level and insensitive collation

Adriano,

can you please clarify what is "SEPARATE unique" index ?

-------
BTW (sorry if offtop): seems that DISTINCT still does not use ANY index at all, not only for text fields:

SQL> recreate table t1(x int); commit;
SQL> insert into t1 select rand()*1000 from rdb$types, rdb$types; commit;
SQL> create index t1_x_unq on t1(x); commit;
SQL> set planonly;
SQL> select x from t1 group by x;

PLAN (T1 ORDER T1_X_UNQ)

SQL> select distinct x from t1;

PLAN SORT (T1 NATURAL)

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

No branches or pull requests

1 participant