1

I have an oracle db on my Linux machine. A single sql query (1 connection) via bash is as follows:

su - oracle
sqlplus <dbuser>/<dbpass>
select * from cat;
exit

I'm trying to run parallel queries via bash, the following script is for running 10000 connections in parallel (Correct me if i'm wrong):

for i in $(seq 1 10000); do echo "select * from <tableName>;" | sqlplus <dbuser>/<dbpass>&done

I would like to make this code more robust and flexible, for the sake of example i want to add a sleep between each of the following command:

  • Create a connection
  • Create a table (Unique to this connection, i as index for example)
  • Select data from the table
  • Close the connection

The following code is my attempt of doing so: (Not working)

for i in $(seq 1 10000); 
do 
   echo "CREATE TABLE test+i (id NUMBER NOT NULL);" 
   sleep 2
   echo "select * from test+i"
   sleep 2 
   echo "DROP TABLE test+i" | sqlplus <dbuser>/<dbpass>& 
done

1) Syntactically, how should i write it?

2) How can i know how many queries/connections succeeded and how many failed?

3) How can i know how many connections actually ran in parallel

2 Answers 2

1

1) you can use ( and ) to group command into subshells, and send them background:

for i in $(seq 1 10000); 
do
   echo "CREATE TABLE test_$i (id NUMBER NOT NULL);
   !sleep 2
   select * from test_$i;
   !sleep 2
   DROP TABLE test_$i;" | sqlplus <dbuser>/<dbpass> &
done

2) you can set up error handling after each sqlplus call (examine output or exit value)

     echo "CREATE TABLE test_$i (id NUMBER NOT NULL);"  | sqlplus <dbuser>/<dbpass> 2>&1 | grep -i error

3) you can use the jobs command to examine how many job is running in the background:

> sleep 100 &
[1] 31642
> jobs
[1]+  Running                 sleep 100 &
Sign up to request clarification or add additional context in comments.

2 Comments

Those commands you wrote in 1, is doing the following: do action and disconnect, do another action and disconnect, do another and disconnect, i want the 3 actions to happen via 1 connection, connect -> do 3 queries -> disconnect
I modified the post, you can call out to shell from sqlplus using !
1

10000 jobs in parallel will often cause overflow. By setting 'WHENEVER SQLERROR EXIT SQL.SQLCODE' sqlplus will return an error, if the SQL fails. GNU Parallel can then re-run the query.

my.log will show if the query failed after rerunning 3 times.

doit() {
   i=$1
   (echo "WHENEVER SQLERROR EXIT SQL.SQLCODE CREATE TABLE test$i (id NUMBER NOT NULL);"
    sleep 2
    echo "WHENEVER SQLERROR EXIT SQL.SQLCODE select * from test$i;"
    sleep 2
    echo "WHENEVER SQLERROR EXIT SQL.SQLCODE DROP TABLE test$i;") |
     sqlplus <dbuser>/<dbpass>
}
export -f doit

seq 1 10000 | parallel --joblog my.log -j0 --retries 3 doit

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.