Skip to content

Conversation

@GuySartorelli
Copy link
Member

this index is present on the associative entity).
- All fields used in `default_sort` configuration
- All fields used in `default_sort` configuration (see [`default_sort` index mode](#default-sort-index-mode) below for additional options)
- Some built-in models (such as [`Member`](api:SilverStripe\Security\Member)) have specific indexes added
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this last note for completion's sake

class MyObject extends DataObject
{
private static $indexes = [
private static array $indexes = [
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated - just updating the config references in this page to follow latest best practice

```

## Complex/Composite indexes
### Complex/Composite indexes
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense as a subheading of "Defining an index"

Comment on lines 93 to 114
- index (col1) - `WHERE col1 = ?`
- index (col1, col2) = `WHERE (col1 = ? AND col2 = ?)`
- index (col1, col2, col3) = `WHERE (col1 = ? AND col2 = ? AND col3 = ?)`
```php
namespace App\Model;

The index would not be used for a query `WHERE col2 = ?` or for `WHERE col1 = ? OR col2 = ?`
use SilverStripe\ORM\DataObject;

class MyTestObject extends DataObject
{
// ...
private static array $indexes = [
'MyCompositeIndex' => ['MyField', 'MyOtherField', 'MyThirdField'],
];
}
```

Composite indexes can be used for any query that uses the fields in this specific order, include queries that omit columns from later in the index. For example, the index would be used for these queries:

- `WHERE MyField = ?`
- `WHERE (MyField = ? AND MyOtherField = ?)`
- `WHERE (MyField = ? AND MyOtherField = ? AND MyThirdField = ?)`

The index would not be used for a query `WHERE MyOtherField = ?` (because the first column is not used in this query) or for `WHERE MyField = ? OR MyOtherField = ?` (because the database has to check all rows to see if the `MyOtherField` column matches on its own).
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated this to be clearer - it wasn't really obvious what index (col1, col2) meant - was that a separate index or just showing that only col1 and col2 of the 3-column index were being used?

Copy link
Member

@emteknetnz emteknetnz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge conflict

@GuySartorelli GuySartorelli force-pushed the pulls/6/default-sort-index branch 2 times, most recently from cf37906 to ef8177c Compare June 4, 2025 21:31
@GuySartorelli
Copy link
Member Author

Conflict resolved

@GuySartorelli GuySartorelli force-pushed the pulls/6/default-sort-index branch from ef8177c to d54bab7 Compare June 4, 2025 21:55
@emteknetnz emteknetnz merged commit a09d724 into silverstripe:6 Jun 5, 2025
3 checks passed
@emteknetnz emteknetnz deleted the pulls/6/default-sort-index branch June 5, 2025 02:05
emteknetnz pushed a commit to creative-commoners/developer-docs that referenced this pull request Jul 22, 2025
…efault-sort-index

DOC Document default_sort composite indexes
lozcalver pushed a commit to lozcalver/developer-docs that referenced this pull request Nov 14, 2025
…efault-sort-index

DOC Document default_sort composite indexes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants