3 ms·
> If the SQL engine decided not to use the right index for this query... tough, there's literally nothing you can do. MySQL Index Hints: https://dev.mysql.com/
by KronisLV 4y ago
> If the SQL engine decided not to use the right index for this query... tough, there's literally nothing you can do.
MySQL Index Hints: https://dev.mysql.com/doc/refman/8.0/en/index-hints.html https://dev.mysql.com/doc/refman/8.0/en/index-hints.html
MariaDB Index Hints: https://mariadb.com/kb/en/use-index/ https://mariadb.com/kb/en/use-index/
PostgreSQL: No. More information: https://stackoverflow.com/questions/309786/how-do-i-force-postgres-to-use-a-particular-index https://stackoverflow.com/questions/309786/how-do-i-force-po...
SQL Server INDEX hint: https://learn.microsoft.com/en-us/sql/t-sql/queries/hints-transact-sql-query?view=sql-server-ver16#g-use-index https://learn.microsoft.com/en-us/sql/t-sql/queries/hints-tr...
Oracle INDEX Hint: https://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements006.htm#SQLRF50405 https://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_e...
Of course, knowing exactly what is wrong and what hint you need to use (whether the issue is the index in particular or something else) is also a bit of work to figure out.
I recall working on a project that had Oracle as the RDBMS of choice and there was this one query that took approx. 45 minutes to execute, which wasn't acceptable. Merely a SELECT that JOINed a few tables and checked the data with some EXISTS constructions and such. If memory serves me right, looking through the AWR reports and adding a single NO_EXPAND hint made the query execute in not 45 minutes but around 3 seconds.
Of course, ORMs don't necessarily solve the aforementioned types of problems either, since it's still SQL statements being executed under the hood and figuring out how to add hints to those might be a bit more challenging. I've had cases where you need to drop down to native query level when using ORMs for particular queries, because there were no elegant ways of doing it otherwise (apart from creating a DB view and mapping against that in some cases, for read only access).
I think both using ORMs and using SQL directly suck, just in their own unique ways. That said, I wouldn't outright dismiss either: depending on what you're doing, one or the other is going to be a good enough tool for the job. You occasionally also see some pretty interesting projects in the ORM space, which is nice.
For example, jOOQ allows you to use a type safe fluent API for constructing your queries: https://www.jooq.org/ https://www.jooq.org/
Oh and MyBatis let's you generate your own SQL dynamically, which is an interesting approach: https://mybatis.org/mybatis-3/dynamic-sql.html https://mybatis.org/mybatis-3/dynamic-sql.html