How to make this sql processing faster?


My code below does this,

-it reads rows from a db table row by row
-every row that is reads it gets the user info and send data to the user info via a method sendText()

Now, my current code takes very long to run especially when there's lots of rows. My requests are,

-Suggestion on how to load my rows of data into memory then send one by one from mem
-Suggestion on improving sql processing of this code.

 * Classname: Description: Generic class to send
 * readings to fortune teller subscribers
 * Author: melwong Date started: 11-Mar-2004
 * Version: 1.0


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.dbutils.DbUtils;


public class FortuneTellerReadingsDelivery {
      private Connection conn = null;
      public FortuneTellerReadingsDelivery(Connection conn) {
            this.conn = conn;

      public void processDelivery (String message) {
            int[] fortuneReadings = null;
            int groupId = 0;
            int serviceId = 0;
            int subkeyword1Id = 0;
            String lineFeed = "";
            String serviceKeyword = "";
            String fortuneCookie = "";
            String readings = "";
            String phoneNumber = "";
            String sqlStr = "";
            //Connection conn = null;
            PreparedStatement statement = null;
            ResultSet rs = null;
            KeywordProcessor keyPro = null;
            OutboundResponse outRes = null;
            FortuneTellerUtils util = null;

            keyPro = new KeywordProcessor();
            outRes = new OutboundResponse();
            util = new FortuneTellerUtils();
            lineFeed = System.getProperty("line.separator");
            //To get the subscribers table
            serviceId = keyPro.getServiceId(message);
            subkeyword1Id = keyPro.getSubkeyword1Id(message, serviceId);
            groupId = keyPro.getGroupId(subkeyword1Id);
            serviceKeyword = keyPro.getServiceKeyword(groupId);
            //Get one fortune cookie for all subscribers
            fortuneCookie = util.getFortuneCookie();

            try {
                  //conn = DriverManager.getConnection(DbConfig.dbUrl);

                  sqlStr = "SELECT DISTINCT phoneNumber FROM "
                              + serviceKeyword + "_fortuneteller_users"
                              + " WHERE inactive = 0";
                  statement = conn
                  rs = statement.executeQuery();

                  while ( {
                        phoneNumber = rs.getString(1);
                        fortuneReadings = util.getFortuneReadings();
                        //fortuneCookie = util.getFortuneCookie();
                        readings = "Love:" + fortuneReadings[0] + "%" + lineFeed
                                          + "Money:" + fortuneReadings[1] + "%" + lineFeed
                                          + "Attitude:" + fortuneReadings[2] + "%";
                        //Melvin: 02/08/05
                        outRes.processText(groupId, "Your today's fortune cookie: " + fortuneCookie + lineFeed + readings, phoneNumber);
                        //outRes.processText(groupId, fortuneCookie + lineFeed + readings, phoneNumber);


            } catch (SQLException e) {
                  throw new FortuneTellerException(this.getClass().toString(), e);
            } finally{


      public static void main(String args[]) {

            String message = "";
            String absPath = "";
            PropertyFileReader readProp = null;
            DbConfig initDbConfig = null;
            FortuneTellerReadingsDelivery fortuneTellerReadingsDelivery = null;
            Connection conn = null;

            if (args.length != 0) {

                  absPath = args[0];

                  for (int i = 1; i < args.length; i++) {
                        message += args[i];
                        message += " ";
                  message = message.trim();

                  System.out.println("absPath=" + absPath);
                  System.out.println("message=" + message);

            } else {
                              .println("FortuneTellerReadingsDelivery: ERROR: Invalid command line arguments.");
                  System.err.println("\tFortuneTellerReadingsDelivery: Usage:");
                              .println("\tjava FortuneTellerReadingsDelivery <full webapp path> <full SMS message>");
                              .println("\tE.g: java FortuneTellerReadingsDelivery C:\\tomcat\\webapps\\jm HI FC");

            //Read properties must process first, then initialize db
            readProp = new PropertyFileReader(absPath);
            //To initialize the db stuffs
            initDbConfig = new DbConfig();

            //To get URL for WAP Push and app full path
            try {
                  conn = DriverManager.getConnection(DbConfig.dbUrl);
                  fortuneTellerReadingsDelivery = new FortuneTellerReadingsDelivery(conn);
            } catch (SQLException e) {
                  throw new FortuneTellerException("FortuneTellerReadingsDelivery", e);
            } finally {
            //fortuneTellerReadingsDelivery = new FortuneTellerReadingsDelivery();




---EOF CODE-----
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can start with:

a. bind the parameter to the PS

statement.setString(1, serviceKeyword);

b. make sure there's an index on phone number column

Does the below need to be called in the loop?

>>fortuneReadings = util.getFortuneReadings();
Mick BarryJava DeveloperCommented:
Don't use a PreparedSTatement, just use a standard one using createStatement()

And if your result set size is large then try adjusting the fetch size of the resukt set

mwhcAuthor Commented:

a. What is PS

>>Does the below need to be called in the loop?
>>fortuneReadings = util.getFortuneReadings();
Yes is does.

To objects,
How do I adjust the fetch size of the result set?
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

mwhcAuthor Commented:

What do you mean by "make sure there's an index on phone number column". This is my table structure,

CREATE TABLE `just_fortuneteller_users` (
  `userId` int(11) NOT NULL auto_increment,
  `phoneNumber` varchar(100) NOT NULL default '',
  `registrationTime` datetime NOT NULL default '0000-00-00 00:00:00',
  `totalMessage` int(11) NOT NULL default '0',
  `inactive` int(11) NOT NULL default '0',
  PRIMARY KEY  (`userId`)

Is this ok?

>>a. What is PS


>>What do you mean by "make sure there's an index on phone number column". This is my table structure,

try the following in your CREATE TABLE

phoneNumber varchar(100) NOT NULL, INDEX USING BTREE (phoneNumber)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mick BarryJava DeveloperCommented:
> How do I adjust the fetch size of the result set?

mwhcAuthor Commented:

I'm learning on indexing now. But I found out that indexing is useful especially in the WHERE statement. Will it be useful in my phoneNumber col since it's not in the WHERE statement?
Mick BarryJava DeveloperCommented:
doubt it, your better off indexing on inactive.
>>But I found out that indexing is useful especially in the WHERE statement.
>>your better off indexing on inactive.

Sorry mwhc, inactive is the correct column

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.