-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2134 from mponomar/comdb2-index-usage
Add comdb2_index_usage tables
- Loading branch information
Showing
13 changed files
with
171 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
Copyright 2020 Bloomberg Finance L.P. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
#if (!defined(SQLITE_CORE) || defined(SQLITE_BUILDING_FOR_COMDB2)) && \ | ||
!defined(SQLITE_OMIT_VIRTUALTABLE) | ||
|
||
#if defined(SQLITE_BUILDING_FOR_COMDB2) && !defined(SQLITE_CORE) | ||
#define SQLITE_CORE 1 | ||
#endif | ||
|
||
#include "comdb2.h" | ||
#include "comdb2systblInt.h" | ||
#include "sql.h" | ||
#include "ezsystables.h" | ||
#include "cdb2api.h" | ||
#include "schema_lk.h" | ||
|
||
static sqlite3_module systblIndexUsageModule = { | ||
.access_flag = CDB2_ALLOW_USER, | ||
}; | ||
|
||
struct index_usage { | ||
char *tablename; | ||
int64_t ixnum; | ||
char *cscname; | ||
char *sqlname; | ||
int64_t steps; | ||
int64_t non_sql_steps; | ||
}; | ||
typedef struct index_usage index_usage; | ||
|
||
static void free_index_usage(void *recsp, int nrecs) { | ||
struct index_usage *ixs = (struct index_usage*) recsp; | ||
for (int i = 0; i < nrecs; i++) { | ||
struct index_usage *ix = &ixs[i]; | ||
free(ix->cscname); | ||
free(ix->sqlname); | ||
free(ix->tablename); | ||
} | ||
free(ixs); | ||
} | ||
|
||
static int get_index_usage(void **recsp, int *nrecs) { | ||
struct dbtable *db; | ||
struct index_usage *ixs = NULL; | ||
int allocated = 0; | ||
int nix = 0; | ||
|
||
rdlock_schema_lk(); | ||
|
||
for (int dbn = 0; dbn < thedb->num_dbs; dbn++) { | ||
db = thedb->dbs[dbn]; | ||
if (strncmp(db->tablename, "sqlite_stat", strlen("sqlite_stat")) == 0) | ||
continue; | ||
logmsg(LOGMSG_USER, "table '%s'\n", db->tablename); | ||
for (int ixnum = 0; ixnum < db->nix; ixnum++) { | ||
if (nix == allocated) { | ||
allocated = allocated * 2 + 16; | ||
struct index_usage *n = realloc(ixs, allocated * sizeof(struct index_usage)); | ||
if (n == NULL) { | ||
free_index_usage(ixs, nix); | ||
unlock_schema_lk(); | ||
return -1; | ||
} | ||
ixs = n; | ||
} | ||
struct index_usage *ix = &ixs[nix]; | ||
ix->tablename = strdup(db->tablename); | ||
ix->ixnum = ixnum; | ||
ix->cscname = strdup(db->schema->ix[ixnum]->csctag); | ||
ix->sqlname = strdup(db->schema->ix[ixnum]->sqlitetag); | ||
ix->steps = db->sqlixuse[ixnum]; | ||
ix->non_sql_steps = db->ixuse[ixnum]; | ||
|
||
nix++; | ||
} | ||
} | ||
*nrecs = nix; | ||
*recsp = ixs; | ||
unlock_schema_lk(); | ||
return 0; | ||
} | ||
|
||
int systblSQLIndexStatsInit(sqlite3 *db) { | ||
int rc = create_system_table(db, "comdb2_index_usage", &systblIndexUsageModule, | ||
get_index_usage, free_index_usage, sizeof(index_usage), | ||
CDB2_CSTRING, "table_name", -1, offsetof(index_usage, tablename), | ||
CDB2_INTEGER, "ix_num", -1, offsetof(index_usage, ixnum), | ||
CDB2_CSTRING, "csc_name", -1, offsetof(index_usage, cscname), | ||
CDB2_CSTRING, "sql_name", -1, offsetof(index_usage, sqlname), | ||
CDB2_INTEGER, "steps", -1, offsetof(index_usage, steps), | ||
CDB2_INTEGER, "non_sql_steps", -1, offsetof(index_usage, non_sql_steps), | ||
SYSTABLE_END_OF_FIELDS); | ||
return rc; | ||
} | ||
|
||
|
||
#endif /* (!defined(SQLITE_CORE) || defined(SQLITE_BUILDING_FOR_COMDB2)) \ | ||
&& !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/bin/bash | ||
|
||
a_dbn=$1 | ||
default=default | ||
#default=local | ||
|
||
cdb2sql ${CDB2_OPTIONS} $a_dbn $default - <<EOF | ||
create table ixtest(a int, b int); \$\$ | ||
create index ixtest_a on ixtest(a); | ||
create index ixtest_b on ixtest(b); | ||
EOF | ||
|
||
( | ||
echo "begin" | ||
for a in $(seq 1 99); do | ||
for b in $(seq 1 99); do | ||
echo "insert into ixtest values($a, $b);" | ||
done | ||
done | ||
echo "commit" | ||
echo "analyze" | ||
echo 'select a from ixtest where a between 1 and 10' | ||
echo 'select a from ixtest where a between 1 and 10' | ||
echo 'select a from ixtest where a between 1 and 10' | ||
echo 'select a from ixtest where a between 1 and 10' | ||
echo 'select a from ixtest where a between 1 and 10' | ||
echo 'select b from ixtest where b between 1 and 10' | ||
) | cdb2sql ${CDB2_OPTIONS} $a_dbn $default - >/dev/null | ||
|
||
cdb2sql ${CDB2_OPTIONS} $a_dbn $default 'select * from comdb2_index_usage where table_name="ixtest"' > testindexusage.out | ||
diff testindexusage.out testindexusage.expected >/dev/null | ||
rc=$? | ||
if [[ $rc -ne 0 ]]; then | ||
echo "Failed index usage test" | ||
echo diff $(pwd)/testindexusage.out $(pwd)/testindexusage.expected | ||
fi | ||
|
||
exit $? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
(table_name='ixtest', ix_num=0, csc_name='IXTEST_A', sql_name='$IXTEST_A_B7CCCE2', steps=4955, non_sql_steps=0) | ||
(table_name='ixtest', ix_num=1, csc_name='IXTEST_B', sql_name='$IXTEST_B_92759D58', steps=991, non_sql_steps=0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters