specify Index hint in HQL - sql

I need to specify a Index hint for my HQL
Query.setComment()
method is of no use as it is appending the hint before the select clause,
though this can be achieved by NativeSQL but I just wanted to check is there a way we can achieve this in HQL rather then SQL

As per My understanding there is no support for specifying index hints in HQL currently as of version 3.6.5
though this can be achieved by specifying #NamedNativeQueries or by calling CreateSQLQuery() on query interface

You can find a technique I use to specify Query Hints here, you can adapt this to insert some custom SQL inside some specific HQL or ICriteria query.
In my example I'm only interested in adding OPTION (RECOMPILE) or OPTION (HASH JOIN) but the technique comments<->interceptor can be used to manipulate the generate SQL in any way.

Related

How to add index hint option to aggregate query in PyMongo

Using PyMongo, is there a way to add an index hint to an aggregate query?
None of these option works with PyMongo:
db.collection.aggregate(pipeline, {'hint': 'index_name'})
db.collection.aggregate(pipeline).hint(index_name')
I don't believe this is supported.
Separately, hint specifies an index to use for the particular query; an aggregation pipeline can contain many queries. Simply specifying index name would be ambiguous.

Should we avoid IN clause in DB2 SQL while retrieving data?

Is it good practice to get all the data from DB2 table by using only select command not mentioning where clause (IN) ?
I was told to avoid IN clause where I am passing 10 String value in SQL.
Yes, you should avoid using an IN() clause whenever you can use a better alternative. (If you need to know whether some other element is actually better, you'll need to specify an environment, table definitions, data volumes, indexes, the statement being executed, etc. IOW, specify everything necessary for us to know an actual answer to an actual problem.)

How can I find the columns returned by a postgresql query without running the query itself?

Given a messy postgres query (e.g. with lots of subqueries) is there a way to figure out what columns will be returned by the query without running the query itself?
If I understand correctly, Sequel's Dataset#columns method (Documentation) calls the query with a LIMIT 1 attached. That's fine for a simple query, but if subqueries are involved it seems that this approach still results in computing those subqueries.
(One approach might be to add a LIMIT 1 to every subquery, but I'm not exactly sure how to go about doing that.)
I'm using Postgres 9.2 with Sequel.
Thanks! (I know this question isn't as precisely posed as might be desirable -- please let me know what more information I can provide that might be helpful.)
You can do this with explain and add the option VERBOSE. Have a look here
http://www.postgresql.org/docs/9.1/static/sql-explain.html

How to use in hibernate user-defined-function done is sql (and still have pagination)

I need to run a query that uses a function I created on the Oracle SQL(10g) database.
I've seen that Hibernate supports some aggregate functions (avg, sum, min, max and some forms of count) but I want my own function so this doesn't work.
I have also seen that I can use SQLQuery object for direct sql injection. But here I loose pagination and I really need this.
Anyone knows how to do this?
Thanks in advance!
For HQL queries, you need to create your own dialect based on the Oracle dialect you're currently using (Oracle10gDialect), and add your function to this dialect.
For Criteria queries, you may define your own Criterion implementation and use it in your query. This Criterion is responsible of the generation of the SQL expression. Look at the Hibernate sources to find an example of an existing criterion implementation similar to the one you want to create.

How to know what index is being used in SQL queries?

Having multiple indices for an SQL table, is there a way to know what index will be used automatically when using a specific query?
EDIT: I wanted the question to be general, but I mostly use MySQL, PostgreSQL and SQLite
Use the EXPLAIN PLAN statement to see what the dbms will do with your query.