001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.execute.rts.RunTimeStatisticsImpl
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.sql.execute.rts;
023:
024: import org.apache.derby.iapi.services.io.StoredFormatIds;
025: import org.apache.derby.iapi.services.io.Formatable;
026:
027: import org.apache.derby.iapi.services.i18n.MessageService;
028: import org.apache.derby.iapi.reference.SQLState;
029:
030: import org.apache.derby.iapi.services.io.FormatableHashtable;
031:
032: import org.apache.derby.iapi.sql.execute.RunTimeStatistics;
033: import java.util.Vector;
034:
035: import java.io.ObjectOutput;
036: import java.io.ObjectInput;
037: import java.io.IOException;
038:
039: import java.sql.Timestamp;
040:
041: /**
042: RunTimeStatistics implemenation.
043:
044: @author jerry
045:
046: */
047: public final class RunTimeStatisticsImpl implements RunTimeStatistics {
048:
049: /* Leave these fields public for object inspectors */
050: public String statementText;
051: public String statementName;
052: public String spsName;
053: public long parseTime;
054: public long bindTime;
055: public long optimizeTime;
056: public long generateTime;
057: public long compileTime;
058: public long executeTime;
059: public Timestamp beginCompilationTimestamp;
060: public Timestamp endCompilationTimestamp;
061: public Timestamp beginExecutionTimestamp;
062: public Timestamp endExecutionTimestamp;
063: public ResultSetStatistics topResultSetStatistics;
064: public ResultSetStatistics[] subqueryTrackingArray;
065:
066: // CONSTRUCTORS
067: /**
068: *
069: */
070: public RunTimeStatisticsImpl(String spsName, String statementName,
071: String statementText, long compileTime, long parseTime,
072: long bindTime, long optimizeTime, long generateTime,
073: long executeTime, Timestamp beginCompilationTimestamp,
074: Timestamp endCompilationTimestamp,
075: Timestamp beginExecutionTimestamp,
076: Timestamp endExecutionTimestamp,
077: ResultSetStatistics[] subqueryTrackingArray,
078: ResultSetStatistics topResultSetStatistics) {
079: this .spsName = spsName;
080: this .statementName = statementName;
081: this .statementText = statementText;
082: this .compileTime = compileTime;
083: this .parseTime = parseTime;
084: this .bindTime = bindTime;
085: this .optimizeTime = optimizeTime;
086: this .generateTime = generateTime;
087: this .executeTime = executeTime;
088: this .beginCompilationTimestamp = beginCompilationTimestamp;
089: this .endCompilationTimestamp = endCompilationTimestamp;
090: this .beginExecutionTimestamp = beginExecutionTimestamp;
091: this .endExecutionTimestamp = endExecutionTimestamp;
092: this .subqueryTrackingArray = subqueryTrackingArray;
093: this .topResultSetStatistics = topResultSetStatistics;
094: }
095:
096: // RunTimeStatistics methods
097: /**
098: * Get the total compile time for the associated query in milliseconds.
099: * Compile time can be divided into parse, bind, optimize and generate times.
100: *
101: * @return long The total compile time for the associated query in milliseconds.
102: */
103: public long getCompileTimeInMillis() {
104: return compileTime;
105: }
106:
107: /**
108: * Get the parse time for the associated query in milliseconds.
109: *
110: * @return long The parse time for the associated query in milliseconds.
111: */
112: public long getParseTimeInMillis() {
113: return parseTime;
114: }
115:
116: /**
117: * Get the bind time for the associated query in milliseconds.
118: *
119: * @return long The bind time for the associated query in milliseconds.
120: */
121: public long getBindTimeInMillis() {
122: return bindTime;
123: }
124:
125: /**
126: * Get the optimize time for the associated query in milliseconds.
127: *
128: * @return long The optimize time for the associated query in milliseconds.
129: */
130: public long getOptimizeTimeInMillis() {
131: return optimizeTime;
132: }
133:
134: /**
135: * Get the generate time for the associated query in milliseconds.
136: *
137: * @return long The generate time for the associated query in milliseconds.
138: */
139: public long getGenerateTimeInMillis() {
140: return generateTime;
141: }
142:
143: /**
144: * Get the execute time for the associated query in milliseconds.
145: *
146: * @return long The execute time for the associated query in milliseconds.
147: */
148: public long getExecuteTimeInMillis() {
149: return executeTime;
150: }
151:
152: /**
153: * Get the timestamp for the beginning of query compilation.
154: *
155: * @return java.sql.Timestamp The timestamp for the beginning of query compilation.
156: */
157: public Timestamp getBeginCompilationTimestamp() {
158: return beginCompilationTimestamp;
159: }
160:
161: /**
162: * Get the timestamp for the end of query compilation.
163: *
164: * @return java.sql.Timestamp The timestamp for the end of query compilation.
165: */
166: public Timestamp getEndCompilationTimestamp() {
167: return endCompilationTimestamp;
168: }
169:
170: /**
171: * Get the timestamp for the beginning of query execution.
172: *
173: * @return java.sql.Timestamp The timestamp for the beginning of query execution.
174: */
175: public Timestamp getBeginExecutionTimestamp() {
176: return beginExecutionTimestamp;
177: }
178:
179: /**
180: * Get the timestamp for the end of query execution.
181: *
182: * @return java.sql.Timestamp The timestamp for the end of query execution.
183: */
184: public Timestamp getEndExecutionTimestamp() {
185: return endExecutionTimestamp;
186: }
187:
188: /**
189: * Get the name of the associated query or statement.
190: * (This will be an internally generated name if the
191: * user did not assign a name.)
192: *
193: * @return java.lang.String The name of the associated query or statement.
194: */
195: public String getStatementName() {
196: return statementName;
197: }
198:
199: /**
200: * Get the name of the Stored Prepared Statement
201: * for the statement.
202: *
203: * @return java.lang.String The SPS name of the associated query or statement.
204: */
205: public String getSPSName() {
206: return spsName;
207: }
208:
209: /**
210: * Get the text for the associated query or statement.
211: *
212: * @return java.lang.String The text for the associated query or statement.
213: */
214: public String getStatementText() {
215: return statementText;
216: }
217:
218: /**
219: * Get the estimated row count for the number of rows returned
220: * by the associated query or statement.
221: *
222: * @return The estimated number of rows returned by the associated
223: * query or statement.
224: */
225: public double getEstimatedRowCount() {
226: if (topResultSetStatistics == null) {
227: return 0.0;
228: }
229: return topResultSetStatistics.getEstimatedRowCount();
230: }
231:
232: /**
233: * Get the execution plan for the associated query or statement as a String.
234: *
235: * @return java.lang.String The execution plan for the associated query or statement.
236: */
237: public String getStatementExecutionPlanText() {
238: if (topResultSetStatistics == null) {
239: return (String) null;
240: }
241:
242: String subqueryInfo = "";
243:
244: /* Dump out the statistics for any subqueries */
245:
246: if (subqueryTrackingArray != null) {
247: boolean foundAttached = false;
248:
249: for (int index = 0; index < subqueryTrackingArray.length; index++) {
250: if (subqueryTrackingArray[index] != null) {
251: /* Only print attached subqueries message once */
252: if (!foundAttached) {
253: subqueryInfo = MessageService
254: .getTextMessage(SQLState.RTS_MATERIALIZED_SUBQS)
255: + ":\n";
256: foundAttached = true;
257: }
258: subqueryInfo = subqueryInfo
259: + subqueryTrackingArray[index]
260: .getStatementExecutionPlanText(1);
261: }
262: }
263: }
264: return subqueryInfo
265: + topResultSetStatistics
266: .getStatementExecutionPlanText(0);
267: }
268:
269: /**
270: * Get the information on the nodes relating to table and index scans
271: * from the execution plan for the associated query or statement as a String.
272: *
273: * @return java.lang.String The nodes relating to table and index scans
274: * from the execution plan for the associated query or statement.
275: */
276: public String getScanStatisticsText() {
277: return (topResultSetStatistics == null) ? (String) null
278: : topResultSetStatistics.getScanStatisticsText(null, 0);
279: }
280:
281: /**
282: * Get the information on the nodes relating to table and index scans
283: * for table tableName from the execution plan for the associated query
284: * or statement as a String.
285: *
286: * @param tableName table for which user seeks statistics.
287: *
288: * @return java.lang.String The nodes relating to table and index scans
289: * from the execution plan for the associated query or statement for
290: * tableName.
291: */
292: public String getScanStatisticsText(String tableName) {
293: if (topResultSetStatistics == null)
294: return (String) null;
295: String s = topResultSetStatistics.getScanStatisticsText(
296: tableName, 0);
297: return (s.equals("")) ? null : s;
298: }
299:
300: // Class implementation
301:
302: public String toString() {
303: String spstext = (spsName != null) ? ("Stored Prepared Statement Name: \n\t"
304: + spsName + "\n")
305: : "";
306: return spstext
307: + MessageService
308: .getTextMessage(SQLState.RTS_STATEMENT_NAME)
309: + ": \n\t"
310: + statementName
311: + "\n"
312: + MessageService
313: .getTextMessage(SQLState.RTS_STATEMENT_TEXT)
314: + ": \n\t"
315: + statementText
316: + "\n"
317: + MessageService
318: .getTextMessage(SQLState.RTS_PARSE_TIME)
319: + ": "
320: + parseTime
321: + "\n"
322: + MessageService.getTextMessage(SQLState.RTS_BIND_TIME)
323: + ": "
324: + bindTime
325: + "\n"
326: + MessageService
327: .getTextMessage(SQLState.RTS_OPTIMIZE_TIME)
328: + ": "
329: + optimizeTime
330: + "\n"
331: + MessageService
332: .getTextMessage(SQLState.RTS_GENERATE_TIME)
333: + ": "
334: + generateTime
335: + "\n"
336: + MessageService
337: .getTextMessage(SQLState.RTS_COMPILE_TIME)
338: + ": "
339: + compileTime
340: + "\n"
341: + MessageService
342: .getTextMessage(SQLState.RTS_EXECUTE_TIME)
343: + ": "
344: + executeTime
345: + "\n"
346: + MessageService
347: .getTextMessage(SQLState.RTS_BEGIN_COMP_TS)
348: + " : "
349: + beginCompilationTimestamp
350: + "\n"
351: + MessageService
352: .getTextMessage(SQLState.RTS_END_COMP_TS)
353: + " : "
354: + endCompilationTimestamp
355: + "\n"
356: + MessageService
357: .getTextMessage(SQLState.RTS_BEGIN_EXE_TS)
358: + " : "
359: + beginExecutionTimestamp
360: + "\n"
361: + MessageService
362: .getTextMessage(SQLState.RTS_END_EXE_TS)
363: + " : "
364: + endExecutionTimestamp
365: + "\n"
366: + MessageService
367: .getTextMessage(SQLState.RTS_STMT_EXE_PLAN_TXT)
368: + ": \n" + getStatementExecutionPlanText();
369: }
370:
371: /**
372: * Get the objects to be displayed when this tree object is expanded.
373: * <P>
374: * The objects returned can be of any type, including addtional Inspectables.
375: *
376: * @return java.util.Vector A vector of objects.
377: */
378: public Vector getChildren() {
379: Vector children = new Vector();
380: children.addElement(topResultSetStatistics);
381: return children;
382: }
383:
384: }
|