0.38.2

drizzle-team/drizzle-orm0.38.2Dec 13, 2024by github-actions[bot]

AI Summary

This release adds support for MySQL index hints (USE INDEX, FORCE INDEX, and IGNORE INDEX) to drizzle-orm, providing developers with fine-grained control over how the MySQL query optimizer selects indexes for query execution.

Key Highlights

  • Added USE INDEX hint - suggests which indexes the optimizer should consider
  • Added IGNORE INDEX hint - tells optimizer to avoid specific indexes
  • Added FORCE INDEX hint - forces the optimizer to use specified indexes
  • Multiple index hints can be combined in a single query
  • Available in select queries via options object (useIndex, ignoreIndex, forceIndex)

New Features

  • USE INDEX hint for MySQL - suggests preferred indexes to the query optimizer
  • IGNORE INDEX hint for MySQL - excludes specific indexes from consideration
  • FORCE INDEX hint for MySQL - forces use of specified indexes

Full Release Notes

# New features

## `USE INDEX`, `FORCE INDEX` and `IGNORE INDEX` for MySQL

In MySQL, the statements USE INDEX, FORCE INDEX, and IGNORE INDEX are hints used in SQL queries to influence how the query optimizer selects indexes. These hints provide fine-grained control over index usage, helping optimize performance when the default behavior of the optimizer is not ideal.

### Use Index

The `USE INDEX` hint suggests to the optimizer which indexes to consider when processing the query. The optimizer is not forced to use these indexes but will prioritize them if they are suitable.

```ts 
export const users = mysqlTable('users', {
  id: int('id').primaryKey(),
  name: varchar('name', { length: 100 }).notNull(),
}, () => [usersTableNameIndex]);

const usersTableNameIndex = index('users_name_index').on(users.name);

await db.select()
  .from(users, { useIndex: usersTableNameIndex })
  .where(eq(users.name, 'David'));
```

### Ignore Index

The `IGNORE INDEX` hint tells the optimizer to avoid using specific indexes for the query. MySQL will consider all other indexes (if any) or perform a full table scan if necessary.

```ts
export const users = mysqlTable('users', {
  id: int('id').primaryKey(),
  name: varchar('name', { length: 100 }).notNull(),
}, () => [usersTableNameIndex]);

const usersTableNameIndex = index('users_name_index').on(users.name);

await db.select()
  .from(users, { ignoreIndex: usersTableNameIndex })
  .where(eq(users.name, 'David'));
```

### Force Index

The `FORCE INDEX` hint forces the optimizer to use the specified index(es) for the query. If the specified index cannot be used, MySQL will not fall back to other indexes; it might resort to a full table scan instead.

```ts copy
export const users = mysqlTable('users', {
  id: int('id').primaryKey(),
  name: varchar('name', { length: 100 }).notNull(),
}, () => [usersTableNameIndex]);

const usersTableNameIndex = index('users_name_index').on(users.name);

await db.select()
  .from(users, { forceIndex: usersTableNameIndex })
  .where(eq(users.name, 'David'));
```

You can also combine those hints and use multiple indexes in a query if you need