JPA Entity use index hint - sql-server

Is it possible to specify a database index hint on a play framework Entity query.
My code looks like:
public static List<Transaction> findOnInactive(Date date) {
return Transaction.find(
"date = ? and account in ( select d.acctNb from account d "
+ " where d.date = ? and (d.inactive = true or d.blocked = true)"
+ " group by d.acctNb )", date, date).fetch();
}
Running the generated query takes 20 sec. However running the same query manually with
select * from transaction with (INDEX(_dta_index_k1_1)) ...
only take 1 sec. Anyway I could specify the index hint in my JPA query?

You need to use native SQL query, something like this:
return JPA.em().createNativeQuery(
"select * from transaction with (INDEX(_dta_index_k1_1)) ...",
Transaction.class).getResultList();

Related

Pagination with EclipseLink, SQL Server and spring data

I am currently trying to find out what the best pagination option is with EclipseLink and SQL Server.
I saw that for SQL Server there is no special support for pagination with EclipseLink. If I have the following data model, findAll creates a set of select (n + 1) for each subentity. That doesn't look efficient at first.
public class Foo {
#OneToMany( fetch = FetchType.LAZY )
private Set<Bar> bars = Sets.newHashSet();
}
public class Bar {
#OneToMany( fetch = FetchType.LAZY )
private Set<FooBar> fooBars = Sets.newHashSet();
}
public class FooBar {
String name;
}
So for every bar and fooBar there is a separate select.
Now I would like to discuss a few solutions. My current suggestions are. If a good possibility has been found, I can see how it is best implemented in spring data.
Chunking using Ids with IN
Query query = em.createQuery("SELECT f.id FROM Foo f ORDER BY f.id OFFSET 10 ROWS FETCH NEXT
10 ROWS ONLY");
List<Integer> ids = query.getResultList();
Query query = em.createQuery("SELECT DISTINCT f FROM foo f"
+ " LEFT JOIN FETCH f.bar bars "
+ " LEFT JOIN FETCH bar.fooBars foobars "
+ " WHERE t.id in (:ids) " )
query.setParameter("ids", ids.subList(5, 10));
List<Foo> foos = query.getResultList();
Using IN statement for every subselect (like hibernate) instead of single select
Using Batch with QueryHints IN or JOIN?
https://www.eclipse.org/eclipselink/documentation/2.5/jpa/extensions/q_batch.htm
Another possibility?

FLINK- how to process logic on sql query result

My requirement is to process or build some logic around the result of sql query in flink. For simplicity lets say I have two sql query they are running on different window size and one event stream. My question is
a) how I will know for which query result is this
b) how I will know how many rows are the result of executed query? I need this info as I have to build a notification message with list of event those are part of the query result.
DataStream<Event> ds = ...
String query = "select id, key" +
" from eventTable GROUP BY TUMBLE(rowTime, INTERVAL '10' SECOND), id, key ";
String query1 = "select id, key" +
" from eventTable GROUP BY TUMBLE(rowTime, INTERVAL '1' DAY), id, key ";
List<String> list = new ArrayList<>();
list.add(query);
list.add(query1);
tabEnv.createTemporaryView("eventTable", ds, $("id"), $("timeLong"), $("key"),$("rowTime").rowtime());
for(int i =0; i< list.size(); i++ ){
Table result = tabEnv.sqlQuery(list.get(i));
DataStream<Tuple2<Boolean, Row>> dsRow = tabEnv.toRetractStream(result, Row.class);
dsRow.process(new ProcessFunction<Tuple2<Boolean, Row>, Object>() {
List<Row> listRow = new ArrayList<>();
#Override
public void processElement(Tuple2<Boolean, Row> booleanRowTuple2, Context context, Collector<Object> collector) throws Exception {
listRow.add(booleanRowTuple2.f1);
}
});
}
Appreciate your help. thanks Ashutosh
To sort out which results are from which query, you could include an identifier for each query in the queries themselves, e.g.,
SELECT '10sec', id, key FROM eventTable GROUP BY TUMBLE(rowTime, INTERVAL '10' SECOND), id, key
Determining the number of rows in the result table is trickier. One issue is that there is no final answer to the number of results from a streaming query. But where you are processing the results, it seems like you could count the number of rows.
Or, and I haven't tried this, but maybe you could use something like row_number() over(order by tumble_rowtime(rowTime, interval '10' second)) to annotate each row of the result with a counter.

Can Any one help me to convert this sql query into linq

