Skip to content

Index hints #9421

Open

Activity

  1. added
    type: feature
    dialect: mssql
    dialect: mysql
    dialect: sqlite
    on May 9, 2018
  2. jessethomson commented on May 17, 2018

    @jessethomson

    Okay, I think a good starting point might be to go over SQL capabilities, and how/if we want to implement all of these.

    Valid MySQL queries:

    1. SELECT * FROM t1 USE INDEX (); Without using indices
    2. SELECT * FROM t1 USE INDEX (i1); With single index
    3. SELECT * FROM t1 USE INDEX (i1,i2); With multiple indices
    4. SELECT * FROM t1 USE INDEX (i1) USE INDEX (i2); With multiple USE INDEX

    I think 1, 2, and 3 are all reasonable functionality. I don't see any reason why we would need to implement 4. As far as I know, 4 is simply a syntactically different way to represent 3, without adding any new functionality (I could be wrong).

    Note: It is perfectly valid to use the same index more than once in an index hint.
    For example:
    SELECT * FROM t1 USE INDEX (i1) USE INDEX (i1);
    and
    SELECT * FROM t1 USE INDEX (i1,i1);
    are both valid statement so we don't need to enforce index uniqueness.

  3. jessethomson commented on May 17, 2018

    @jessethomson

    Also, it looks like all 3 Pull Requests referenced in the original post are for MySQL. I don't think there is any actual demand for this feature outside of the MySQL dialect; at least nothing that I have been able to find online.

  4. harbolaez commented on May 17, 2018

    @harbolaez

    Agree with you @jwt1143 on my PR I was able to pass a collection of indexes. I like option 1, 2 and 3 aswell

    SELECT * FROM Order USE INDEX(idx_status_id) WHERE status_id = 1

  5. harbolaez commented on May 17, 2018

    @harbolaez

    Also, I like to support the ability to use FORCE INDEX sometimes is useful but rare.

  6. ezequielzacca commented on May 18, 2018

    @ezequielzacca

    is not rare at all for very large databases

  7. jessethomson commented on May 18, 2018

    @jessethomson

    Okay. I think it makes sense to support force (and probably ignore too in that case).

    Here's an API proposal:

    We add an indexHints property to the sequelize options.

    db.User.findOne({
        where: {
            email: "[email protected]",
        },
        indexHints: [
            { type: "use", values: ["i1"] }
        ],
    });
    

    Examples:

    SELECT * FROM t1 USE INDEX ();

    indexHints: [
        { type: "use", values: [] },
    ]
    

    SELECT * FROM t1 USE INDEX (i1);

    indexHints: [
        { type: "use", values: ["i1"] },
    ]
    

    SELECT * FROM t1 USE INDEX (i1,i2);

    indexHints: [
        { type: "use", values: ["i1", "i2"] },
    ]
    

    SELECT * FROM t1 FORCE INDEX (i1);

    indexHints: [
        { type: "force", values: ["i1"] },
    ]
    

    SELECT * FROM t1 IGNORE INDEX (i1);

    indexHints: [
        { type: "force", values: ["i1"] },
    ]
    

    SELECT * FROM t1 USE INDEX (i1,i2) IGNORE INDEX (i2);

    indexHints: [
        { type: "use", values: ["i1","i2"] },
        { type: "ignore", values: ["i2"] },
    ]
    

    Notes

    I prefer using objects over arrays to represent each index hint because they are more explicit.
    For example:

    indexHints:[
        ["use", ["i1","i2"]]
    ]
    

    is not nearly as clear as

    indexHints: [
        { type: "use", values: ["i1","i2"] },
    ]
    

    Being more verbose shouldn't be an issue as indexHints are only used in limited/rare circumstances.

  8. sushantdhiman commented on May 23, 2018

    @sushantdhiman
    ContributorAuthor

    @jwt1143 Your proposal looks good, some comments

    type should be an constant like query hints

    indexHints: [
        { type: IndexHints.USE || FORCE || IGNORE, values: ["i1","i2"] },
    ]

    I also do perfer object based approach

    indexHints: [
        { type: IndexHints.USE, values: ["i1","i2"] },
    ]

    As for implementation

    MSSQL

    We are only going to introduce this

    WITH (TableHints, INDEX (<index>))

    Form of table hint expansion, we already support table hints for MSSQL, we just need to include index part when indexHints is also supplied, see this pull request

    SQLite

    Only need to add

    INDEXED BY <index_name>

    For any given indexHints

    MySQL

    Up to you, as that is your primary objective with this issue

  9. monukanyal commented on Oct 5, 2018

    @monukanyal
  10. OsoianMarcel commented on Nov 5, 2018

    @OsoianMarcel
  11. peteychuk commented on Dec 12, 2018

    @peteychuk
  12. danfarewell commented on Dec 19, 2018

    @danfarewell
  13. KevinTemes commented on Mar 18, 2019

    @KevinTemes
  14. Diferno commented on Apr 2, 2019

    @Diferno
  15. stale commented on Jul 23, 2019

    @stale
  16. benjamin658 commented on Oct 17, 2019

    @benjamin658
    Contributor

    Will the indexHints option support mariadb dialect?

  17. papb commented on Oct 25, 2019

    @papb
    Member

    @benjamin658 Thanks for adding it on #11573 :)

  18. added
    dialect: mariadb
    on Oct 25, 2019
  19. timbowhite commented on Jan 9, 2020

    @timbowhite

    MySQL supports index hints on JOIN statements. It'd be nice to be able to do this:

    Project.findAll({
      where: {
        id: {
          [Op.gt]: 623
        },
        name: {
          [Op.like]: 'Foo %'
        }
      },
      include:[{
        association: Company,
        indexHints: [
          {
            type: IndexHints.USE, 
            values: ['index1', 'index2'] 
          }
        ]
      }]
    })
    

    and get something like this:

    SELECT * FROM Project
    INNER JOIN `company` AS `Company` 
      USE INDEX (`index1`,`index2`) 
      ON `Project`.`company_id` = `Company`.`id`
    WHERE name LIKE 'FOO %' AND id > 623;
    

    PR incoming.

  20. theironcook commented on Oct 13, 2020

    @theironcook

    Is it possible to get indexHints enabled with sql server / tedious?

  21. QuanticPotatoes commented on Mar 5, 2021

    @QuanticPotatoes

    @timbowhite The index hints on JOIN statements features would be so life saving 🙏

  22. arafat-al-mahmud commented on Apr 24, 2021

    @arafat-al-mahmud

    Any workaround in v3?

  23. added theissue type on Mar 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    dialect: mariadbdialect: mssqldialect: mysqldialect: sqlitetype: feature

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions