Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update IndexOptimize.sql #848

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

michele-tahay-kohera
Copy link

Included lock timeout setting on fetching details about indexes/statistics. Even with the transaction level to readuncommitted, I had an issue on my environment that locked this query for 15 hours because of another process taking that long (an issue on its own :) ).

Included lock timeout setting on fetching details about indexes/statistics. Even with the transaction level to readuncommitted, I had an issue on my environment that locked this query for 15 hours because of another process taking that long (an issue on its own :) ).
variable @CurrentCommand should be initialized
Copy link

@Imran-imtiaz48 Imran-imtiaz48 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Query Optimization and Index Selection Logic
Strengths:

  1. Comprehensive Query Construction:
    o The query dynamically builds a @CurrentCommand string that adapts based on the provided conditions. This approach adds flexibility to execute specific commands such as setting lock timeouts or transaction isolation levels.
    o The SELECT statement includes a broad range of index and table-related properties, making it a powerful tool for index diagnostics and optimization.
  2. Well-Defined Conditional Logic:
    o The use of conditions ensures that the logic only executes when specific parameters (@ActionsPreferred or @UpdateStatistics) are present or within a time limit. This ensures resource-efficient execution and aligns well with good database practices.
  3. Attention to Detail:
    o Properties like IsMemoryOptimized, AllowPageLocks, and ResumableIndexOperation show that you’re considering modern SQL Server features and edge cases.
    Suggestions for Improvement:
  4. Code Readability:
    o Consider splitting long lines of code for better readability. For example, break the SET @CurrentCommand assignments into multiple lines using string concatenation.
    o Adding comments about what each major section of the code does will make it easier for collaborators to follow.
  5. Dynamic SQL Risks:
    o Ensure that any dynamic SQL generated by @CurrentCommand is thoroughly validated to mitigate the risk of SQL injection or unintended consequences.
  6. Error Handling:
    o Adding error-handling mechanisms (e.g., TRY...CATCH blocks) can improve the robustness of the script.
  7. Logging and Debugging:
    o Incorporate logging for debugging purposes. Logging the generated @CurrentCommand could be useful for troubleshooting.
  8. Performance Optimization:
    o Since this query accesses system views and metadata, ensure that it performs well under heavy loads. Test it in a production-like environment if possible.
    Additional Notes:
    • The inclusion of index-specific columns like IndexType, IsColumnStore, and StatisticsID indicates that this script could be useful for tasks such as performance tuning or index management.
    • If this script will be frequently reused, consider encapsulating it in a stored procedure or a reusable function to enhance modularity.

Copy link

@Imran-imtiaz48 Imran-imtiaz48 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strengths:

  1. Use of Conditional Execution: The inclusion of conditions such as checking for @ActionsPreferred or @UpdateStatistics ensures that the script only executes under specific scenarios, which is a good practice to avoid unnecessary processing.
  2. Dynamic Command Building: The use of dynamic command building with @CurrentCommand allows flexibility in setting up configurations like LOCK_TIMEOUT and TRANSACTION ISOLATION LEVEL. This modularity enhances the script’s adaptability.
  3. Comprehensive Column Selection: The SELECT statement includes a detailed list of columns, which demonstrates thoroughness in extracting metadata or performance-related details about database objects.
    Suggestions for Improvement:
  4. Code Readability:
    o Consider breaking the dynamic command into smaller, clearly labeled sections. This will make the script easier to read and maintain. For example, you could create separate variables for each command part.
    o Add comments to explain the purpose of each block of code.
  5. Lock Timeout Logic:
    o Validate that the multiplication by 1000 (to convert seconds to milliseconds) aligns with the intended behavior for all possible @LockTimeout values. It might help to add a check to handle edge cases like NULL or negative values.
  6. Use of READ UNCOMMITTED Isolation Level:
    o While the READ UNCOMMITTED isolation level is efficient, it can lead to dirty reads. Confirm that this behavior is acceptable for the intended use case and document the reason for this choice in a comment.
  7. Error Handling:
    o The script currently lacks error handling. Consider wrapping the dynamic SQL execution in a TRY...CATCH block to log or handle potential issues during execution.
  8. Parameter Validation:
    o Add input validation at the beginning of the script to ensure that parameters like @ActionsPreferred, @UpdateStatistics, @Timelimit, and @starttime are not NULL or out of expected ranges unless intended.
  9. Formatting the SELECT Statement:
    o For better readability, align the selected columns vertically in a structured format.
    Example:
    o SELECT SchemaID,
    o SchemaName,
    o ObjectID,
    o ObjectName,
    o ObjectType,
    o ...
    Additional Enhancements:
    • If this script is intended for long-term use or frequent modification, consider parameterizing it further and documenting its usage in an external file or header comment block.
    • Integrate logging mechanisms to track the execution of the script for auditing purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants