Prior to performing a migration, the following temporary indexes should be created:
- CREATE INDEX [__TO_DELETE_IX_Activities_MatterId_DeletedMatterId_InvoiceId_DateAndTime_C09AF] ON [Activities] ([MatterId], [DeletedMatterId], [InvoiceId],[DateAndTime])
- CREATE INDEX [__TO_DELETE_IX_Activities_MatterId_DeletedMatterId_InvoiceId_DateAndTime_FDEF4] ON [Activities] ([MatterId], [DeletedMatterId], [InvoiceId],[DateAndTime]) INCLUDE ([UserId], [BillableUnits], [ActivityTypeId], [Cost], [IsCredit], [IsLineItemOverride], [NonBillable], [IsTaxable])
- CREATE INDEX [__TO_DELETE_IX_Activities_DeletedMatterId_InvoiceId_MatterId_DateAndTime_42704] ON [Activities] ([DeletedMatterId], [InvoiceId],[MatterId], [DateAndTime])
- CREATE INDEX [__TO_DELETE_IX_Activities_CreatedDate_165B5] ON [Activities] ([CreatedDate])
- CREATE INDEX [__TO_DELETE_IX_Activities_DateAndTime_5E6C1] ON [Activities] ([DateAndTime]) INCLUDE ([UserId], [BillableUnits], [NonBillable])
- CREATE INDEX [__TO_DELETE_IX_CalendarEntriesUsers_ID_3EEB4] ON [CalendarEntriesUsers] ([ID])
- CREATE INDEX [__TO_DELETE_IX_CalendarEntriesUsers_BillForParticipant_C2457] ON [CalendarEntriesUsers] ([BillForParticipant]) INCLUDE ([NonBillableForParticipant])
- CREATE INDEX [__TO_DELETE_IX_IntegrationSyncItems_EntityNameId_EntityIntegrationId_5DF30] ON [IntegrationSyncItems] ([EntityNameId], [EntityIntegrationId]) INCLUDE ([EntityId], [UserIntegrationId], [DateCreated], [DateModified], [IsDeleted], [TenantId], [ChildSyncItemId])
- CREATE INDEX [__TO_DELETE_IX_IntegrationSyncItemSkippedAttendees_IntegrationSyncItemId_068CB] ON [IntegrationSyncItemSkippedAttendees] ([IntegrationSyncItemId])
- CREATE INDEX [__TO_DELETE_IX_PaymentsInvoices_AmountPaid_88346] ON [PaymentsInvoices] ([AmountPaid])
- CREATE INDEX [__TO_DELETE_IX_LedgerEntries_Amount_IsPaid_InvoiceId_0A67C] ON [LedgerEntries] ([Amount], [IsPaid],[InvoiceId])
- CREATE INDEX [__TO_DELETE_IX_LedgerEntries_IsCredit_8C111] ON [LedgerEntries] ([IsCredit]) INCLUDE ([Amount], [MatterId])
- CREATE INDEX [__TO_DELETE_IX_Activities_DeletedMatterId_InvoiceId_MatterId_DateAndTime_F5FD7] ON [Activities] ([DeletedMatterId], [InvoiceId],[MatterId], [DateAndTime]) INCLUDE ([UserId], [BillableUnits], [ActivityTypeId], [Cost], [IsCredit], [IsLineItemOverride], [NonBillable], [IsTaxable])
The following triggers should be disabled:
- DISABLE TRIGGER CalendarEntries_Tag_Update ON CalendarEntries
- DISABLE TRIGGER Activities_Tag_Update ON Activities
- DISABLE TRIGGER Documents_Tag_Update ON Documents
Delete the temporary indexes:
- SELECT
- schema_name(t.schema_id) + '.' + t.[name] AS table_view,
- i.[name] AS index_name,
- FORMATMESSAGE('DROP INDEX %s.%s', t.name, i.name) AS drop_statement
- FROM
- sys.objects t
- INNER JOIN
- sys.indexes i ON t.object_id = i.object_id
- CROSS APPLY (
- SELECT col.[name] + ', '
- FROM sys.index_columns ic
- INNER JOIN sys.columns col ON ic.object_id = col.object_id
- AND ic.column_id = col.column_id
- WHERE ic.object_id = t.object_id
- AND ic.index_id = i.index_id
- ORDER BY col.column_id
- FOR XML PATH ('')) D (column_names)
- WHERE
- t.is_ms_shipped <> 1
- AND i.name LIKE '__TO_DELETE%'
- AND index_id > 0
- ORDER BY
- schema_name(t.schema_id) + '.' + t.[name],
- i.index_id
Re-Enable the relevant triggers
- ENABLE Trigger Activities_Tag_Update ON Activities
- ENABLE Trigger CalendarEntries_Tag_Update ON CalendarEntries
- ENABLE TRIGGER Documents_Tag_Update ON Documents
Batch-Update the activities.
- -- Update the Matters table with recent activity, pending invoicing, current balance, and pending taxes
- UPDATE m
- SET
- m.LastActivityDate = mra.RecentActivityDate,
- m.PendingInvoicing = ISNULL(p.Amount + p.TaxAmount, 0),
- m.CurrentBalance = ISNULL(mgb.Balance, 0),
- m.PendingTaxes = ISNULL(p.TaxAmount, 0)
- FROM
- Matters m
- LEFT JOIN
- Matters_RecentActivity mra ON m.Id = mra.MatterId
- LEFT JOIN
- Activities_PendingInvoicingByMatter p ON m.Id = p.MatterId
- LEFT JOIN
- Matters_GetWithBalances mgb ON m.Id = mgb.MatterId;
- -- Update the Clients table with trust balance
- UPDATE cl
- SET
- cl.TrustBalance = ISNULL(t.Balance, 0)
- FROM
- Clients cl
- LEFT JOIN
- ClientTrustAccountBalances t ON cl.Id = t.ClientId;