yiisoft / yiisoft/db

Add Hint Index in queries

Open
#90 38 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
216
Forks
51
Avg merge
3d 10h
Merged PRs (30d)
2

Description

I need force index in my project, so I've add the method addHintIndex to set the hints indexes associated to a table.
The param is an assoc array of the hintIndex(s) to be used on the query.
The key is the name of the table to be hinted.
The value is the hint settings or an array of hints settings to apply.
Each hint setting is compose by one to three string values defining the hint and one array defining the index(s) to hint.

Here are some examples:

$query = (new \yii\db\Query())->from(['t' => 'user'])->addHintIndex(['user' => [
    ['force', 'index', ['primary']],
    ['ignore', 'index', 'order by', ['i1']],
]])->leftJoin('profile as p', 'user.id = profile.user_id')->addHintIndex(['profile' => [
    'use', 'index', ['i2']
]]);

// sql -> SELECT * FROM `user` `t` FORCE INDEX (primary) IGNORE INDEX FOR ORDER BY (i1)
 LEFT JOIN `profile` `p` ON user.id = profile.user_id USE INDEX (i2)
$users = User::find()->addHintIndex(['user' => [['use', 'index', ['primary']]]])->one();

// sql -> SELECT * FROM {{%user}} USE INDEX (primary)

I have no experience enought with other drivers than mysql, so just write functionality for mysql and sqlite drivers (the former tested the last pending for testing).

Driver overwritting can be done on:

  • /yii2/framework/db/mysql/QueryBuilder.php
  • /yii2/framework/db/sqlite/QueryBuilder.php (require testing)*
  • /yii2/framework/db/cubrid/QueryBuilder.php (trace a message and ignore addHintIndex)*
  • /yii2/framework/db/mssql/QueryBuilder.php (trace a message and ignore addHintIndex)*
  • /yii2/framework/db/oci/QueryBuilder.php (trace a message and ignore addHintIndex)*
  • /yii2/framework/db/pgsql/QueryBuilder.php (trace a message and ignore addHintIndex)*

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading framework/db/mysql/QueryBuilder.php and framework/db/sqlite/QueryBuilder.php, then trace how Query builds table and join SQL. Check the corresponding driver QueryBuilder entry points listed in the issue; done means addHintIndex is handled for MySQL and SQLite and safely ignored with a trace message by the other listed drivers.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, php, sqlite
Domain
database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.