How to make this sql processing faster?

Hi,

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.

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

package com.jm.fortuneteller;

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;

import com.jm.config.DbConfig;
import com.jm.config.FileConfig;
import com.jm.config.PropertyFileReader;
import com.jm.gateway.OutboundResponse;
import com.jm.util.KeywordProcessor;

public class FortuneTellerReadingsDelivery {
      
      private Connection conn = null;
      
      //Melvin:16/03/06
      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 = "";
            
            //Melvin:16/03/06
            //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);
            
            //Melvin:16/03/06
            //Get one fortune cookie for all subscribers
            fortuneCookie = util.getFortuneCookie();

            try {
                  //Melvin:16/03/06
                  //DbUtils.loadDriver(DbConfig.dbDriver);
                  //conn = DriverManager.getConnection(DbConfig.dbUrl);

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

                  while (rs.next()) {
                        phoneNumber = rs.getString(1);
                        
                        fortuneReadings = util.getFortuneReadings();
                        
                        //Melvin:16/03/06
                        //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{
                  DbUtils.closeQuietly(rs);
                  DbUtils.closeQuietly(statement);
                  
                  //Melvin:16/03/06
                  //DbUtils.closeQuietly(conn);
            }

      }

      
      public static void main(String args[]) {

            String message = "";
            String absPath = "";
            PropertyFileReader readProp = null;
            DbConfig initDbConfig = null;
            FortuneTellerReadingsDelivery fortuneTellerReadingsDelivery = null;
            
            //Melvin:16/03/06
            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 {
                  System.err
                              .println("FortuneTellerReadingsDelivery: ERROR: Invalid command line arguments.");
                  System.err.println("\tFortuneTellerReadingsDelivery: Usage:");
                  System.err
                              .println("\tjava FortuneTellerReadingsDelivery <full webapp path> <full SMS message>");
                  System.err
                              .println("\tE.g: java FortuneTellerReadingsDelivery C:\\tomcat\\webapps\\jm HI FC");
                  System.exit(1);
            }

            //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
            FileConfig.setSysConfig();
            
            //Melvin:16/03/06
            try {
                  DbUtils.loadDriver(DbConfig.dbDriver);
                  conn = DriverManager.getConnection(DbConfig.dbUrl);
                  fortuneTellerReadingsDelivery = new FortuneTellerReadingsDelivery(conn);
                  fortuneTellerReadingsDelivery.processDelivery(message);
            } catch (SQLException e) {
                  throw new FortuneTellerException("FortuneTellerReadingsDelivery", e);
            } finally {
                  DbUtils.closeQuietly(conn);
            }
            //fortuneTellerReadingsDelivery = new FortuneTellerReadingsDelivery();

            //fortuneTellerReadingsDelivery.processDelivery(message);

      }

}

---EOF CODE-----
mwhcAsked:
Who is Participating?

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

x
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.

CEHJCommented:
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:
Hi CEHJ,

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:
BTW,

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`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Is this ok?

Melv
CEHJCommented:
>>a. What is PS

PreparedStatement

>>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?

rs.setFetchSize(size);

http://java.sun.com/j2se/1.5.0/docs/api/java/sql/ResultSet.html#setFetchSize(int)
mwhcAuthor Commented:
Hi,

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.
CEHJCommented:
>>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
Java

From novice to tech pro — start learning today.