sql

shohi 2025-06-12T19:27:18.031619Z

is it possible to use honeysql to generate query with use index (mysql index hints)? like

select * from tbl use index (my_index) where ...

seancorfield 2025-06-12T20:15:26.738079Z

Currently, only MS SQL hints are available -- see https://github.com/seancorfield/honeysql/issues/522 Feel free to create a new issue for MySQL referencing that old issue and I'll think about it. In general, USE INDEX can be very problematic since the query optimizer is often better than you 🙂 At work, we had a few USE INDEX hints in MySQL code -- and when we upgraded MySQL, we got worse performance until we removed those hints.

shohi 2025-06-12T20:26:37.699429Z

Thank you! recently we performed some optimization (de-fragmentation) on a large table and noticed that the query on it became slower. We found that the root cause was a change in the index chosen by query optimizer. As a workaround, we explicitly specified the index in the query.

seancorfield 2025-06-12T21:43:36.144249Z

@social820 I went ahead and implemented this: {:select :* :from [^{:use-index [:my-index]} :tbl] :where ...} -- try it out with the latest 2.7.9999-SNAPSHOT or git deps with SHA 3701da16325c7c09da282898e82f5121b27c73fb

shohi 2025-06-12T21:46:48.029779Z

Sure, thank you for the quick support @seancorfield!

shohi 2025-06-12T22:01:56.286489Z

java.lang.IllegalArgumentException: Metadata can only be applied to IMetas

seancorfield 2025-06-12T22:02:50.527029Z

Oh, sorry, I missed a bracket. The documentation has a correct example -- Clause Reference for FROM

seancorfield 2025-06-12T22:03:22.311889Z

{:select :* :from [^{:use-index [:my-index]} [:tbl]] :where ...} -- the docs even explain why you have to have those extra [ .. ] there 🙂

shohi 2025-06-12T22:08:33.677669Z

sorry i did't read the doc carefully, it's working now. do you have a plan to update helpers/form accordingly?

seancorfield 2025-06-12T22:10:44.091779Z

Not sure what you mean?

user=> (-> (h/select :*) (h/from ^{:use-index [:my-index]} [:tbl]) (h/where := :name "steve") (sql/format))
["SELECT * FROM tbl USE INDEX (my_index) WHERE name = ?" "steve"]

shohi 2025-06-12T22:13:56.925519Z

that's exactly what i mean, i'm not familiar with the implementation, i though you need to update the helpers as well. Thank you for your help 🙂

👍🏻 1
seancorfield 2025-06-12T22:16:02.580859Z

In general, the helpers are just thin wrappers around assoc or update / into so as long as core functions "work", nothing special needs to be done with helpers when the core DSL is updated.

👍 1