mssql indexes analyse WIP

This commit is contained in:
Jan Prochazka
2021-08-14 09:36:22 +02:00
parent 71e1ea5736
commit 3c0bc69662
5 changed files with 42 additions and 1 deletions

View File

@@ -2,6 +2,7 @@ const engines = require('../engines');
const { testWrapper } = require('../tools');
const t1Sql = 'CREATE TABLE t1 (id int not null primary key, val1 varchar(50) null)';
const ix1Sql = 'CREATE index ix1 ON t1(val1)';
const t2Sql = 'CREATE TABLE t2 (id int not null primary key, val2 varchar(50) null)';
const txMatch = (tname, vcolname, nextcol) =>
@@ -98,7 +99,7 @@ describe('Table analyse', () => {
const structure1 = await driver.analyseFull(conn);
if (engine.dbSnapshotBySeconds) await new Promise(resolve => setTimeout(resolve, 1100));
await driver.query(conn, 'ALTER TABLE t2 ADD nextcol varchar(50)');
const structure2 = await driver.analyseIncremental(conn, structure1);
@@ -109,4 +110,16 @@ describe('Table analyse', () => {
expect(structure2.tables.find(x => x.pureName == 't2')).toEqual(t2NextColMatch);
})
);
test.each(engines.map(engine => [engine.label, engine]))(
'Index - full analysis - %s',
testWrapper(async (conn, driver, engine) => {
await driver.query(conn, t1Sql);
await driver.query(conn, ix1Sql);
const structure = await driver.analyseFull(conn);
const t1 = structure.tables.find(x => x.pureName == 't1');
expect(t1.indexes.length).toEqual(1);
})
);
});

View File

@@ -117,6 +117,9 @@ 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

@@ -8,6 +8,8 @@ const views = require('./views');
const programmables = require('./programmables');
const viewColumns = require('./viewColumns');
const getSchemas = require('./getSchemas');
const indexes = require('./indexes');
const indexcols = require('./indexcols');
module.exports = {
columns,
@@ -20,4 +22,6 @@ module.exports = {
programmables,
viewColumns,
getSchemas,
indexes,
indexcols,
};

View File

@@ -0,0 +1,7 @@
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
where c.object_id =OBJECT_ID_CONDITION
order by c.key_ordinal
`;

View File

@@ -0,0 +1,14 @@
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
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
and i.index_id between 1 and 254
--and i.name not in
-- (select o.name from sysobjects o
-- where o.parent_obj = i.object_id
-- and objectproperty(o.id, N'isConstraint') = 1.0)
and i.object_id =OBJECT_ID_CONDITION
`;