0

Need your insights. Thank you for taking time and sharing.

Context

We have a 10 sql scripts which has more than 10000 lines of code and most of them are scripts and some code in each script are irrelevant

Problem I need to identify which scripts and which parts of the script actually being applied on the DB so that we can re-structure the scripts . I at least need to share stats for my safety. I don't have any idea how to start

  • DML operations
  • DDL operations
  • Deadlocks

I'm a junior DBA never had done this activity and I don't what compelled them to push it to me. May be I'm asking too much kindly at least push me in right direction so I can start somewhere.

4
  • Are those scripts running in scheduled jobs or triggered by any other methods? You mentioned "some code in each script are irrelevant", but how did you get to that conclusion? Commented Aug 12, 2024 at 11:28
  • @ronaldo We run each of them manually on DB every time when a DB is restored and that comment was said by DEV which led to these situation. Commented Aug 12, 2024 at 14:08
  • By the described situation I understand you have 2 problems: one is errors happening during the execution of the scripts and the other is low performance. I suggest you begin correcting the errors: see How to find and fix SQL Server deadlocks. To understand what is irrelevant you're gonna have to read the code and judge for yourself what's relevant or not. Commented Aug 12, 2024 at 15:16
  • @Ronaldo I agree. Thanks for the link. I'm looking into Commented Aug 12, 2024 at 18:09

1 Answer 1

0

If you dont know what the script does. Then dont run on live db server and live database.

  1. First step setup a virtual or physical test db server.

    You can easily setup one. If you are using virtual servers If not then you need a bit more time.

    For safety you need a test server.

  2. Backup of the live db and then Restore the backup in test server with the same name of live db.

Analysing of the scripts

You can use few methods to investigate the scripts.

  1. First Method: Run Sql Server Profiler > New Trace > Run

This is a test server so we dont expect to be a noise of sql commands from other users and jobs.

Sql Profiler will show just your script runnings.

If you expect other query running from other sources then you need to filter the trace like in the following picture. enter image description here

I suggest to use Sql Profiler for insulated test server with default config and filters but for production server you could need filters and common configs. You can set them with for events and columns.

enter image description here

You will see actual running portion of your script in profiler as in the following screen shot.

enter image description here

Run the first script and then save the Sql Profiler Trace to a Sql Table with File> Save As > Trace Table

Saved trace table will look like to the following picture.

enter image description here

You can copy TextData to a text or .sql file.

Then Compare, investigate the Trace Table Text data with your script.

  1. Second Method : Add Print Statement lines into the scripts.

     PRINT N'The running code line - command is 1000 - Select Customer ';
    

    IF Condition PRINT N'The running code line - command is 1000'; ELSE PRINT N'The running code line - command is 1000'; GO

  2. Third Method : Instead of using Print Statement Write a function to insert the running lines into a table.

  3. Fourth Method : Make a stored procedure from a script that is you need to investigate. Then by using Visual Studio Data tools debug the procedure

2
  • Thanks for your thoughtful answer. It's clearly pointing it in right direction . Also may I know what are the options to select in sql profiler to identify dml & ddl operations on each table in DB Commented Aug 12, 2024 at 18:08
  • I modified my answer with additional screens. I suggest to use sql profiler as default for test Sql server. You can query the text data for target tables like select * from tracetbl where TextData like '%Net%' Commented Aug 13, 2024 at 8:21

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.