Search and Inquiry of Website

9.6 Search and Inquiry of Website

Browsing your virtual shop, customers always want to find the goods they need easily. So the search and inquiry is very important for customers to make decision of purchasing goods. How to add simple search and inquiry function to the website will be discussed in this section.

9.6.1 Application of Advanced SQL

1. Grouping result function

To get the collective information, these functions aggregate many group data. When the data rows are gathered together, it can be viewed as the operation that incorporates the same kind of information when searching for information from the database. Some common grouping functions are listed in Table 9.3.

After understanding the common grouping functions, let’s review their application in the following part. In SQL*Plus, a large amount of queries of users are completed by the grouping functions when searching for data in the database.

Introduction to E-commerce

Therefore, mastering the functions is the foundation of understanding the function of SQL *PLUS.

Table 9.3 Common grouping functions

Function Return Example

Avg(column_name) The average of all values in the Select avg(sales) from customer column_name Table

Count(*) The number of rows in the table Select count(*) from customer Max(column_name) The maximum in the column_name Table Select max(sales) from customer Min(column_name) The minimum in the column_name Table Select min(sales) from customer

(1) Using group by clause The functions described in the above table can be followed by “group by”

clause or not. When not using the “group by” clause, for example “select max (sales) from customer;” it actually makes the database view all rows in the table as a group. For another example, to find out the average sale or sale amount, inquiry statement “select avg (sales) from customer;” returns the average sale, and statement “select sum (sales) from customer;” returns the sale amount. However sometimes what interested people is that it is needed to ensure the correct number of column of table in each statement using “group by” when inquiring pre-classified or grouped data.

When “group by” is used, and columns not used in the group by part appear in the select part, the grouping function is supposed to be used. Let’s reconsider the probable problems when omitting to use the grouping function. When “select last_name, state_cd, sum (sales) from customer group by last_name;” is executed, it will return the following error:

ERROR at line 1: ORA-00979: not a GROUP BY expression

This is due to not using “group by” clause in the column state_cd, therefore it is needed to add the grouping function. In other words, max( ), min( ), sum( ), count( ) or avg( ) is needed. If no appropriate grouping functions are found for the specified column, the column is moved to the “group by” clause.

(2) Using “having” clause Just like setting inquiry condition for single row inquiry (for example,

state_cd “MA”), “having” clause can be used to set inquiry condition for a set of records. For example, provided that we want to find out the states where numbers of customers exceed 300, using “having” clause, the inquiry state is “select state_cd, avg(sales) from customer group by state_cd having count (state_cd)>300;”. The “having” clause permits users to specify the search condition for a group of records, whereas the common “where” inquiry condition focuses on single record, instead of record group.

9 Portal Site Design of Virtual Enterprise

2. Embedded inquiry

Another powerful function of SQL is embedded inquiry, which is also called sub-inquiry. The format of sub-inquiry is shown as follows:

{main inquiry text} where {condition} ( {sub inquiry text});

For instance, the following main query accesses the customer table, and sub-query accesses the state table:

select last_name, sales from customer where state_cd=(select max(state_cd)from state)˗

Please pay attention to the following points: the sub-query is parenthesized, and the condition of “where” clause depends on the queried result. In other words, the “where” clause includes another SQL select statement. SQL statement running embedded sub-query will affect statement’s performance. As the experiences of using sub-query are continuously increasing, users will find out that it is needed to tie up with the database administrator to optimize the query statement including sub-query processing.

3. Defined variables in SQL*Plus

It permits to use defined variables in the same program in SQL*Plus, which can

be viewed as a column that owns only one column of data. Just as column, variables own type (number or character) and data. Remember that there can be one or more SQL query statements in a command file, and one variable which can be used in all SQL statements in the command file can be defined with define statement in SQL*Plus. Take the following codes as an example, and let’s see how to define a variable:

define rpt_cd="MA" select sales from customer where state_cd='&rpt_cd';

Although there is no mandatory provision, it is advisable to parenthesize the text assigned to the variable with double quotation marks, thus blank horizontal space can be embedded in the parenthesized text.

Provided that the variable “rpt_cd” has been assigned to a value “MA”, when writing a query statement, we can add prefix “&” in front of the variable, and parenthesize the variable by single quotation marks. Imputing command “define” can find out all defined variables. Suppose that three variables “rpt_cd”, “sales_amt” and “cust_start” have been defined; the following output is shown after command “define” executes:

DEFINE RPT_CD="MA"(CHAR) DEFINE SALES_AMT="18000"(CHAR) DEFINE CUST_START="A"(CHAR)

Introduction to E-commerce

Imputing variable name after “define” can find out the value of the variable. If Imputing “define sales_amt”, it will display that:

DEFINE SALES_AMT="18000"(CHAR)

We should know that all variables are character type data, no matter what kind of data type has been assigned to.

If it needs to clean the value of the variable for some reason, command “undefined” followed by the variable name can be imputed. If command “undefine sales_amt” is executed and then command “define sales_amt” is imputed, the following message will be displayed to show that the variable has been cleared.

SP2-0135: symbol sales_amt is UNDEFINED

Note that “define” and “undefined” can be shortened as “def” and “undef”.

9.6.2 Optimizing Database Inquiry

