laravel-postgresql-enhanced icon indicating copy to clipboard operation
laravel-postgresql-enhanced copied to clipboard

Ability to add query hinting to make use of pg_hint_plan

Open aijorgenson opened this issue 1 year ago • 7 comments
trafficstars

It would be nice if there was a way to prefix queries with hints for extensions. For example, an officially recognized extension ( https://www.postgresql.org/about/news/pg_hint_plan-v160-released-2712/ ) pg_hint_plan which allows you to modify which index is utilized for a query.

https://github.com/ossc-db/pg_hint_plan

Syntax looks like so:

/*+ Something(my_table index_name) Something(my_other_table other_index_name) */
select * from my_table mt join my_other_table mot on mot.my_table_id = mt.id

aijorgenson avatar May 10 '24 14:05 aijorgenson

Thats a great idea 👍 Also had it on my list for some day.

Should it be one long string or more PHP like as an array?

Model::query()
  ->hintplan('Something(my_table index_name) Something(my_other_table other_index_name)')
  ->....

Model::query()
  ->hintplan(['Something(my_table index_name)', 'Something(my_other_table other_index_name)'])
  ->....

Hmm, hintplan as a method looks/sounds strange.

tpetry avatar May 10 '24 14:05 tpetry

@tpetry I'm relatively new to the PostgresSQL world, are there other extensions that could make use of a comment before the query like that?

Maybe ->queryPrefix() and then you can toss in whatever string you would like?

Model::query()
    ->queryPrefix('/*+ Something(my_table index_name) Something(my_other_table other_index_name) */')
    ->....

aijorgenson avatar May 10 '24 14:05 aijorgenson

Theres nothing else.

tpetry avatar May 10 '24 14:05 tpetry

->hint or ->hintPlan both sound great then! I'd imagine it'd be pretty simple to allow either a string or an array to be concatenated passed in?

aijorgenson avatar May 10 '24 15:05 aijorgenson

Sure. I could allowe both options.

tpetry avatar May 10 '24 15:05 tpetry

@tpetry I think we should take a look at https://github.com/spatie/laravel-sql-commenter for referrence and possible conflicts. I'm using this package to debug and analyze query.

dangnhdev avatar May 10 '24 22:05 dangnhdev

I don‘t see a conflict there. But I‘ll test it.

tpetry avatar May 11 '24 07:05 tpetry