olahallengren / sql-server-maintenance-solution

SQL Server Maintenance Solution
https://ola.hallengren.com
MIT License
2.91k stars 756 forks source link

Allow generic threading so IndexOptimize can run in arbitrarily many jobs #668

Open RickPuig opened 2 years ago

RickPuig commented 2 years ago

There's currently a parameter in IndexOptimize that allows you to thread between databases.

This can be generalized to allow threading even within a database, or even schema.

The only necessary change is to create parameters to indicate the number of total threads allowed, as well as which specific thread this job instance is executing on.

The total number of threads allowed can be used to MODULO the object_ids. Then the specific thread for the job instance will just grab instances where object_id % total_threads = specific_thread - 1.

For example, if you allow 3 total threads, and you're executing thread 1, the job will perform maintenance over objects in which

object_id % 3 = 0 (so any object_id divisible by 3).

This allows users to create an arbitrary number of jobs that can run concurrently.

Concurrency on insert into CommandLog might need to get looked at to avoid deadlocks? Dunno. Just an idea.

speedy32129 commented 1 year ago

Are you referring to @DatabasesInParallel parameter ... I'm not following what you're saying and it may be why no one has followed up with anything?

Otherwise please be more specific. Maybe an example.