fix: sort field init performance (#1675)
* fix: sort field init performance * chore: test table prefix * fix: mysql compability
This commit is contained in:
parent
e9d5fa7f9c
commit
55c7bb5cf5
@ -17,6 +17,50 @@ describe('string field', () => {
|
|||||||
await db.close();
|
await db.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should init sorted value with thousand records', async () => {
|
||||||
|
const Test = db.collection({
|
||||||
|
name: 'tests',
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
type: 'string',
|
||||||
|
name: 'name',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'string',
|
||||||
|
name: 'group',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
await db.sync();
|
||||||
|
|
||||||
|
await Test.model.bulkCreate(
|
||||||
|
(() => {
|
||||||
|
const values = [];
|
||||||
|
for (let i = 0; i < 100000; i++) {
|
||||||
|
values.push({
|
||||||
|
group: 'a',
|
||||||
|
name: `r${i}`,
|
||||||
|
});
|
||||||
|
|
||||||
|
values.push({
|
||||||
|
group: 'b',
|
||||||
|
name: `r${i}`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return values;
|
||||||
|
})(),
|
||||||
|
);
|
||||||
|
|
||||||
|
Test.setField('sort', { type: 'sort', scopeKey: 'group' });
|
||||||
|
|
||||||
|
const begin = Date.now();
|
||||||
|
await db.sync();
|
||||||
|
const end = Date.now();
|
||||||
|
// log time cost as milliseconds
|
||||||
|
console.log(end - begin);
|
||||||
|
});
|
||||||
|
|
||||||
it('should init sorted value with scopeKey', async () => {
|
it('should init sorted value with scopeKey', async () => {
|
||||||
const Test = db.collection({
|
const Test = db.collection({
|
||||||
name: 'tests',
|
name: 'tests',
|
||||||
|
@ -42,7 +42,19 @@ export class SortField extends Field {
|
|||||||
};
|
};
|
||||||
|
|
||||||
initRecordsSortValue = async ({ transaction }) => {
|
initRecordsSortValue = async ({ transaction }) => {
|
||||||
const doInit = async (scopeKey = null, scopeValue = null) => {
|
const orderField = (() => {
|
||||||
|
const model = this.collection.model;
|
||||||
|
if (model.primaryKeyAttribute) {
|
||||||
|
return model.primaryKeyAttribute;
|
||||||
|
}
|
||||||
|
if (model.rawAttributes['createdAt']) {
|
||||||
|
return model.rawAttributes['createdAt'].field;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`can not find order key for collection ${this.collection.name}`);
|
||||||
|
})();
|
||||||
|
|
||||||
|
const needInit = async (scopeKey = null, scopeValue = null) => {
|
||||||
const filter = {};
|
const filter = {};
|
||||||
if (scopeKey && scopeValue) {
|
if (scopeKey && scopeValue) {
|
||||||
filter[scopeKey] = scopeValue;
|
filter[scopeKey] = scopeValue;
|
||||||
@ -61,44 +73,53 @@ export class SortField extends Field {
|
|||||||
transaction,
|
transaction,
|
||||||
});
|
});
|
||||||
|
|
||||||
const orderKey = (() => {
|
return emptyCount === totalCount && emptyCount > 0;
|
||||||
const model = this.collection.model;
|
};
|
||||||
if (model.primaryKeyAttribute) {
|
|
||||||
return model.primaryKeyAttribute;
|
const doInit = async (scopeKey = null, scopeValue = null) => {
|
||||||
}
|
const queryInterface = this.collection.db.sequelize.getQueryInterface();
|
||||||
if (model.rawAttributes['createdAt']) {
|
|
||||||
return 'createdAt';
|
const quotedOrderField = queryInterface.quoteIdentifier(orderField);
|
||||||
|
|
||||||
|
const sql = `
|
||||||
|
WITH ordered_table AS (
|
||||||
|
SELECT *, ROW_NUMBER() OVER (${
|
||||||
|
scopeKey ? `PARTITION BY ${queryInterface.quoteIdentifier(scopeKey)}` : ''
|
||||||
|
} ORDER BY ${quotedOrderField}) AS new_sequence_number
|
||||||
|
FROM ${this.collection.quotedTableName()}
|
||||||
|
${
|
||||||
|
scopeKey
|
||||||
|
? `WHERE ${queryInterface.quoteIdentifier(scopeKey)} IN (${scopeValue.map((v) => `'${v}'`).join(',')})`
|
||||||
|
: ''
|
||||||
|
}
|
||||||
|
)
|
||||||
|
${
|
||||||
|
this.collection.db.inDialect('mysql')
|
||||||
|
? `
|
||||||
|
UPDATE ${this.collection.quotedTableName()}, ordered_table
|
||||||
|
SET ${this.collection.quotedTableName()}.${this.name} = ordered_table.new_sequence_number
|
||||||
|
WHERE ${this.collection.quotedTableName()}.${quotedOrderField} = ordered_table.${quotedOrderField}
|
||||||
|
${scopeKey ? `AND ${this.collection.quotedTableName()}.${scopeKey} = ordered_table.${scopeKey}` : ''}
|
||||||
|
`
|
||||||
|
: `
|
||||||
|
UPDATE ${this.collection.quotedTableName()}
|
||||||
|
SET ${queryInterface.quoteIdentifier(this.name)} = ordered_table.new_sequence_number
|
||||||
|
FROM ordered_table
|
||||||
|
WHERE ${this.collection.quotedTableName()}.${quotedOrderField} = ${queryInterface.quoteIdentifier(
|
||||||
|
'ordered_table',
|
||||||
|
)}.${quotedOrderField};
|
||||||
|
`
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(`can not find order key for collection ${this.collection.name}`);
|
`;
|
||||||
})();
|
|
||||||
|
|
||||||
if (emptyCount === totalCount && emptyCount > 0) {
|
await this.collection.db.sequelize.query(sql, {
|
||||||
const records = await this.collection.repository.find({
|
transaction,
|
||||||
order: [orderKey],
|
});
|
||||||
filter,
|
|
||||||
transaction,
|
|
||||||
});
|
|
||||||
|
|
||||||
let start = 1;
|
|
||||||
for (const record of records) {
|
|
||||||
await record.update(
|
|
||||||
{
|
|
||||||
sort: start,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
hooks: false,
|
|
||||||
transaction,
|
|
||||||
silent: true,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
start += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const scopeKey = this.options.scopeKey;
|
const scopeKey = this.options.scopeKey;
|
||||||
|
|
||||||
if (scopeKey) {
|
if (scopeKey) {
|
||||||
const groups = await this.collection.repository.find({
|
const groups = await this.collection.repository.find({
|
||||||
attributes: [scopeKey],
|
attributes: [scopeKey],
|
||||||
@ -106,10 +127,17 @@ export class SortField extends Field {
|
|||||||
raw: true,
|
raw: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const needInitGroups = [];
|
||||||
for (const group of groups) {
|
for (const group of groups) {
|
||||||
await doInit(scopeKey, group[scopeKey]);
|
if (await needInit(scopeKey, group[scopeKey])) {
|
||||||
|
needInitGroups.push(group['group']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
|
if (needInitGroups.length > 0) {
|
||||||
|
await doInit(scopeKey, needInitGroups);
|
||||||
|
}
|
||||||
|
} else if (await needInit()) {
|
||||||
await doInit();
|
await doInit();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user