Giter VIP home page Giter VIP logo

sqlcop's Introduction

SQLCop

Welcome to the SQL Cop repository!

These are a set of tSQLt tests designed to allow you to detect common problems in your database, originally maintained by George Mastros on LessThanDot.

This project is now managed by SQLServerCentral and Redgate Software. Steve Jones is the primary contact, but all updates and contributions are welcome by pull request. If you submit a contribution, please include reasons why the changes are helpful to most users of the project.

Requirements

This project requires the tsqlt framework to run the tests. All SQL Server versions that support the SQLCLR can use these tests. Please check the tsqlt requirements for your system.

NOTE : These Tests are NOT suitable for production databases

Installing the tests

All tests are created a stored procedures in a test class (schema), called SQLCop. You can compile them in a SQL Server database and use the tsql.run command to execute any of these tests.

Issues Checked

The following is a list of issues that are checked by the current project.

Code

Procedures with SP_
VarChar Size Problems
Decimal Size Problem
Undocumented Procedures
Procedures without SET NOCOUNT ON
Procedures with SET ROWCOUNT
Procedures with @@Identity
Procedures with dynamic sql
Procedures using dynamic sql without sp_executesql

Column

Column Name Problems
Columns with float data type
Columns with image data type
Tables with text/ntext
Collation Mismatch
UniqueIdentifier with NewId

Table/Views

Table Prefix
Table Name Problems
Missing Foreign Keys
Wide Tables
Tables without a primary key
Empty Tables
Views with order by
Unnamed Constraints

Indexes

Fragmented indexes
Missing Foreign Key Indexes
Forwarded Records

Configuration

Database Collation
Auto Close
Auto Create
Auto Shrink
Auto Update
Compatibility Level
Login Language
Old Backups
Orphaned Users
User Aliases
Ad Hoc Distributed Queries
CLR
Database and log files on the same physical disk
Database Mail
Deprecated Features
Instant File Initialization
Max Degree of Parallelism
OLE Automation Procedures
Service Account
SMO and DMO
SQL Server Agent Service
xp cmdshell

Health

Buffer cache hit ratio
Page life expectancy

sqlcop's People

Contributors

chrislambrou avatar mark-raymond avatar michelletaylorrg avatar redgateadmin avatar robclenshaw avatar way0utwest avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sqlcop's Issues

"Invalid objects" check fails with cross database calls

When running the "Invalid objects" check if a proc (for instance) references an entity on another database (on the same server) the reference is considered invalid/missing.

Consider:

  • DatabaseA
    • Tables
      • MyTable1
  • DatabaseB
    • Procs
      • MyProcB
        begin
        select * from DatabaseA.dbo.MyTable1
        end

When running the "Invalid objects" test the above will fail as no reference to "MyTable1" will be found in the "DatabaseB" database.

Missing Foreign Key Indexes missing multicolumn indexes

I have some foreign keys that I put together in a multicolumn index to ensure that all foreign keys are indexed. Running the following query shows me I don't have any missing foreign key indexes: source for the query

CREATE TABLE #TempForeignKeys (TableName varchar(100), ForeignKeyName varchar(100) , ObjectID int)
INSERT INTO #TempForeignKeys 
SELECT OBJ.NAME, ForKey.NAME, ForKey .[object_id] 
FROM sys.foreign_keys ForKey
INNER JOIN sys.objects OBJ
ON OBJ.[object_id] = ForKey.[parent_object_id]
WHERE OBJ.is_ms_shipped = 0
 
CREATE TABLE #TempIndexedFK (ObjectID int)
INSERT INTO #TempIndexedFK  
SELECT ObjectID      
FROM sys.foreign_key_columns ForKeyCol
JOIN sys.index_columns IDXCol
ON ForKeyCol.parent_object_id = IDXCol.[object_id]
JOIN #TempForeignKeys FK
ON  ForKeyCol.constraint_object_id = FK.ObjectID
WHERE ForKeyCol.parent_column_id = IDXCol.column_id 
 
SELECT * FROM #TempForeignKeys WHERE ObjectID NOT IN (SELECT ObjectID FROM #TempIndexedFK)
 
 
DROP TABLE #TempForeignKeys
DROP TABLE #TempIndexedFK

I tend to keep columns in a multicolumn index that I know will always be used together when joining on to my table. The rest of the foreign keys I suspect will be used separately I give them their own index.

Is it by design that the Missing Foreign Key Indexes test ignores multicolumn indexes, and if so, what is the reason behind it?

To reproduce, create a table with 2 foreign keys and add those foreign keys to one multicolumn index.

[SQLCop].[test Table name problems] -- logic check

Currently, the logic check for invalid table names is:

SET @AcceptableSymbols = '_$' TABLE_NAME COLLATE SQL_LATIN1_GENERAL_CP1_CI_AI Like '%[^a-z' + @AcceptableSymbols + ']%'

The way I read that is if the table name has any character, anywhere in the table name that isn't an underscore, dollar sign, or the letters a through z, fail. A few problems in that logic:

Tables that start with underscore or dollar sign would require brackets.
Tables can have numerics in the name (just not for the first character)
Tables could have a space if they are bracketed.

Should this test assume brackets are in use when needed? Should this test actually attempt to create a table with the given name to test it's valiidty like this solution on StackOverflow?

I would like to contribute

I would like to contribute, I can add the code that checks if a single query has multiple plans
Do you agree?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.