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 index hints with the QueryBuilder #1663

Closed
merceyz opened this issue Apr 8, 2021 · 4 comments
Closed

Support index hints with the QueryBuilder #1663

merceyz opened this issue Apr 8, 2021 · 4 comments
Labels
enhancement New feature or request
Milestone

Comments

@merceyz
Copy link
Contributor

merceyz commented Apr 8, 2021

Is your feature request related to a problem? Please describe.

The MySQL query optimizer needs a little help choosing the right index every now and then and the query builder doesn't support this so I need to use knex and map the result to entities.

Describe the solution you'd like

A way to provide index hints using the query builder directly.

Describe alternatives you've considered

Using getKnex (or raw queries) and mapping the result to entities.

Additional context

https://dev.mysql.com/doc/refman/8.0/en/index-hints.html
https://stackoverflow.com/a/63603866

@merceyz merceyz added the enhancement New feature or request label Apr 8, 2021
@B4nan
Copy link
Member

B4nan commented Apr 8, 2021

Would a qb.from() method be good for this, or can you propose some API?

@B4nan B4nan added this to the 5.0 milestone Apr 8, 2021
@B4nan B4nan mentioned this issue Apr 8, 2021
48 tasks
@merceyz
Copy link
Contributor Author

merceyz commented Apr 12, 2021

Would a dedicated function make more sense or would that be too MySQL specific? (qb.index)

type IndexHint = {
	type: 'use' | 'force' | 'ignore';
	indexName: string | string[];
	forType?: 'JOIN' | 'ORDER BY' | 'GROUP BY' | Array<'JOIN' | 'ORDER BY' | 'GROUP BY'>;
};

declare function index(hint: IndexHint): void;

declare function index(
	type: IndexHint['type'],
	indexName: IndexHint['indexName'],
	forType?: IndexHint['forType']
): void;

@B4nan
Copy link
Member

B4nan commented Apr 12, 2021

Well, if this would work only for mysql, then I would rather not have such a common API directly on QB. Apparently sqlite has something similar but with different syntax and postgres does not support it at all.

Maybe we should just have a qb.indexHint(sql: string) that would add to the from clause (after the main table alias). That way we would support sqlite too.

@merceyz
Copy link
Contributor Author

merceyz commented Apr 12, 2021

That would work 👍

@B4nan B4nan closed this as completed in ce89e1f Apr 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants