QUERY_LOG_SUMMARY
Obtain a report of logged queries.
QUERY_LOG_SUMMARY enables you to analyze queries you have logged. For example, suppose you have an application that searches a database of large animals, and your analysis of queries against it shows that users are continually searching for the word mouse; this analysis might induce you to rewrite your application so that a search for mouse redirects the user to a database for small animals instead of simply returning an unsuccessful search.
With query analysis, you can find out the following:
-
Which queries were made
-
Which queries were successful
-
Which queries were unsuccessful
-
How many times each query was made
You can combine these factors in various ways, such as determining the 50 most frequent unsuccessful queries made by your application.
Query logging is begun with CTX_OUTPUT.START_QUERY_LOG and terminated with CTX_OUTPUT.END_QUERY_LOG.
Note: You must connect as CTXSYS to use CTX_REPORT.QUERY_LOG_SUMMARY.
See Also: “START_QUERY_LOG” and “END_QUERY_LOG”
Syntax
procedure CTX_REPORT.QUERY_LOG_SUMMARY(
logfile IN VARCHAR2,
indexname IN VARCHAR2 DEFAULT NULL,
result_table IN OUT NOCOPY QUERY_TABLE,
row_num IN NUMBER,
most_freq IN BOOLEAN DEFAULT TRUE,
has_hit IN BOOLEAN DEFAULT TRUE
);
logfile
Specify the name of the logfile that contains the queries. Starting with Oracle Database 12c release 2 (12.2), this parameter is ignored as all the query logs are written to database trace files.
indexname
Specify the name of the context index for which you want the summary report. If you specify NULL, the procedure provides a summary report for all context indexes.
result_table
Specify the name of the in-memory table of type TABLE OF RECORD where the results of the QUERY_LOG_SUMMARY are to go. The default is the location specified by the system parameter LOG_DIRECTORY.
row_num
The number of rows of results from QUERY_LOG_SUMMARY to be reported into the table named by restab. For example, if this is number is 10, most_freq is TRUE, and has_hit is TRUE, then the procedure returns the 10 most frequent queries that were successful (that is, returned hits).
most_freq
Specify whether QUERY_LOG_SUMMARY should return the most frequent or least frequent queries. The default is most frequent queries. If most_freq is set to FALSE, the procedure returns the least successful queries.
has_hit
Specify whether QUERY_LOG_SUMMARY should return queries that are successful (that is, that generate hits) or unsuccessful queries. The default is to count successful queries; set has_hit to FALSE to return unsuccessful queries.
Example
The following example shows how a query log can be used.
First connect as CTXSYS. Then create and populate two tables, and then create an index for each:
create table qlogtab1 (tk number primary key, text varchar2(2000));
insert into qlogtab1 values(1, 'The Roman name for France was Gaul.');
insert into qlogtab1 values(2, 'The Tour de France is held each summer.');
insert into qlogtab1 values(3, 'Jacques Anatole Thibault took the pen name Anatole France.');
create index idx_qlog1 on qlogtab1(text) indextype is ctxsys.context;
create table qlogtab2 (tk number primary key, text varchar2(2000));
insert into qlogtab2 values(1, 'The Great Wall of China is about 2400 kilometers long');
insert into qlogtab2 values(2, 'Soccer dates back at least to 217 C.E.');
insert into qlogtab2 values(3, 'The Corn Palace is a tourist attraction in South Dakota.');
create index idx_qlog2 on qlogtab2(text) indextype is ctxsys.context;
Turn on query logging, creating a log called query_log:
exec ctx_output.start_query_log('query.log');
Now make some queries (some of which will be unsuccessful):
select text from qlogtab1 where contains(text, 'France',1)>0;
select text from qlogtab1 where contains(text, 'cheese',1)>0;
select text from qlogtab1 where contains(text, 'Text Wizard',1)>0;
select text from qlogtab2 where contains(text, 'Corn Palace',1)>0;
select text from qlogtab2 where contains(text, 'China',1)>0;
select text from qlogtab1 where contains(text, 'Text Wizards',1)>0;
select text from qlogtab2 where contains(text, 'South Dakota',1)>0;
select text from qlogtab1 where contains(text, 'Text Wizard',1)>0;
select text from qlogtab2 where contains(text, 'China',1)>0;
select text from qlogtab1 where contains(text, 'Text Wizard',1)>0;
select text from qlogtab2 where contains(text, 'company',1)>0;
select text from qlogtab1 where contains(text, 'Text Wizard',1)>0;
select text from qlogtab1 where contains(text, 'France',1)>0;
select text from qlogtab1 where contains(text, 'database',1)>0;
select text from qlogtab2 where contains(text, 'high-tech',1)>0;
select text from qlogtab1 where contains(text, 'database',1)>0;
select text from qlogtab1 where contains(text, 'France',1)>0;
select text from qlogtab1 where contains(text, 'Japan',1)>0;
select text from qlogtab1 where contains(text, 'Egypt',1)>0;
select text from qlogtab1 where contains(text, 'Argentina',1)>0;
select text from qlogtab1 where contains(text, 'Argentina',1)>0;
select text from qlogtab1 where contains(text, 'Argentina',1)>0;
select text from qlogtab1 where contains(text, 'Japan',1)>0;
select text from qlogtab1 where contains(text, 'Egypt',1)>0;
select text from qlogtab1 where contains(text, 'Air Shuttle',1)>0;
select text from qlogtab1 where contains(text, 'Argentina',1)>0;
With the querying over, turn query logging off:
exec ctx_output.end_query_log;
Use QUERY_LOG_SUMMARY to get query reports. In the first instance, you ask to see the three most frequent queries that return successfully. First declare the results table (the_queries).
set serveroutput on;
declare
the_queries ctx_report.query_table;
begin
ctx_report.query_log_summary('query.log', null, the_queries,
row_num=>3, most_freq=>TRUE, has_hit=>TRUE);
dbms_output.put_line('The 3 most frequent queries returning hits');
dbms_output.put_line('number of times query string');
for i in 1..the_queries.count loop
dbms_output.put_line(the_queries(i).times||' '||the_queries(i).query);
end loop;
end;
/
This returns the following:
The 3 most frequent queries returning hits
number of times query string
3 France
2 China
1 Corn Palace
Next, look for the three most frequent queries on idx_qlog1 that were successful.
declare
the_queries ctx_report.query_table;
begin
ctx_report.query_log_summary('query.log', 'idx_qlog1', the_queries,
row_num=>3, most_freq=>TRUE, has_hit=>TRUE);
dbms_output.put_line('The 3 most frequent queries returning hits for index idx_qlog1');
dbms_output.put_line('number of times query string');
for i in 1..the_queries.count loop
dbms_output.put_line(the_queries(i).times||' '||the_queries(i).query);
end loop;
end;
/
Because only the queries for France were successful, ctx_report.query_log_summary returns the following:
The 3 most frequent queries returning hits for index idx_qlog1
number of times query string
3 France
Lastly, ask to see the three least frequent queries that returned no hits (that is, queries that were unsuccessful and called infrequently). In this case, you are interested in queries on both context indexes, so you set the indexname parameter to NULL.
declare
the_queries ctx_report.query_table;
begin
ctx_report.query_log_summary('query.log', null, the_queries, row_num=>3,
most_freq=>FALSE, has_hit=>FALSE);
dbms_output.put_line('The 3 least frequent queries returning no hit');
dbms_output.put_line('number of times query string');
for i in 1..the_queries.count loop
dbms_output.put_line(the_queries(i).times||' '||the_queries(i).query);
end loop;
end;
/
This returns the following results:
The 3 least frequent queries returning no hit
number of times query string
1 high-tech
1 company
1 cheese
Argentina and Japan do not make this list, because they are queried more than once, while Corn Palace does not make this list because it is successfully queried.