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

support for conditional index #353

Closed
carmi2214 opened this issue Jan 22, 2022 · 12 comments · Fixed by #485
Closed

support for conditional index #353

carmi2214 opened this issue Jan 22, 2022 · 12 comments · Fixed by #485

Comments

@carmi2214
Copy link

carmi2214 commented Jan 22, 2022

Hi, I think it would be very useful to add in Index.java a property called condition / where, to use in DB types that support the feature.

For example:

@Target({})
@Retention(RUNTIME)
public @interface Index {

    /**
     * (Optional) The name of the index; defaults to a provider-generated name.
     */
    String name() default "";

    /**
     * (Required) The names of the columns to be included in the index, 
     * in order.
     */
    String columnList();

    /**
     * (Optional) Whether the index is unique.
     */
    boolean unique() default false;

    /**
     * (Optional) The contents of the where clause while creating the index.
     */
    String where() default "";

}

Useful for indicies on tables with logical deletion, or many other cases

@gavinking
Copy link
Contributor

How many databases support this? Just Postgres and SQL Server?

@carmi2214
Copy link
Author

How many databases support this? Just Postgres and SQL Server?

Yep, those two and SQLite

@beikov
Copy link

beikov commented Aug 11, 2023

It can be easily and safely emulated by transforming the index to a unique one I.e. index on tbl1(col1) where col2 = 1 to unique index on tbl1(col1, case when col2 = 2 then primaryKey else null end) because nulls in unique indexes are ignored by default.

@gavinking
Copy link
Contributor

gavinking commented Aug 11, 2023

Mmmmm. So look, every database has a bunch of fancy proprietary options for creating indexes. We can't possibly expand the @Index annotation to accommodate all these things individually.

Now, perhaps it could make sense to add something like indexDefinition in analogy to columnDefinition in @Column, so you could write:

@Index(name="orders_unbilled_index ", 
       columnList="order_nr", 
       indexDefinition ="where billed is not true")

To produce this DDL:

create index orders_unbilled_index on orders (order_nr)
       where billed is not true;

And of course you could also use it for lots of other things.

I guess that's kinda intellectually consistent at least.

On the other hand, you could just put that create index statement in a DDL script, and the JPA provider will happily execute the script for you, so the need is not really that acute, IMO.

Dunno.

[Note: I wrote this before seeing @beikov's comment.]

@gavinking
Copy link
Contributor

It can be easily and safely emulated by transforming the index to a unique one

Ah, that's interesting.

@carmi2214
Copy link
Author

It can be easily and safely emulated by transforming the index to a unique one I.e. index on tbl1(col1) where col2 = 1 to unique index on tbl1(col1, case when col2 = 2 then primaryKey else null end) because nulls in unique indexes are ignored by default.

Thank you for the suggestion, though still trying to add that option since it seems more like an elegant solution, for the relevant databases, IMO.

@carmi2214
Copy link
Author

Mmmmm. So look, every database has a bunch of fancy proprietary options for creating indexes. We can't possibly expand the @Index annotation to accommodate all these things individually.

Looks awsome.
Would using it in ORM libraries be acceptable?

@gavinking
Copy link
Contributor

@carmi2214 We need to pick just one of those alternatives. You can't have both 😅

@carmi2214
Copy link
Author

Of course - excuse me, by "awsome" I meant to respond on the indexDefinition idea 😄

@gavinking
Copy link
Contributor

gavinking commented Aug 18, 2023

Actually, since the @ForeignKey annotation defines a foreignKeyDefinition member which is supposed to specify the whole DDL definition of the FK, instead of just some text to be appended at the end, we had better stay away from the use of "definition" here.

So perhaps what we should do is call it options:

Index(name = "orders_unbilled_index ", 
      columnList = "order_nr", 
      options = "where billed is not true")

We could even add options members to other annotations like @UniqueConstraint, @ForeignKey, and even @Table and @Column.

I think this would be a quite useful enhancement.

@carmi2214
Copy link
Author

carmi2214 commented Aug 22, 2023

@gavinking Great! So, what's next?

@gavinking
Copy link
Contributor

So your nagging worked: #485.

But now lets see what other people think.

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

Successfully merging a pull request may close this issue.

3 participants