sql server index analyser works

This commit is contained in:
Jan Prochazka 2021-08-19 15:14:27 +02:00
parent a5cc99005a
commit c5a3ee01ee
5 changed files with 34 additions and 7 deletions

View File

@ -120,6 +120,8 @@ describe('Table analyse', () => {
const t1 = structure.tables.find(x => x.pureName == 't1');
expect(t1.indexes.length).toEqual(1);
expect(t1.indexes[0].columns.length).toEqual(1);
expect(t1.indexes[0].columns[0]).toEqual(expect.objectContaining({ columnName: 'val1' }));
})
);
});

View File

@ -6,6 +6,8 @@ export interface NamedObjectInfo {
export interface ColumnReference {
columnName: string;
refColumnName?: string;
isIncludedColumn?: boolean;
isDescending?: boolean;
}
export interface ConstraintInfo extends NamedObjectInfo {

View File

@ -51,6 +51,7 @@ class MsSqlAnalyser extends DatabaseAnalyser {
}
createQuery(resFileName, typeFields) {
if (!sql[resFileName]) throw new Error(`Missing analyse file ${resFileName}`);
return super.createQuery(sql[resFileName], typeFields);
}
@ -67,6 +68,8 @@ class MsSqlAnalyser extends DatabaseAnalyser {
const pkColumnsRows = await this.driver.query(this.pool, this.createQuery('primaryKeys', ['tables']));
const fkColumnsRows = await this.driver.query(this.pool, this.createQuery('foreignKeys', ['tables']));
const schemaRows = await this.driver.query(this.pool, this.createQuery('getSchemas'));
const indexesRows = await this.driver.query(this.pool, this.createQuery('indexes', ['tables']));
const indexcolsRows = await this.driver.query(this.pool, this.createQuery('indexcols', ['tables']));
const schemas = schemaRows.rows;
@ -92,6 +95,26 @@ class MsSqlAnalyser extends DatabaseAnalyser {
columns: columnsRows.rows.filter(col => col.objectId == row.objectId).map(getColumnInfo),
primaryKey: DatabaseAnalyser.extractPrimaryKeys(row, pkColumnsRows.rows),
foreignKeys: DatabaseAnalyser.extractForeignKeys(row, fkColumnsRows.rows),
indexes: indexesRows.rows
.filter(idx => idx.object_id == row.objectId && !idx.is_unique_constraint)
.map(idx => ({
..._.pick(idx, ['constraintName', 'indexType', 'isUnique']),
columns: indexcolsRows.rows.filter(
col => col.object_id == idx.object_id && col.index_id == idx.index_id
).map(col => ({
..._.pick(col, ['columnName', 'isDescending', 'isIncludedColumn']),
})),
})),
uniques: indexesRows.rows
.filter(idx => idx.object_id == row.objectId && idx.is_unique_constraint)
.map(idx => ({
..._.pick(idx, ['constraintName']),
columns: indexcolsRows.rows.filter(
col => col.object_id == idx.object_id && col.index_id == idx.index_id
).map(col => ({
..._.pick(col, ['columnName']),
})),
})),
}));
const views = viewsRows.rows.map(row => ({
@ -117,9 +140,6 @@ class MsSqlAnalyser extends DatabaseAnalyser {
createSql: getCreateSql(row),
}));
const indexesRows = await this.driver.query(this.pool, this.createQuery('indexes', ['tables']));
const indexcolsRows = await this.driver.query(this.pool, this.createQuery('indexesindexcols', ['tables']));
return {
tables,
views,

View File

@ -1,7 +1,10 @@
module.exports = `
select c.object_id, c.index_id, c.column_id, c.is_descending_key, c.is_included_column from sys.index_columns c
select
c.object_id, c.index_id, c.column_id,
col.name as columnName,
c.is_descending_key as isDescending, c.is_included_column as isIncludedColumn
from sys.index_columns c
inner join sys.columns col on c.object_id = col.object_id and c.column_id = col.column_id
where c.object_id =OBJECT_ID_CONDITION
order by c.key_ordinal
`;

View File

@ -1,5 +1,5 @@
module.exports = `
select i.object_id as objectId, i.name as constraintName, i.type_desc, i.is_unique as isUnique,i.index_id, i.is_unique_constraint from sys.indexes i
select i.object_id, i.name as constraintName, i.type_desc as indexType, i.is_unique as isUnique,i.index_id, i.is_unique_constraint from sys.indexes i
where i.is_primary_key=0
and i.is_hypothetical=0 and indexproperty(i.object_id, i.name, 'IsStatistics') = 0
and objectproperty(i.object_id, 'IsUserTable') = 1