BCD Websmat



HOME    SUBSCRIBE

  Midrange Guru - OS/400 Edition

 

Editors: Ted Holt      Managing Editor: Mari Barrett
Howard Arner Technical Editor: David Morris

Topics Covered In Volume 1, Number 6:

With SQL, Sometimes Less Is More

Free Advice From Howard:

Most of the consulting work that I do involves helping companies with SQL access issues against their OS/400 legacy systems. Typically, these companies are doing Active Server Pages or Java Server Pages to display information from the system via the Web. This week, I want to share one of the most common performance problems that I see in my practice and how you can avoid sending me or another consultant money by proactively solving the problem.

The issue is something I call "lazy-fare" programming. When you program using record level access, you are used to opening a physical file or logical file and having all of the fields available to your program just by referencing them. Usually, when RPG programmers construct SQL statements, they tend to use the SQL * character to indicate to SQL to return all fields from the physical file. (A lot of SQL programmers are guilty of this shorthand, too, by the way.) So, the following statement might be found in an ASP or JSP page to list customers that match an inquiry screen:

SELECT * FROM CUSMAS WHERE CUSNAME LIKE 'ABC%'

Now, this is fine and dandy SQL. If there is a logical file associated with the physical file with the CUSNAME field as the first key, it will probably return records quite quickly. However, the number of records per second that a query can return is directly effected by the number of fields in the physical file. To wit: If CUSMAS has 10 fields, the query will run really fast, probably more than 2,000 records per second. But, if CUSMAS has 150 fields, the query will return noticeably slower, probably under 750 records per second.

<<Communications and memory>>

It is important to understand what is happening during the processing of the SQL statement on both the client and the server to understand why there is such a huge disparity in performance outlined in the example above. I'll tell you all about it after the advertisement below.

 

Reorg While Active

Any further explanation needed?

Reorg While Active does just what its name says, reducing downtime associated with reorganizing AS/400 files to just seconds. Now, you can reclaim lost disk space without taking users down.

Reorg While Active offers advanced functionality for improved AS/400 performance and user response times. Inexpensive and easy to use, Reorg While Active features fully automated installation and operations.

ITera, Inc. helps you live the promise of 24x7 computing. Visit us today at http://www.iterainc.com for more information, or simply call us at 800-957-4511 ext. 150.



OK. Let's get back to SQL programming. On the client side, whether you are using a Java program or a Visual Basic program with Active-X Data Objects (ADO), the following steps occur when you attempt to run the above query:

* Preparation
* Execution
* Fetch

In the preparation phase, the query is sent to the server and the server parses the SQL statement to determine what information (fields) the client wants and where the information comes from (physical file). The AS/400 query optimizer then looks at the physical file and its associated logical files to determine the best way to get the data that you want. Once this is done, the optimizer has what is called an execution plan. Having a plan on how to get the data, the AS/400 then gets information from the system catalogue about each field that your query is asking to return and sends this information (metadata) back to the client application. The metadata consists of the name of the field, the data type, length, etc. Once the client application gets the metadata, it constructs objects in memory to hold the returned query information.

At this point, the client can send the AS/400 an execution request. The execution request causes the AS/400 to look for the data that satisfies the query request by using the execution plan that it formulated. Once it finds records that satisfy the request, the AS/400 sends an acknowledgement message to the client that the execution request is complete.

Having received the notification, the client can then request that the AS/400 return one or more records from the open query using by issuing a fetch request. The AS/400 will acknowledge this request with one or more records from the open data path. (That one or more caveat depends on how the client issues the fetch request and the size of the fetch buffers. But that is a different column…) Now that you understand what is happening during the execution of an SQL query, lets examine why SELECT * is a bad idea.

<<Practical Applications>>

If you use SELECT * and a physical file contains only ten fields, then only 10 pieces of metadata are returned to the client application. Then, the client application only has to allocate 10 buffers to hold the returned fields. The AS/400 only has to read 10 fields of information per record and only has to transform 10 fields from internal representation to pretty client-side representation. Now, contrast this with a physical file that contains 150 fields of information. The AS/400 has to look up and return metadata on 150 fields. The client has to interpret 150 pieces of metadata to allocate objects or buffers to hold 150 pieces of information. The AS/400 has to read 150 fields and transform them from internal storage format to pretty client format. Also, since the number of fields is larger, the size of the records is larger, so fewer records fit into a transmission buffer causing more packets to be sent and acknowledged in transferring the information. Easy to understand, right?

Ok, so why is SELECT * bad? It's like my momma used to tell me, only put on your plate what you are going to eat. Most of the programs I see that have SELECT * do not require all of the fields in the physical file, just 5 or 10. Why squander your resources gathering data that you are not going to look at anyway? Name your fields and you increase the throughput of your information, thereby increasing the scalability of your system on both the client and server side.

If you don’t believe me, here are a few numbers from the real world. I had a client that was doing the above-mentioned customer listing web page. His query was a SELECT * going against a physical file with 179 columns of information. His program was only using 15 fields of information from the result set. Before he changed the query, the execution phase of the query took .4 seconds and the records per second clocked in at 250. After changing the query to only ask for the fields required, the execution phase went from .4 seconds to .1 seconds (less metadata and client memory allocation), and the records per second returned from the AS/400 went to over 1,200. Also, the time it took for JSP to render the page went from 2.2 seconds down to .9 seconds.

Remember, less is sometime more.

-- Howard 

 

Subscription And Advertising Information

Subscription Information