I need to convert this SQL Query to Link :
"Select * FROM [Register]
where RegisterId IN (SELECT MyId
FROM Friends
WHERE FriendId='" + Session["CurrentProfileId"] + "'
AND Status=1
UNION
SELECT FriendId
FROM Friends
WHERE MyId='" + Session["CurrentProfileId"] + "'
AND Status=1) ";
It may be look like this??? but this is incorrect and having errors
(from u in db.Register
where RegisterId).Contains
(from f in db.Freinds
where f.MyId == Id && m.Status == 1
select new { m.MyId })
.Union(from m in db.Freinds
where f.FreindId == Id && m.Status == 1
select new { m.CreateDate } ));
You have a few problems with the linq above and here are a few:
In the query in the Union you select the CreateDate whereas in the top on you select the MyId. I assume you meant to select FreindId.
In these 2 queries you create an anonymous class instance with the field but then compare it to the RegisterId which is probably a guid/string/int - but for sure not of the type you just created.
You are using the Contains method wrong. Linq syntax can be similar to sql but it is not the same. Check here for Contains
The correct Linq way of doing it is:
var idsCollection = ((from f in db.Freinds
where f.StatusId == 1 && f.MyId == Id
select f.MyId)
.Union(from m in db.Friends
where m.StatusId == 1 && f.FreindId == Id
select m.FriendId)).ToList();
var result = (from u in db.Register
where idsCollection.Contains(u.RegisterId)
select u).ToList();
Notice that the .ToList() is not a must and is here just to ease in debugging. For more information about this .ToList() and Linq in general check MSDN

Get random result from JPQL query over large table

I'm currently using JPQL queries to retrieve information from a database. The purpose of the project is testing a sample environments through randomness with different elements so I need the queries to retrieve random single results all through the project.
I am facing that JPQL does not implement a proper function for random retrieval and postcalculation of random takes too long (14 seconds for the attached function to return a random result)
public Player getRandomActivePlayerWithTransactions(){
List<Player> randomPlayers = entityManager.createQuery("SELECT pw.playerId FROM PlayerWallet pw JOIN pw.playerId p"
+ " JOIN p.gameAccountCollection ga JOIN ga.iDAccountStatus acs"
+ " WHERE (SELECT count(col.playerWalletTransactionId) FROM pw.playerWalletTransactionCollection col) > 0 AND acs.code = :status")
.setParameter("status", "ACTIVATED")
.getResultList();
return randomPlayers.get(random.nextInt(randomPlayers.size()));
}
As ORDER BY NEWID() is not allowed because of JPQL restrictions I have tested the following inline conditions, all of them returned with syntax error on compilation.
WHERE (ABS(CAST((BINARY_CHECKSUM(*) * RAND()) as int)) % 100) < 10
WHERE Rnd % 100 < 10
FROM TABLESAMPLE(10 PERCENT)
Have you consider to generate a random number and skip to that result?
I mean something like this:
String q = "SELECT COUNT(*) FROM Player p";
Query query=entityManager.createQuery(q);
Number countResult=(Number) query.getSingleResult();
int random = Math.random()*countResult.intValue();
List<Player> randomPlayers = entityManager.createQuery("SELECT pw.playerId FROM PlayerWallet pw JOIN pw.playerId p"
+ " JOIN p.gameAccountCollection ga JOIN ga.iDAccountStatus acs"
+ " WHERE (SELECT count(col.playerWalletTransactionId) FROM pw.playerWalletTransactionCollection col) > 0 AND acs.code = :status")
.setParameter("status", "ACTIVATED")
.setFirstResult(random)
.setMaxResults(1)
.getSingleResult();
I have figured it out. When retrieving the player I was also retrieving other unused related entity and all the entities related with that one and so one.
After adding fetch=FetchType.LAZY (don't fetch entity until required) to the problematic relation the performance of the query has increased dramatically.

Hibernate SQLQuery, how to get arrays and rows objects?

Hibernate 3.6 & postgresql 9.1.
Using SQLQuery how to get result array data (array of Long - assistants, array of rows of "Text, Long, Timestamp" - accounts)?
limit = 10000;
final SQLQuery sqlQuery = getSession().createSQLQuery("SELECT id, name, ts, " +
" array(SELECT assistant_id FROM user_assistant WHERE p_id=pr.id ORDER BY assistant_id) AS accounts," +
" array(SELECT row(type,uid,ts) FROM user_account WHERE p_id=pr.id ORDER BY type) AS accs," +
" FROM profile pr WHERE ts > ? ORDER BY ts LIMIT " + limit);
The most of DAO functions written with hibernate Entities & annotations.
But for a few statistics tasks easier to work with HQL or even SQL.
As opposed to pure JDBC in hibernateSQL working with arrays is not so intuitive.
JDBC could be a solution, but I haven't found any way to get JDBC Statement from Hibernate Session or Connection.
ResultTransformer doesn't help also, fails with:
org.hibernate.MappingException: No Dialect mapping for JDBC type: 2003
By referring this
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querysql.html
What you can do something like;
session.createSQLQuery("Your custom query")
.addScalar("field1", Hibernate.STRING)
.addScalar("field2", Hibernate.STRING)
.addScalar("field3", Hibernate.STRING)
and then
for(Object rows : query.list()){
Object[] row = (Object[]) rows;
String field1 = row[0] // contains field1
String field2 = row[1]
..
..
}

Resources