Ola Hallengren Index Optimize script failed because a deadlock to accomplish the nightly maintenance tasks on production servers, every night I have scheduled the script to defrag the indexes. Due to other nightly application maintenance tasks, sometimes the job fails because of a deadlock. At the same time starts the script to defrag an index and on the same table a query is doing an update where the index was created. I would like to find a solution to avoid that job fails. Because I can't have a control on that query, I would find a way to check if the table is locked until the query will be completed and after that, start the defragmentation of the indexes.

Any suggestions would be greatly appreciated.

Thanks.
In most cases, running an index job should be done only once per week.  If there is an off peak time (like weekends) that you can run it that would be better.


  Index maintenance one day per week, Scheduling, How should I schedule jobs? 


If you must run daily, you can change the variables below to address lock timeouts. 


WaitAtLowPriorityAbortAfterWait
LockTimeout


https://ola.hallengren.com/sql-server-index-and-statistics-maintenance.html