I want to execute multiple select statements in the same JDBC query, in order to amortize network latency over a number of related queries, as described in this question:
Multiple queries executed in java in single statement
The accepted answer is to use allowMultiQueries=true. Unfortunately, this is a feature specific to the MySQL JDBC driver.
What is the equivalent in PostgreSQL JDBC?
Related
I'm trying to query on tables which are in oracle and postgres. here I have used two getconnection methods but while I'm trying to do some join operations it is giving me an error. This error is because of using or querying on a single resultset which has particular(either postgres or oracle) database connection. can we pass two database connections in a single getConnection() method?
note :- Written in scala
JDBC essentially works by sending a query to a database server and presenting the response of the server as a ResultSet to the developer. In other words it does not execute the query but hands it off the database which executes it. Hence JDBC can not magically execute a single query that combines tables from different database servers.
Some databases support linking multiple servers together. In that case you would have to configure the databases to know about each other, and then connect to one of them and send it a query that references the linked server in the correct format (which is different for every vendor). However not all vendors support linked servers, and even less support linking to servers of other vendors.
Another option is using something like spark which has its own query engine. Spark can use JDBC to download the data from both servers to your machine or spark cluster and then execute the query locally.
I've a procedure in Oracle PL/SQL which fetches transactional data based on certain condition, then performs some logical calculations. I used cursor to store the SQL and then I used FETCH (cursor) BULK COLLECT INTO (table type variable) LIMIT 10000, iterated over this table variable to perform calculation and ultimately storing the value in a DB table. Once 10000 rows have been processed, query will be executed to fetch next set of records,
This helped me limiting number of times SQL is executed via cursor and limiting the number of records loaded into memory.
I am trying to migrate this code to plpgsql. How can I achieve this functionality in plpgsql?
You cannot achieve this functionality in PostgreSQL.
I wrote an extension https://github.com/okbob/dbms_sql . It can be used for reduce of necessary work related to migration from Oracle to Postgres.
But you don't need this feature in Postgres. Although PL/pgSQL is similar to PL/SQL, the architecture is very different - and bulk collect operations are not necessary.
I have a use case to distribute data across many databases on many servers, all in postgres tables.
From any given server/db, I may need to query another server/db.
The queries are quite basic, standard selects with where clauses on standard fields.
I have currently implemented postgres_FDW, (I'm, using postgres 9.5), but I think the queries are not using indexes on the remote db.
For this use case (a random node may query N other nodes), which is likely my best performance choice based on how each underlying engine actually executes?
The Postgres foreign data wrapper (postgres_FDW) is newer to
PostgreSQL so it tends to be the recommended method. While the
functionality in the dblink extension is similar to that in the
foreign data wrapper, the Postgres foreign data wrapper is more SQL
standard compliant and can provide improved performance over dblink
connections.
Read this article for more detailed info: Cross Database queryng
My solution was simple: I upgraded to Postgres 10, and it appears to push where clauses down to the remote server.
Can anyone suggest me what are the pros & cons of using dblink in Postgres?
I use dblink to connect to multiple database in my function in Postgres.
dblink is a great tool and it works very well.
The main cons are:
If you run a query between 2 servers not on the same network you will have a lot of latency and the performance will be very degraded
If you use dblink in a JOIN, in order to process this JOIN a lot of rows will have to be transferred from the remote server which will use bandwidth and degrade performance
If you have the possibility to use a single database for each query and not use multiple databases with dblink it will always be a better option.
Read also this interesting thread: http://www.postgresql-archive.org/dblink-performance-td5056145.html
I am new to jOOQ and consider replacing some JDBC code with jOOQ.
Looking at the jOOQ Java 8 streams is examples I start wondering if I can get a performance improvement by using jOOQ.
I have a PostgreSQL query with this characteristics:
Merge Join (cost=1.34..7649.90 rows=30407 width=333) (actual time=0.042..46.644 rows=28264 loops=1)
At the database the server the first row is returned after 0.042 ms while the last row is returned after 46.644 ms.
But my JDBC does not return the ResultSet until it is complete.
Is jOOQ (with Java 8 streams) able to start handling tuples as soon as the are ready or is jOOQ limited by JDBC?
jOOQ's Java 8 integration has two methods that might be interesting to you:
fetchStream(), which returns a Stream
fetchAsync(), which returns a CompletionStage
As of jOOQ 3.8, both APIs are limited by the blocking nature of the underlying JDBC API, i.e. both APIs internally iterate on ResultSet.next().
In particular, you can turn on using server side cursors by setting:
// JDBC
statement.setFetchSize(50);
// jOOQ, which delegates this call to JDBC
quest.fetchSize(50);
See also Statement.setFetchSize() or this question for more details.