To unsubscribe, change your email address, or sign up for any of Guild Companies, Inc's free email newsletters, visit http://www.itjungle.com. Hit the SUBSCRIBE button on the homepage and it will lead you to our online subscription system.

When you sign up for one of our e-newsletters, you can be assured that your e-mail address will NEVER be sold to an outside company.

Advertising Information

Please contact Timothy Prickett Morgan at

Phone: 212 942 5818

Email: tpm@itjungle.com

Run CL Commands From Interactive SQL

Hey, Ted:

When I am using Interactive SQL/400 (i.e., STRSQL) and need to run a CL command, I have to exit SQL, run the command, and re-enter SQL. Is there any way to run system commands within an interactive SQL session?

Here's the deal. IBM has not provided a way to run CL commands from an SQL session, but you can create a stored procedure to run them. Here are two examples to get you started.

The first one creates a stored procedure, called EXCCMD, to run program QCMDEXC:

CREATE PROCEDURE mylib/exccmd
(IN cmd CHAR (32000), IN len DEC (15,5))
LANGUAGE CL
NOT DETERMINISTIC
NO SQL
EXTERNAL NAME QSYS/QCMDEXC
PARAMETER STYLE GENERAL

Once you have created this stored procedure, you will be able to run CL commands using the SQL CALL command:

call exccmd ('wrkoutq garbage',15)

Here's the SQL command to create a stored procedure called CMD, which brings up a pop-up window like the one you get when you press F21 from Screen Entry Utility (SEU):

CREATE PROCEDURE mylib/cmd
LANGUAGE CL
NOT DETERMINISTIC
NO SQL
EXTERNAL NAME QSYS/QUSCMDLN
PARAMETER STYLE GENERAL

To run CL commands, type the following command in your interactive SQL session:

call cmd

To resume entering SQL commands, press Enter with an empty command line, or press F12.

-- Ted  

 

WebSmart--Voted the 2001 Award Winner for BEST iSeries e-Business / e-Commerce tool in the marketplace.

Easiest, Fastest & most Affordable, portable PC based, iSeries400 Web & Wireless development tool available...and we'll prove it.

Use / customize the 70+ templates! Get a jump start on developing and deploying robust iSeries400 Web & Wireless apps: Inquiries, reporting, maintenance, wireless, ordering, shopping carts....

Have employees, vendors & clients access secured information via Browser based, Intranet, Extranet / Internet sites.

Automatically produces dynamic HTML CGI programs written in ILE/RPG.

WebSmart users include: Affinity Insurance, Airways Freight, Calvin Klein, CSC, Formica, Goodyear, Midwest Trophy, and many small IS shops....

Read these industry REVIEWS:

* http://www.400times.co.uk/Documents/Progen_WebSmart.htm

* http://www.bcdsoftware.com/websmartreview.htm

Ask for BCD's 15 point opinion email on how WebSmart is better than IBM's WebSphere product. Email us at sales@bcdsoftware.com.

Also try CATAPULT, voted best iSeries Automated Report Distribution - Email Tool.

See the difference for yourself.

Download and try WebSmart -or- CATAPULT now for FREE or get a FREE CD and you'll be more productive the very first day.

Call 630-986-0800 or go to http://www.BCDsoftware.com for more information.




 

 

ATTEND COMMON IN MINNEAPOLIS OCT 21-25

Join thousands and receive the iSeries IT education you need at the COMMON User Group's Fall 2001 Conference October 21-25 in Minneapolis. Complete information is available at http://www.common.org/Conferences/conf.html

Right now, read what a $995 registration gives you:

You'll choose from over 800 session hours that cover such vital topics as Web development, Java, VPN, WebSphere, Client Access, RPG IV, and Linux. Select the exact education you need and make yourself the problem solver in your organization.

Also, because we'll only be 1 1/2 hours from Rochester, Minnesota, you'll learn more than ever before about the iSeries. More IBMers will be in attendance, and this is a golden opportunity to share ideas with them.

In addition, you'll network with top iSeries professionals and exchange e-mail addresses. This connects you with the people who can help you accomplish more for your company.

To register online and/or become a member go to http://www.common.org/Conferences/confnew.html

Most important: Take action now. Register for the COMMON IT Education Conference, because there's no faster way to increase your value to your organization.




 


Reader Feedback And Insights

One of the great things about the OS/400 community is that it is indeed a community. We may be all working from our cubicles, but we are all connected and trying to figure out how to best employ the computer technology at our disposal. There are more than a few ways to skin any cat, and if you have a clever and unique answer to a problem that one of our Midrange Gurus has solved, we'd love to hear from you. This newsletter is an open dialog, and we value your input as well as your readership.

It goes without saying--but we'll say it anyway--that your hard technical questions pertaining to real world problems are equally valuable as a foundation for this newsletter as are your programming insights. We hope you find all the editions of Midrange Guru valuable, and we are going to work hard to make sure that they are.

Contact the Editors

If you have a tough problem, our gurus can probably help. Their mailboxes are always open.

* Email Ted Holt at tholt@itjungle.com

* Email Howard Arner at harner@itjungle.com

BCD Websmat

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved. This document may be redistributed freely and enthusiastically by email, but only in its unedited form. Thanks for your cooperation. Midrange Guru is a registered trademark of Guild Companies, Inc. IBM, AS/400, iSeries, OS/400, and eServer registered trademarks of International Business Machines Corp. All other product names are trademarked or copyrighted by their respective holders.