sql-concat icon indicating copy to clipboard operation
sql-concat copied to clipboard

Add a function for optimizer hint queries?

Open ArtskydJ opened this issue 1 year ago • 5 comments

https://dev.mysql.com/doc/refman/8.0/en/optimizer-hints.html

Add a function for optimizer hint queries?

I don't think I can do query.select('/*+ INDEX(table_alias foreign_key) */') or it would add a comma after it.

What API do you like for this? For now I'm probably doing a find/replace in my own code.

ArtskydJ avatar Jul 05 '24 21:07 ArtskydJ

Can you not put them into the select argument in the same string as a column?

q.select('/*+ NO_RANGE_OPTIMIZATION(t3 PRIMARY, f2_idx) */ f1').from('t3')
q.select('/*+ BKA(t1) NO_BKA(t2) */ *').from('t2')

(from examples in Optimizer Hints)

TehShrike avatar May 01 '25 14:05 TehShrike

It's possible, but depending on the surrounding code, it's easier to just find/replace the final string.

ArtskydJ avatar May 01 '25 15:05 ArtskydJ

Ah, I see. That makes sense. Maybe adding something like this

	selectComment: addToClause(clauses, `select`, comment),

after this line: https://github.com/TehShrike/sql-concat/blob/9272e7338c76c95e4d7cead1cd2c28778465c863/query-object.js#L13

where comment is a new clause handler something like this:

const comment = (...comments) => ({
	sql: '/* ' + comments.join(' */ /* ') + ' */',
	values: [],
	joinedBy: ' ',
})

?

TehShrike avatar May 01 '25 19:05 TehShrike

oh no, they're not normal comments, I see that important + now. So it would be selectOptimizerHint instead of selectComment.

TehShrike avatar May 01 '25 19:05 TehShrike

And the placement in the query is important. Right after SELECT and before any columns are listed.

ArtskydJ avatar May 01 '25 20:05 ArtskydJ