How does index on array works

For this query to work:

select * from `mybucket`;

you’ll need a primary index on the bucket:

create primary index on `mybucket`;

When you want to search on the exit_code in an array, you need to put that as the array index element instead of the when clause of the array index:

create index ix_scripts_exitcode on `mybucket` (DISTINCT array s.exit_code FOR s IN scripts END, id);

query:

select id from `mybucket` as data where ANY s IN data.scripts SATISFIES s.exit_code = 0 END;

note also that you originally have id as the leading index key, and since your query does not have a predicate on id the index will not be chosen. The suggested index has id as the second field so it can be used for covering purpose.

Also try:

http://index-advisor.couchbase.com

1 Like