![]() |
|||
![]()
|
![]() |
![]() Click Here! |
![]() |
Teach Yourself SQL in 21 Days, Second Edition- 15 -
|
Condition | Distinct Values |
calc_ytd = '-2109490.8' | 13,000 + |
dt_stmp = '01-SEP-96' | 15 |
output_cd = '001' | 13 |
activity_cd = 'IN' | 10 |
status_cd = 'A' | 4 |
function_cd = '060' | 6 |
NOTE: The most restrictive condition is also the condition with the most distinct values.
The next example places the most restrictive conditions first in the WHERE clause:
SQL> SET TIMING ON 2 SELECT COUNT(*) 3 FROM FACT_TABLE 4 WHERE CALC_YTD = '-2109490.8' 5 AND DT_STMP = '01-SEP-96' 6 AND OUTPUT_CD = '001' 7 AND ACTIVITY_CD = 'IN' 8 AND STATUS_CD = 'A' 9 AND FUNCTION_CD = '060';
COUNT(*) -------- 8 1 row selected. Elapsed: 00:00:15.37
This example places the most restrictive conditions last in the WHERE clause:
SQL> SET TIMING ON 2 SELECT COUNT(*) 3 FROM FACT_TABLE 4 WHERE FUNCTION_CD = '060' 5 AND STATUS_CD = 'A' 6 AND ACTIVITY_CD = 'IN' 7 AND OUTPUT_CD = '001' 8 AND DT_STMP = '01-SEP-96' 9 AND CALC_YTD = '-2109490.8'; COUNT(*) -------- 8 1 row selected. Elapsed: 00:00:01.80
Notice the difference in elapsed time. Simply changing the order of conditions according to the given table statistics, the second query ran almost 14 seconds faster than the first one. Imagine the difference on a poorly structured query that runs for three hours!
For queries that are executed on a regular basis, try to use procedures. A procedure is a potentially large group of SQL statements. (Refer to Day 13, "Advanced SQL Topics.")
Procedures are compiled by the database engine and then executed. Unlike an SQL statement, the database engine need not optimize the procedure before it is executed. Procedures, as opposed to numerous individual queries, may be easier for the user to maintain and more efficient for the database.
Avoid using the logical operator OR in a query if possible. OR inevitably slows down nearly any query against a table of substantial size. We find that IN is generally much quicker than OR. This advice certainly doesn't agree with documentation stating that optimizers convert IN arguments to OR conditions. Nevertheless, here is an example of a query using multiple ORs:
SQL> SELECT * 2 FROM FACT_TABLE 3 WHERE STATUS_CD = 'A' 4 OR STATUS_CD = 'B' 5 OR STATUS_CD = 'C' 6 OR STATUS_CD = 'D' 7 OR STATUS_CD = 'E' 8 OR STATUS_CD = 'F' 9 ORDER BY STATUS_CD;
Here is the same query using SUBSTR and IN:
SQL> SELECT * 2 FROM FACT_TABLE 3 WHERE STATUS_CD IN ('A','B','C','D','E','F') 4 ORDER BY STATUS_CD;
Try testing something similar for yourself. Although books are excellent sources for standards and direction, you will find it is often useful to come to your own conclusions on certain things, such as performance.
Here is another example using SUBSTR and IN. Notice that the first query combines LIKE with OR.
SQL> SELECT * 2 FROM FACT_TABLE 3 WHERE PROD_CD LIKE 'AB%' 4 OR PROD_CD LIKE 'AC%' 5 OR PROD_CD LIKE 'BB%' 6 OR PROD_CD LIKE 'BC%' 7 OR PROD_CD LIKE 'CC%' 8 ORDER BY PROD_CD;
SQL> SELECT * 2 FROM FACT_TABLE 3 WHERE SUBSTR(PROD_CD,1,2) IN ('AB','AC','BB','BC','CC') 4 ORDER BY PROD_CD;
The second example not only avoids the OR but also eliminates the combination of the OR and LIKE operators. You may want to try this example to see what the real-time performance difference is for your data.
When tuning a database, you must first determine what the database is being used for. An online analytical processing (OLAP) database is a system whose function is to provide query capabilities to the end user for statistical and general informational purposes. The data retrieved in this type of environment is often used for statistical reports that aid in the corporate decision-making process. These types of systems are also referred to as decision support systems (DSS). An online transactional processing (OLTP) database is a system whose main function is to provide an environment for end-user input and may also involve queries against day-to-day information. OLTP systems are used to manipulate information within the database on a daily basis. Data warehouses and DSSs get their data from online transactional databases and sometimes from other OLAP systems.
A transactional database is a delicate system that is heavily accessed in the form of transactions and queries against day-to-day information. However, an OLTP does not usually require a vast sort area, at least not to the extent to which it is required in an OLAP environment. Most OLTP transactions are quick and do not involve much sorting.
One of the biggest issues in a transactional database is rollback segments. The amount and size of rollback segments heavily depend on how many users are concurrently accessing the database, as well as the amount of work in each transaction. The best approach is to have several rollback segments in a transactional environment.
Another concern in a transactional environment is the integrity of the transaction logs, which are written to after each transaction. These logs exist for the sole purpose of recovery. Therefore, each SQL implementation needs a way to back up the logs for use in a "point in time recovery." SQL Server uses dump devices; Oracle uses a database mode known as ARCHIVELOG mode. Transaction logs also involve a performance consideration because backing up logs requires additional overhead.
Tuning OLAP systems, such as a data warehouse or decision support system, is much different from tuning a transaction database. Normally, more space is needed for sorting.
Because the purpose of this type of system is to retrieve useful decision-making data, you can expect many complex queries, which normally involve grouping and sorting of data. Compared to a transactional database, OLAP systems typically take more space for the sort area but less space for the rollback area.
Most transactions in an OLAP system take place as part of a batch process. Instead of having several rollback areas for user input, you may resort to one large rollback area for the loads, which can be taken offline during daily activity to reduce overhead.
A major factor in the performance of a database and SQL statements is the type of processing that takes place within a database. One type of processing is OLTP, discussed earlier today. When we talk about transactional processing, we are going to refer to two types: user input and batch loads.
Regular user input usually consists of SQL statements such as INSERT, UPDATE, and DELETE. These types of transactions are often performed by the end user, or the customer. End users are normally using a front-end application such as PowerBuilder to interface with the database, and therefore they seldom issue visible SQL statements. Nevertheless, the SQL code has already been generated for the user by the front-end application.
Your main focus when optimizing the performance of a database should be the end-user transactions. After all, "no customer" equates to "no database," which in turn means that you are out of a job. Always try to keep your customers happy, even though their expectations of system/database performance may sometimes be unreasonable. One consideration with end-user input is the number of concurrent users. The more concurrent database users you have, the greater the possibilities of performance degradation.
What is a batch load? A batch load performs heaps of transactions against the database at once. For example, suppose you are archiving last year's data into a massive history table. You may need to insert thousands, or even millions, of rows of data into your history table. You probably wouldn't want to do this task manually, so you are likely to create a batch job or script to automate the process. (Numerous techniques are available for loading data in a batch.) Batch loads are notorious for taxing system and database resources. These database resources may include table access, system catalog access, the database rollback segment, and sort area space; system resources may include available CPU and shared memory. Many other factors are involved, depending on your operating system and database server.
Both end-user transactions and batch loads are necessary for most databases to be successful, but your system could experience serious performance problems if these two types of processing lock horns. Therefore, you should know the difference between them and keep them segregated as much as possible. For example, you would not want to load massive amounts of data into the database when user activity is high. The database response may already be slow because of the number of concurrent users. Always try to run batch loads when user activity is at a minimum. Many shops reserve times in the evenings or early morning to load data in batch to avoid interfering with daily processing.
You should always plan the timing for massive batch loads, being careful to avoid scheduling them when the database is expected to be available for normal use. Figure 15.1 depicts heavy batch updates running concurrently with several user processes, all contending for system resources.
Figure 15.1.
System resource contention.
As you can see, many processes are contending for system resources. The heavy batch updates that are being done throw a monkey wrench into the equation. Instead of the system resources being dispersed somewhat evenly among the users, the batch updates appear to be hogging them. This situation is just the beginning of resource contention. As the batch transactions proceed, the user processes may eventually be forced out of the picture. This condition is not a good way of doing business. Even if the system has only one user, significant contention for that user could occur.
Another problem with batch processes is that the process may hold locks on a table that a user is trying to access. If there is a lock on a table, the user will be refused access until the lock is freed by the batch process, which could be hours. Batch processes should take place when system resources are at their best if possible. Don't make the users' transactions compete with batch. Nobody wins that game.
One way to expedite batch updates is by dropping indexes. Imagine the history table with many thousands of rows. That history table is also likely to have one or more indexes. When you think of an index, you normally think of faster table access, but in the case of batch loads, you can benefit by dropping the index(es).
When you load data into a table with an index, you can usually expect a great deal of index use, especially if you are updating a high percentage of rows in the table. Look at it this way. If you are studying a book and highlighting key points for future reference, you may find it quicker to browse through the book from beginning to end rather than using the index to locate your key points. (Using the index would be efficient if you were highlighting only a small portion of the book.)
To maximize the efficiency of batch loads/updates that affect a high percentage of rows in a table, you can take these three basic steps to disable an index:
When performing batch transactions, you must know how often to perform a "commit." As you learned on Day 11, "Controlling Transactions," a COMMIT statement finalizes a transaction. A COMMIT saves a transaction or writes any changes to the applicable table(s). Behind the scenes, however, much more is going on. Some areas in the database are reserved to store completed transactions before the changes are actually written to the target table. Oracle calls these areas rollback segments. When you issue a COMMIT statement, transactions associated with your SQL session in the rollback segment are updated in the target table. After the update takes place, the contents of the rollback segment are removed. A ROLLBACK command, on the other hand, clears the contents of the rollback segment without updating the target table.
As you can guess, if you never issue a COMMIT or ROLLBACK command, transactions keep building within the rollback segments. Subsequently, if the data you are loading is greater in size than the available space in the rollback segments, the database will essentially come to a halt and ban further transactional activity. Not issuing COMMIT commands is a common programming pitfall; regular COMMITs help to ensure stable performance of the entire database system.
The management of rollback segments is a complex and vital database administrator (DBA) responsibility because transactions dynamically affect the rollback segments, and in turn, affect the overall performance of the database as well as individual SQL statements. So when you are loading large amounts of data, be sure to issue the COMMIT command on a regular basis. Check with your DBA for advice on how often to commit during batch transactions. (See Figure 15.2.)
Figure 15.2.
The rollback area.
As you can see in Figure 15.2, when a user performs a transaction, the changes are retained in the rollback area.
The term dynamic database environment refers to a large database that is in a constant state of change. The changes that we are referring to are frequent batch updates and continual daily transactional processing. Dynamic databases usually entail heavy OLTP systems, but can also refer to DSSs or data warehouses, depending upon the volume and frequency of data loads.
The result of constant high-volume changes to a database is growth, which in turn yields fragmentation. Fragmentation can easily get out of hand if growth is not managed properly. Oracle allocates an initial extent to tables when they are created. When data is loaded and fills the table's initial extent, a next extent, which is also allocated when the table is created, is taken.
Sizing tables and indexes is essentially a DBA function and can drastically affect SQL statement performance. The first step in growth management is to be proactive. Allow room for tables to grow from day one, within reason. Also plan to defragment the database on a regular basis, even if doing so means developing a weekly routine. Here are the basic conceptual steps involved in defragmenting tables and indexes in a relational database management system:
WARNING: Never get rid of the backup of your table until you are sure that the new table was built successfully.
The following example demonstrates a practical use of a mailing list table in an Oracle database environment.
CREATE TABLE MAILING_TBL_BKUP AS SELECT * FROM MAILING_TBL;
Table Created.
drop table mailing_tbl; Table Dropped. CREATE TABLE MAILING_TBL ( INDIVIDUAL_ID VARCHAR2(12) NOT NULL, INDIVIDUAL_NAME VARCHAR2(30) NOT NULL, ADDRESS VARCHAR(40) NOT NULL, CITY VARCHAR(25) NOT NULL, STATE VARCHAR(2) NOT NULL, ZIP_CODE VARCHAR(9) NOT NULL, ) TABLESPACE TABLESPACE_NAME STORAGE ( INITIAL NEW_SIZE, NEXT NEW_SIZE ); Table created. INSERT INTO MAILING_TBL select * from mailing_tbl_bkup; 93,451 rows inserted. CREATE INDEX MAILING_IDX ON MAILING TABLE ( INDIVIDUAL_ID ) TABLESPACE TABLESPACE_NAME STORAGE ( INITIAL NEW_SIZE, NEXT NEW_SIZE ); Index Created. grant select on mailing_tbl to public; Grant Succeeded. drop table mailing_tbl_bkup; Table Dropped.
Rebuilding tables and indexes that have grown enables you to optimize storage, which improves overall performance. Remember to drop the backup table only after you have verified that the new table has been created successfully. Also keep in mind that you can achieve the same results with other methods. Check the options that are available to you in your database documentation.
Tuning a database is the process of fine-tuning the database server's performance. As a newcomer to SQL, you probably will not be exposed to database tuning unless you are a new DBA or a DBA moving into a relational database environment. Whether you will be managing a database or using SQL in applications or programming, you will benefit by knowing something about the database-tuning process. The key to the success of any database is for all parties to work together. Some general tips for tuning a database follow.
Figure 15.3 shows a simple example of how you might segregate the major areas of your database.
Figure 15.3.
Using available disks to enhance performance.
The scenario in Figure 15.3 uses four devices: disk01 through disk04. The objective when spreading your heavy database areas and objects is to keep areas of high use away from each another.
TIP: Also note how the application tables and indexes have been placed on each disk. Tables and indexes should be spread as much as possible.
Notice that in Figure 15.3 the tables and indexes are stored on different devices. You can also see how a "Big Table" or index may be striped across two or more devices. This technique splits the table into smaller segments that can be accessed simultaneously. Striping a table or index across multiple devices is a way to control fragmentation. In this scenario, tables may be read while their corresponding indexes are being referenced, which increases the speed of overall data access.
This example is really quite simple. Depending on the function, size, and system-related issues of your database, you may find a similar method for optimizing system resources that works better. In a perfect world where money is no obstacle, the best configuration is to have a separate disk for each major database entity, including large tables and indexes.
NOTE: The DBA and system administrator should work together to balance database space allocation and optimize the memory that is available on the server.
Tuning a database very much depends on the specific database system you are using. Obviously, tuning a database entails much more than just preparing queries and letting them fly. On the other hand, you won't get much reward for tuning a database when the application SQL is not fine-tuned itself. Professionals who tune databases for a living often specialize on one database product and learn as much as they possibly can about its features and idiosyncrasies. Although database tuning is often looked upon as a painful task, it can provide very lucrative employment for the people who truly understand it.
We have already mentioned some of the countless possible pitfalls that can hinder the general performance of a database. These are typically general bottlenecks that involve system-level maintenance, database maintenance, and management of SQL statement processing.
This section summarizes the most common obstacles in system performance and database response time.
TIP: You can use various methods to optimize the structure of an SQL statement, depending upon the steps taken by the database server during SQL statement processing.
Check with your DBA or database vendor to determine what tools are available to you for performance measuring and tuning. You can use performance-tuning tools to identify deficiencies in the data access path; in addition, these tools can sometimes suggest changes to improve the performance of a particular SQL statement.
Oracle has two popular tools for managing SQL statement performance. These tools are explain plan and tkprof. The explain plan tool identifies the access path that will be taken when the SQL statement is executed. tkprof measures the performance by time elapsed during each phase of SQL statement processing. Oracle Corporation also provides other tools that help with SQL statement and database analysis, but the two mentioned here are the most popular. If you want to simply measure the elapsed time of a query in Oracle, you can use the SQL*Plus command SET TIMING ON.
SET TIMING ON and other SET commands are covered in more depth on Day 20, "SQL*Plus."
Sybase's SQL Server has diagnostic tools for SQL statements. These options are in the form of SET commands that you can add to your SQL statements. (These commands are similar to Oracle's SET commands). Some common commands are SET SHOWPLAN ON, SET STATISTIC IO ON, and SET STATISTICS TIME ON. These SET commands display output concerning the steps performed in a query, the number of reads and writes required to perform the query, and general statement-parsing information. SQL Server SET commands are covered on Day 19, "Transact-SQL: An Introduction."
Two major elements of streamlining, or tuning, directly affect the performance of SQL statements: application tuning and database tuning. Each has its own role, but one cannot be optimally tuned without the other. The first step toward success is for the technical team and system engineers to work together to balance resources and take full advantage of the database features that aid in improving performance. Many of these features are built into the database software provided by the vendor.
Application developers must know the data. The key to an optimal database design is thorough knowledge of the application's data. Developers and production programmers must know when to use indexes, when to add another index, and when to allow batch jobs to run. Always plan batch loads and keep batch processing separate from daily transactional processing.
Databases can be tuned to improve the performance of individual applications that access them. Database administrators must be concerned with the daily operation and performance of the database. In addition to the meticulous tuning that occurs behind the scenes, the DBA can usually offer creative suggestions for accessing data more efficiently, such as manipulating indexes or reconstructing an SQL statement. The DBA should also be familiar with the tools that are readily available with the database software to measure performance and provide suggestions for statement tweaking.
The Workshop provides quiz questions to help solidify your understanding of the material covered, as well as exercises to provide you with experience in using what you have learned. Try to answer the quiz and exercise questions before checking the answers in Appendix F, "Answers to Quizzes and Exercises."
SELECT EMPLOYEE.LAST_NAME, EMPLOYEE.FIRST_NAME, EMPLOYEE.MIDDLE_NAME, EMPLOYEE.ADDRESS, EMPLOYEE.PHONE_NUMBER, PAYROLL.SALARY, PAYROLL.POSITION, EMPLOYEE.SSN, PAYROLL.START_DATE FROM EMPLOYEE, PAYROLL WHERE EMPLOYEE.SSN = PAYROLL.SSN AND EMPLOYEE.LAST_NAME LIKE 'S%' AND PAYROLL.SALARY > 20000;
593 individuals have the last name SMITH.
712 individuals live in INDIANAPOLIS.
3,492 individuals are MALE.
1,233 individuals earn a salary >= 30,000.
5,009 individuals are single.
SELECT M.INDIVIDUAL_NAME, M.ADDRESS, M.CITY, M.STATE, M.ZIP_CODE, S.SEX, S.MARITAL_STATUS, S.SALARY FROM MAILING_TBL M, INDIVIDUAL_STAT_TBL S WHERE M.NAME LIKE 'SMITH%' AND M.CITY = 'INDIANAPOLIS' AND S.SEX = 'MALE' AND S.SALARY >= 30000 AND S.MARITAL_STATUS = 'S' AND M.INDIVIDUAL_ID = S.INDIVIDUAL_ID; --------------
© Copyright, Macmillan Computer Publishing. All rights reserved.
![]() |
|
Use of this site is subject certain Terms & Conditions. Copyright (c) 1996-1999 EarthWeb, Inc.. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Please read our privacy policy for details. |