Debian Codegraph @

src/main/org/h2/command/ddl/Analyze.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.command.ddl;
7
8 import java.util.Arrays;
9
10 import org.h2.command.CommandInterface;
11 import org.h2.engine.Constants;
12 import org.h2.engine.Database;
13 import org.h2.engine.Right;
14 import org.h2.engine.SessionLocal;
15 import org.h2.index.Cursor;
16 import org.h2.result.Row;
17 import org.h2.schema.Schema;
18 import org.h2.table.Column;
19 import org.h2.table.Table;
20 import org.h2.table.TableType;
21 import org.h2.value.DataType;
22 import org.h2.value.Value;
23
24 /**
25 * This class represents the statements
26 * ANALYZE and ANALYZE TABLE
27 */
28 public class Analyze extends DefineCommand {
29
30 private static final class SelectivityData {
31
32 private long distinctCount;
33
34 /**
35 * The number of occupied slots, excluding the zero element (if any).
36 */
37 private int size;
38
39 private int[] elements;
40
41 /**
42 * Whether the zero element is present.
43 */
44 private boolean zeroElement;
45
46 private int maxSize;
47
48 SelectivityData() {
49 elements = new int[8];
50 maxSize = 7;
51 }
52
53 void add(Value v) {
54 int currentSize = currentSize();
55 if (currentSize >= Constants.SELECTIVITY_DISTINCT_COUNT) {
56 size = 0;
57 Arrays.fill(elements, 0);
58 zeroElement = false;
59 distinctCount += currentSize;
60 }
61 int hash = v.hashCode();
62 if (hash == 0) {
63 zeroElement = true;
64 } else {
65 if (size >= maxSize) {
66 rehash();
67 }
68 add(hash);
69 }
70 }
71
72 int getSelectivity(long count) {
73 int s;
74 if (count == 0) {
75 s = 0;
76 } else {
77 s = (int) (100 * (distinctCount + currentSize()) / count);
78 if (s <= 0) {
79 s = 1;
80 }
81 }
82 return s;
83 }
84
85 private int currentSize() {
86 int size = this.size;
87 if (zeroElement) {
88 size++;
89 }
90 return size;
91 }
92
93 private void add(int element) {
94 int len = elements.length;
95 int mask = len - 1;
96 int index = element & mask;
97 int plus = 1;
98 do {
99 int k = elements[index];
100 if (k == 0) {
101 // found an empty record
102 size++;
103 elements[index] = element;
104 return;
105 } else if (k == element) {
106 // existing element
107 return;
108 }
109 index = (index + plus++) & mask;
110 } while (plus <= len);
111 // no space, ignore
112 }
113
114 private void rehash() {
115 size = 0;
116 int[] oldElements = elements;
117 int len = oldElements.length << 1;
118 elements = new int[len];
119 maxSize = (int) (len * 90L / 100);
120 for (int k : oldElements) {
121 if (k != 0) {
122 add(k);
123 }
124 }
125 }
126
127 }
128
129 /**
130 * The sample size.
131 */
132 private int sampleRows;
133 /**
134 * used in ANALYZE TABLE...
135 */
136 private Table table;
137
138 public Analyze(SessionLocal session) {
139 super(session);
140 sampleRows = getDatabase().getSettings().analyzeSample;
141 }
142
143 public void setTable(Table table) {
144 this.table = table;
145 }
146
147 @Override
148 public long update() {
149 session.getUser().checkAdmin();
150 Database db = getDatabase();
151 if (table != null) {
152 analyzeTable(session, table, sampleRows, true);
153 } else {
154 for (Schema schema : db.getAllSchemasNoMeta()) {
155 for (Table table : schema.getAllTablesAndViews(null)) {
156 analyzeTable(session, table, sampleRows, true);
157 }
158 }
159 }
160 return 0;
161 }
162
163 /**
164 * Analyze this table.
165 *
166 * @param session the session
167 * @param table the table
168 * @param sample the number of sample rows
169 * @param manual whether the command was called by the user
170 */
171 public static void analyzeTable(SessionLocal session, Table table, int sample, boolean manual) {
172 if (!table.isValid()
173 || table.getTableType() != TableType.TABLE //
174 || table.isHidden() //
175 || session == null //
176 || !manual && (session.getDatabase().isSysTableLocked() || table.hasSelectTrigger()) //
177 || table.isTemporary() && !table.isGlobalTemporary() //
178 && session.findLocalTempTable(table.getName()) == null //
179 || table.isLockedExclusively() && !table.isLockedExclusivelyBy(session)
180 || !session.getUser().hasTableRight(table, Right.SELECT) //
181 // if the connection is closed and there is something to undo
182 || session.getCancel() != 0) {
183 return;
184 }
185 table.lock(session, Table.READ_LOCK);
186 Column[] columns = table.getColumns();
187 int columnCount = columns.length;
188 if (columnCount == 0) {
189 return;
190 }
191 Cursor cursor = table.getScanIndex(session).find(session, null, null);
192 if (cursor.next()) {
193 SelectivityData[] array = new SelectivityData[columnCount];
194 for (int i = 0; i < columnCount; i++) {
195 Column col = columns[i];
196 if (!DataType.isLargeObject(col.getType().getValueType())) {
197 array[i] = new SelectivityData();
198 }
199 }
200 long rowNumber = 0;
201 do {
202 Row row = cursor.get();
203 for (int i = 0; i < columnCount; i++) {
204 SelectivityData selectivity = array[i];
205 if (selectivity != null) {
206 selectivity.add(row.getValue(i));
207 }
208 }
209 rowNumber++;
210 } while ((sample <= 0 || rowNumber < sample) && cursor.next());
211 for (int i = 0; i < columnCount; i++) {
212 SelectivityData selectivity = array[i];
213 if (selectivity != null) {
214 columns[i].setSelectivity(selectivity.getSelectivity(rowNumber));
215 }
216 }
217 } else {
218 for (int i = 0; i < columnCount; i++) {
219 columns[i].setSelectivity(0);
220 }
221 }
222 session.getDatabase().updateMeta(session, table);
223 }
224
225 public void setTop(int top) {
226 this.sampleRows = top;
227 }
228
229 @Override
230 public int getType() {
231 return CommandInterface.ANALYZE;
232 }
233
234 }