We help IT Professionals succeed at work.
Get Started

How to tune the following function to be faster?

140 Views
Last Modified: 2016-02-02
Hi,  I have a J2EE application in which one function is to retrieve all author names in the database and their corresponding number of strokes in  Chinese.  The application is to be run on JBoss application server 7.1.

I have designed the following SQL tables:

BIOGRAPHY (Storing real name and display name (which equals either real name or one of the pen name of the author) in Traditional Chinese)
AUTHOR_ID, REAL_NAME, DISPLAY_NAME, SHOW_REAL_NAME, IS_DELETED, <other fields>

BIO_PENNAME (Storing pen names of the author in Traditional Chinese)
AUTHOR_ID, PEN_NAME, DISPLAY_NAME, DISPLAY_IN_AUTHOR_LIST

UNICODE_CHAR_STROKE_MAP (Storing all the unicode characters and their corresponding number of strokes)
UNI_CHAR, STROKE_NO

Below please find our program that performs the retrieval of author name list:
	public List<AuthorName> getAuthorNameList(String lang) throws SQLException {
		List<AuthorName> nameList = new ArrayList<AuthorName>();
		
		PreparedStatement stmt = connection.prepareStatement("SELECT AUTHOR_ID, REAL_NAME AS NAME, DISPLAY_NAME, SHOW_REAL_NAME AS SHOW_NAME FROM PUBLISH_BIOGRAPHY WHERE IS_DELETED = 0 UNION "
				+ "SELECT N.AUTHOR_ID, N.PENNAME AS NAME, B.DISPLAY_NAME, N.DISPLAY_IN_AUTHOR_LIST AS SHOW_NAME FROM PUBLISH_BIO_PENNAME N, PUBLISH_BIOGRAPHY B "
				+ "WHERE N.AUTHOR_ID = B.AUTHOR_ID AND B.IS_DELETED = 0");
		ResultSet rs = stmt.executeQuery();
		
		while (rs.next()) {
			AuthorName authorName = new AuthorName();
			authorName.setAuthorID(rs.getString("AUTHOR_ID"));
			
			String name = rs.getString("NAME");
			String displayName = rs.getString("DISPLAY_NAME");
			boolean isShowName = rs.getBoolean("SHOW_NAME");
			int strokeNo = -1;
			
			if (AppConstants.LANG_SIMPLIFIED_CHINESE.equals(lang)) {
				name = StringUtil.convertToSimplified(name);
				displayName = StringUtil.convertToSimplified(displayName);
			}

/****  Getting Stroke number ******/
			PreparedStatement stmt2 = connection.prepareStatement("SELECT STROKE_NO FROM UNICODE_CHAR_STROKE_MAP WHERE UNI_CHAR = ?");
			stmt2.setString(1, name.substring(0, 1));
			ResultSet rs2 = stmt2.executeQuery();
			if (rs2.next())
				strokeNo = rs2.getInt("STROKE_NO");
			rs2.close();
			stmt2.close(); 
/******* End getting stroke number ******/

			authorName.setName(name);
			authorName.setDisplayName(displayName);
			authorName.setShowName(isShowName);
			authorName.setNoStroke(strokeNo);
			nameList.add(authorName);
		}
		rs.close();
		stmt.close();
		
		final Map<Integer, Integer> chineseStrokeMap = getUnicodeCharMap();
		
		Collections.sort(nameList, new Comparator<AuthorName>() {

			@Override
			public int compare(AuthorName n1, AuthorName n2) {
				// TODO Auto-generated method stub
				return StringUtil.compareString(n1.getName(), n2.getName(), chineseStrokeMap);
			}
			
		});
		
		return nameList;
	}

Open in new window

Since our application will need to display either Traditional Chinese or Simplified Chinese whenever the public user wants to click on  a button to change the language. If the user selects "Simplified Chinese", then the author names will be converted into Simplified Chinese using Java library and their corresponding stroke number will be returned.

I have tried to check the code and found that the bottleneck is the above code.  It takes around 4 second to return (around 3000 records) which is not acceptable.  However, if I commented out the section "Getting stroke number", results returns within 300ms, but it does not contain the stroke number information which is crucial.

May I know how can I modify this code to make the retrieval to return results within 1 second? Please note that we need the stroke number of the author names that are in corresponding language (Simplified / Traditional Chinese), which means that some characters may have different stroke number before and after converting the author names in Traditional Chinese to Simplified Chinese.

Our database is SQL Server 2008 R2 and using Java 6 (JBoss AS 7.1.1).

Grateful if someone can help.  Thank you.
Comment
Watch Question
Senior Database Architect
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE