I'm trying to do pagination from the db and so far I have found that if you want to use limit you should use SQL_CALC_FOUND_ROWS.
However, in Java I'm having some issues understanding how to call this.
Can I do it in one prepareStatement or will I need to make multiple calls? If im making multiple calls, how does MySQL know I'm referring to the last call for the count?
Thanks.
You'll have to make two calls (unless you implement a some stored function in the db which returns both the limited resultset and the count, which I assume is possible in mysql but never tried that).
MySQL will know what query counts to return because both the query with the limit and the SELECT FOUND_ROWS(); will be in the same session.
Related
I execute the following queries:
SELECT 1; CREATE TABLE ....; SELECT 2;
after that I try to get all resultSets. I use the following code for this:
while(!stmt.getMoreResults()) {
stmt.getResultSet();
}
Unfortunately I get jus first result set. Please tell me what I'm doing wrong?
Your second CREATE TABLE statement technically does not even return a result set (though JDBC might return a count of the records affected by DML). So, if you want to capture the conceptual return value of each statement, then you should just run them separately.
If your second statement were actually a SELECT, then perhaps we could find some way to combine the queries together.
Read this canonical answer to see how to handle the case where you really do have mulitple selects. But, note that not all databases support this (e.g. Oracle does not support it). And read here to see why multiple queries in a single JDBC call might even be a bad thing.
Are you trying to say that Java is just not capable of doing the thing .Net does without a hitch? Is it as simple as that?
No matter what kind of dummy statements are between selects in the script that is run as batch the bool IDataReader.NextResult() in the C# code reliably returns next result jumping over the next dummy statements for Netezza we are trying to debug for now. It did the same thing for many years for all the platforms that support batch calls in case we had to deal with them.
I have a complex SQL statement that takes a long time to execute. This is going to be a problem as more users start using the system simultaneously.
Are there any options for sorting results in advance and then assigning them to Java POJO's using Hibernate? This way the processed information is already sitting in the MySQL DB waiting for retrieval without doing it upon execute...
I've looked into DB Views + Hibernate but didn't find much...
I think you should look at indexing. I dont think that ist possible prefetch results of sql queries. If query cannot be optimized and it is really REALLY important then you can maybe use some parallel implementation for processing query.
in according to my question above, I need to insert over 20000 thousands rows in one table of my database. In according to the performance I'm searching for a way to increase the efficiency of this process. My first Idea was to realize this with Java.Thread but i not quite sure if this is save enough. Has someone any good advices for me?
Edit: I already use preparedStatement.addBatch()
Looks safe to me, provided that every thread uses a different Connection object, and then disposes of the resources properly.
Anyway, note that the DB itself has a limit on concurrently running requests (max_connections in MySQL), so it doesn't help creating more threads than this number. Also, consider other optimizations such as batch inserts.
There is nothing wrong with accessing a database from different threads. As Eyal wrote, just make sure things like Connections only get used with a single thread and properly disposed.
The other question is, if this will actually help your performance. I'd make sure that you did everything else before resorting to multiple threads. Especially using batch statements seems to be the first option to look into if you haven't already.
If I understand you correctly, you need a way to write thousands of query's to your Database.
Do you know that you can execute a batch of MySQL queries?
There is a question about this already on Stackoverflow,
Java: Insert multiple rows into MySQL with PreparedStatement
you can use: PreparedStatement#addBatch() http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html#addBatch%28%29
and
PreparedStatement#executeBatch()
http://docs.oracle.com/javase/6/docs/api/java/sql/Statement.html#executeBatch%28%29
Check out #BalusC 's answer in the link above for more detail.
I am making a Java gui and web application which will use the same mysql database.
It's a DTh management system where all the information will be stored and retrieved dynamically depending on input.
I believe that views are static by nature and thus would be useless as all my queries will have a different where condition (userid).
Do I need to use triggers? I mean I could code the java to execute multiple statements instead of using a inbuilt trigger (e.g. Insert in customers name and family members name both will have a duplicate copy for head of the family). Is there a performance hit? Am I wrong in some way?
And same thing what is the use of stored procedures? Can't I use methods in java to do everything?
So, I am asking is it possible to shift all the calculation intensive stuff to java and web script instead of the sql. If yes, does this mean I only have to create the backend structure of Database(i.e. all the different tables and FK,PK) and do rest without using any sql stuff on mysql workbench?
Thank you for helping.
There is (as always) one correct answer: It depends.
If you only want to show and query some data, you probably won't need trigger or stored procedures.
Views are a different thing: They are pretty helpful if you want a static viesw to a join-table or something like that. If you don't need this, just don't use it.
Keys are really important. They make your data robust against wrong input.
What you shoud use is PrepearedStatement instead of Statement. If you only use PreparedStatements, you are (nearly ?) safe in the question of SQL-Injection.
We use Views because it just faster than select query and for just showing data (not edit-update) it is faster and preferable.
Trigger are fired at database side so it is faster because it just execute 2 or more queries in single execution.
Same in Stored procedures, because we can execute more than one queries in single database connection. If we execute different queries than it take more time on every execution for database connection (find database server, authenticate, find database,... etc.).
first of all, a preface: I'm writing a java class that creates temporary tables on a database using jdbc. I'm using JSE6 and Oracle 11XE as a test DB, but the class needs to be also DB2 compliant.
The temporary tables I'm trying to create come from a bigger one, and I do some filtering and aggregations on the data. The parameters I base my filtering on are decided by the user at runtime. One simplified example of what I'm trying to do is this:
CREATE TABLE temp_table AS (
SELECT
table1.department_id,
SUM(CASE WHEN table1.number_1 < &1 THEN table1.number_1 ELSE 0 END)) AS column1
FROM
table1
GROUP BY table1.department_id
)
My problem is that I need to specify parameters to filter the data, and I need to be sure they're properly escaped/localized/typed. This would be easy using a prepared statement, but I cannot use bind variables with DDL.
The temporary solution I resorted to is to alter the query String myself, writing the parameters in the correct place, but this means I now have to implement all the checks instead of relying on a PreparedStatement object to do it for me, on top of losing all the other benefits.
I investigated other solutions, but none of them convinced me so far:
I could first create an empty temp_table and then fill it with INSERT INTO temp_table(id, column1) (SELECT ...) but it seems I might incur in performance loss, so I'd like to stick to the CREATE temp_table AS
I thought about creating a temporary statement to hold the inner SELECT query, and have it generate a properly formatted/localized/etc. query string, but I haven't found any way to obtain the final query from it (and I read it's definitely not possible here). The only option I found for this case is to use DebuggableStatement, but I'm not sure I can include it in the project (also, it seems a quite inelegant way of solving my problem)
Another solution I'm thinking of, is to simply put the queries that create the temporary tables (for each of them I'd put the whole CREATE AS (SELECT...) on the database, inside a procedure, which I'll then be able to call using CallableStatement. this way I could avoid handling typization and still have good performances, at the price of a tighter coupling with the db (I'd have to be sure the procedures are there, or manage in java their addition/removal from the db)
So, my question is: are there better alternatives than the ones I could think of?
Is this supposed to be database agnostic, or are you targeting for only Oracle? You don't have to store PL/SQL in a stored procedure to use it; just build an anonymous PL/SQL block that does what you need, and execute it. The anonymous PL/SQL block can be built dynamically so that strongly typed variables are declared in the PL/SQL to hold your parameters, and then your java code sticks the values in. The type safety wouldn't be handled by Java since you're just building a string; it would be handled by Oracle when you execute the anonymous PL/SQL block.