提交 e653e4e6 authored 作者: Noel Grandin's avatar Noel Grandin

#426: Support ANALYZE TABLE statement

上级 3c94053d
...@@ -415,9 +415,11 @@ ALTER VIEW ADDRESS_VIEW RECOMPILE ...@@ -415,9 +415,11 @@ ALTER VIEW ADDRESS_VIEW RECOMPILE
" "
"Commands (DDL)","ANALYZE"," "Commands (DDL)","ANALYZE","
ANALYZE [ SAMPLE_SIZE rowCountInt ] ANALYZE [ TABLE tableName ] [ SAMPLE_SIZE rowCountInt ]
"," ","
Updates the selectivity statistics of all tables. The selectivity is used by the Updates the selectivity statistics of tables.
If no table name is given, all tables are analyzed.
The selectivity is used by the
cost based optimizer to select the best index for a given query. If no sample cost based optimizer to select the best index for a given query. If no sample
size is set, up to 10000 rows per table are read. The value 0 means all rows are size is set, up to 10000 rows per table are read. The value 0 means all rows are
read. The selectivity can be set manually using ALTER TABLE ALTER COLUMN read. The selectivity can be set manually using ALTER TABLE ALTER COLUMN
......
...@@ -21,6 +21,8 @@ Change Log ...@@ -21,6 +21,8 @@ Change Log
<h2>Next Version (unreleased)</h2> <h2>Next Version (unreleased)</h2>
<ul> <ul>
<li>Issue #426: Support ANALYZE TABLE statement
</li>
<li>Issue #472: Support CREATE SEQUENCE ... ORDER as a NOOP for Oracle compatibility <li>Issue #472: Support CREATE SEQUENCE ... ORDER as a NOOP for Oracle compatibility
</li> </li>
</ul> </ul>
......
...@@ -550,6 +550,11 @@ public class Parser { ...@@ -550,6 +550,11 @@ public class Parser {
private Prepared parseAnalyze() { private Prepared parseAnalyze() {
Analyze command = new Analyze(session); Analyze command = new Analyze(session);
if (readIf("TABLE")) {
Table table = readTableOrView();
command.setTable(table);
command.setTop(readPositiveInt());
}
if (readIf("SAMPLE_SIZE")) { if (readIf("SAMPLE_SIZE")) {
command.setTop(readPositiveInt()); command.setTop(readPositiveInt());
} }
......
...@@ -22,8 +22,8 @@ import org.h2.value.ValueInt; ...@@ -22,8 +22,8 @@ import org.h2.value.ValueInt;
import org.h2.value.ValueNull; import org.h2.value.ValueNull;
/** /**
* This class represents the statement * This class represents the statements
* ANALYZE * ANALYZE and ANALYZE TABLE
*/ */
public class Analyze extends DefineCommand { public class Analyze extends DefineCommand {
...@@ -31,20 +31,32 @@ public class Analyze extends DefineCommand { ...@@ -31,20 +31,32 @@ public class Analyze extends DefineCommand {
* The sample size. * The sample size.
*/ */
private int sampleRows; private int sampleRows;
/**
* used in ANALYZE TABLE...
*/
private Table table;
public Analyze(Session session) { public Analyze(Session session) {
super(session); super(session);
sampleRows = session.getDatabase().getSettings().analyzeSample; sampleRows = session.getDatabase().getSettings().analyzeSample;
} }
public void setTable(Table table) {
this.table = table;
}
@Override @Override
public int update() { public int update() {
session.commit(true); session.commit(true);
session.getUser().checkAdmin(); session.getUser().checkAdmin();
Database db = session.getDatabase(); Database db = session.getDatabase();
if (table != null) {
analyzeTable(session, table, sampleRows, true);
} else {
for (Table table : db.getAllTablesAndViews(false)) { for (Table table : db.getAllTablesAndViews(false)) {
analyzeTable(session, table, sampleRows, true); analyzeTable(session, table, sampleRows, true);
} }
}
return 0; return 0;
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论