For a complicated inquiry statement, there are generally various expressions for the query of the same query condition, however different expressions make the response speed of the database different. Statistics shows that about 90% performances problem is caused by programmers’ or users’ input of the inappropriate inquiry statement. Therefore, improving the program quality of the SQL statement will definitely improve software performance. Nevertheless, the quality of inquiry statement is always related to database structure, quantity of records and other specific situations of the practical running system, and there is no simple and universal rule to summarize the optimizing of the inquiry statement. Understanding the basic operation rules of DBMS is helpful to optimize the inquiry statement, and then the using of the appropriate inquiry strategy will improve system performance.

The optimization of optimizer is based on the content of query table and other factors related to server, such as cache size, cache strategy, and I/O size. Generally speaking, access to hard disk is the operation that cost most. Therefore, for the users, the key to optimize the inquiry is to make the optimizer operate on the field index.

There are many executing strategies for SQL inquiry statement, and optimizer will estimate the least-time way of all the executing ways, which is also the least-cost way. Generally speaking, the most important selection is that which kind of index to use and which kind of methods to join the tables, and all the optimizations are based on the “where” clause in the inquiry statement used by users.

1. Classification of optimization

The optimizations of the “where” clause made by the optimizer are classified as

9 Portal Site Design of Virtual Enterprise

follows: (1) Search parameter The key of search parameter is whether the database can use the index of field

in the table to search for data, instead of directly searching for data in the record. The condition query with the operator such as , < , > , > , and < can directly use the index. The optimizer sometimes can convert the non-search parameter into search parameter. Therefore, some redundant search parameters should be provided in the query to make the optimizer have more choices.

(2) Link condition The optimizer enumerates all link methods when we use the linked inquiry,

and computes the cost of each method to select the least-cost one. The system generally uses the average density as the base to estimate the possible hitting rate when the link data cannot be acquired.

(3) “or” calculation condition When there are key words such as “in” in the query statement, the optimizer

converts the content to “or” juxtaposition condition. For example:

"select * from author where au_lname in ('Berry','Densham') " will be

converted to: "select * from author where au_lname = 'Berry' or au_lname ='Densham'"

DBMS will query each “or” clause, merge all results and delete the repeated items to get the final result.

2. Optimization skill

Based on understanding of database optimizer in the previous part, in order to ensure the exact optimization of the query statement be executed, we should pay attention to the following points:

(1) Avoid the using of the incompatible data type. For example, float and int, char and varchar, and binary and varbinary are incompatible. The incompatibility of data type might make the optimizer unable to perform some optimizing operations that can have been performed.

(2) If the value of an expression cannot be obtained in storage procedure or trigger when it is compiled, the optimizer will estimate the hitting record amount only with its average density.

(3) Avoid using other math operator for the search parameter. For example,

select name from employee where SUBSTRING(id, 1, 1) = 'B' select name from employee where salary * 12 > 30000

Written as:

select name from employee where id like 'B%' select name from employee where salary> 3000

Introduction to E-commerce

(4) Avoid using operator such as! or < >, because they make system only search data in tables directly, and unable to use index.

Above-mentioned points are some basic ideas to improve the query speed. However in most situations, the programmer needs to repeatedly compare different statements to get the optimal solution. In addition, what is more important is that the database administrator should adjust parameters of the DBMS in the server side to get faster response performance.

Dokumen yang terkait

AN ALIS IS YU RID IS PUT USAN BE B AS DAL AM P E RKAR A TIND AK P IDA NA P E NY E RTA AN M E L AK U K A N P R AK T IK K E DO K T E RA N YA NG M E N G A K IB ATK AN M ATINYA P AS IE N ( PUT USA N N O MOR: 9 0/PID.B /2011/ PN.MD O)

0 82 16

Anal isi s L e ve l Pe r tanyaan p ad a S oal Ce r ita d alam B u k u T e k s M at e m at ik a Pe n u n jang S MK Pr ogr a m Keahl ian T e k n ologi , Kese h at an , d an Pe r tani an Kelas X T e r b itan E r lan gga B e r d asarkan T ak s on om i S OL O

2 99 16

The Effectiveness of Computer-Assisted Language Learning in Teaching Past Tense to the Tenth Grade Students of SMAN 5 Tangerang Selatan

4 116 138

Modul TK E 2016 150 hlm edit Tina M imas

2 44 165

Membangun aplikasi e-commerce pada Toko Reafshop Bandung

1 26 687

Pengaruh Persepsi Kemudahan dan Kepuasan Wajib Pajak Terhadap Penggunaan E Filling (Survei Pada Wajib Pajak Orang Pribadi Di Kpp Pratama Soreang)

12 68 1

PENGARUH ARUS PENGELASAN TERHADAP KEKUATAN TARIK PADA PENGELASAN BIMETAL (STAINLESS STEEL A 240 Type 304 DAN CARBON STEEL A 516 Grade 70) DENGAN ELEKTRODA E 309-16

10 133 86

Factors Related to Somatosensory Amplification of Patients with Epigas- tric Pain

0 0 15

TEKNIK PERLAKUAN PENDAHULUAN DAN METODE PERKECAMBAHAN UNTUK MEMPERTAHANKAN VIABILITAS BENIH Acacia crassicarpa HASIL PEMULIAAN (Pretreatment Technique and Germination Method to Maintain the Viability of Acacia crassicarpa Improved Seed)

0 1 11

The Risk and Trust Factors in Relation to the Consumer Buying Decision Process Model

0 0 15