Oversizing a column in a table – finding varchar(max) and text in MsSQL

It is very commmon when a table have one or more to wide columns.
Sometimes developers choose not supported columns, for example, text type in MS SQL database.
I always looking for text, ntext, varchar(max) and nvarchar(max) :

After it, I check the (n)varchar and (n)char columns.

I am finding the longest string.

Index fragmentation with table row count and index creation and modification date

You can often check the index defragmentation. It is useful if you start checking with the biggest table, because the biggest effect is there.
I like to check the indexes with the creation and modification date. If you rebuild the index your index modification date will be updated.

If you want to see for all database you can replace the

  • DB_ID()
  • with

  • NULL
  • in the script.

    Python and MS SQL connection

    Install ODBC driver for MS SQL>
    pip install pyodbc
    Downloading pyodbc-4.0.30-cp39-cp39-win_amd64.whl (67 kB)
    |████████████████████████████████| 67 kB 745 kB/s
    Installing collected packages: pyodbc
    Successfully installed pyodbc-4.0.30

    Python query: