SQL Server: Find fragmentation of tables and indexes


Without adding so many paragraphs, since you are looking for fragmentation, you can below procedure. 

You can create it in the same database. 

CREATE PROCEDURE usp_CheckFragmentation @TableName NVARCHAR(128) = NULL AS BEGIN SET NOCOUNT ON; IF OBJECT_ID('tempdb..#Fragmentation') IS NOT NULL DROP TABLE #Fragmentation; CREATE TABLE #Fragmentation ( DatabaseName SYSNAME, SchemaName SYSNAME, TableName SYSNAME, IndexName SYSNAME, IndexTypeDesc NVARCHAR(60), AvgFragmentationInPercent FLOAT, PageCount BIGINT ); DECLARE @SQL NVARCHAR(MAX); SET @SQL = ' INSERT INTO #Fragmentation SELECT DB_NAME() AS DatabaseName, s.name AS SchemaName, t.name AS TableName, i.name AS IndexName, i.type_desc AS IndexTypeDesc, ps.avg_fragmentation_in_percent AS AvgFragmentationInPercent, ps.page_count AS PageCount FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, ''LIMITED'') ps INNER JOIN sys.indexes i ON ps.object_id = i.object_id AND ps.index_id = i.index_id INNER JOIN sys.tables t ON ps.object_id = t.object_id INNER JOIN sys.schemas s ON t.schema_id = s.schema_id WHERE (@TableName IS NULL OR t.name = @TableName)'; EXEC sp_executesql @SQL, N'@TableName NVARCHAR(128)', @TableName; SELECT * FROM #Fragmentation ORDER BY AvgFragmentationInPercent DESC; DROP TABLE #Fragmentation; END; GO

SQL Server Interview Questions - Set-01



If you are preparing for SQL Server interview, it would be better if you prepare few mock interviews. Although, SQL Server is vast and employer might ask you questions based on their requirement, but still you can prepare some of the commonly asked questions. 

Suggestions: 

  • Avoid guessing answers. Only respond when you are confident in your answer. Incorrect guesses can frustrate the interviewer.
  • Try not to ask the interviewer to repeat the question multiple times.
  • Stay calm, even if the question is difficult or very technical. Do not question or criticize the interviewer for asking such questions.
  • Even if you are unable to answer a few questions, don’t start skipping the rest. Some interviewers assess overall knowledge and may still find value in your skillset, even if it is limited.
  • Create an accurate resume that reflects your true knowledge. Avoid exaggerating your skills and experiences.

  • Interview Questions: 

    1. What are the different types of SQL Server backups?

    2. Explain the difference between a full backup and a differential backup.

    3. How can you improve the performance of a slow-running query?

    4. What is a deadlock, and how can you resolve it in SQL Server?

    5. What are SQL Server indexes, and what types are there?

    6. How do you monitor and optimize database performance in SQL Server?

    7. Explain the concept of isolation levels in SQL Server.

    8. What is the purpose of SQL Server Agent, and how do you use it?

    9. Describe the differences between clustered and non-clustered indexes.

    10. How would you handle database corruption?

    11. What is the SQL Server Profiler, and how is it used?

    12. How do you perform database replication in SQL Server?

    13. What are the different types of SQL Server recovery models?

    14. How can you secure a SQL Server database?

    15. Explain the difference between a stored procedure and a function in SQL Server.

    16. What is the use of the `DBCC` command in SQL Server?

    17. How do you manage database connections and sessions in SQL Server?

    18. What is a SQL Server view, and how does it differ from a table?

    19. How do you migrate a database from one SQL Server instance to another?

    20. What is the purpose of SQL Server Integration Services (SSIS)?

    21. How do you handle data archiving in SQL Server?

    22. What are SQL Server Always On Availability Groups, and how do they work?

    23. What is the purpose of SQL Server Management Studio (SSMS)?

    24. How do you implement SQL Server auditing?

    25. What are the common performance tuning techniques in SQL Server?


    These questions cover a broad range of topics including backups, indexing, performance tuning, security, replication, and more. Preparing for these questions can help demonstrate a solid understanding of SQL Server administration.