In SQL Server one way we optimize code is by adding indexes. Indexes help queries by essentially letting the engine know “hey, here’s the fast way to get directly to the data you want”. The alternative is doing full table scans to read all the data, which sucks.
When reviewing code in SQL Server Management Studio (SSMS) we have a similar built-in tool to help save you time: Bookmarks! Bookmarks aren’t quite like indexes but they do help you mark locations in code that you can quickly jump to. This is especially helpful when reviewing code in large scripts.
For this example I’m going to use the amazingly useful Diagnostic Information Queries script (which you should be using if you’re not already) from Glenn Berry (Twitter | Blog). This script is jam packed with all sorts of amazingly insightful information on your server and databases. However I often need to jump around to different parts of the script.
For example, when performance tuning I like to run Glenn’s code for getting back the top wait types on the server. After that I jump down to the section on top cached queries/procedures to get a sense of what’s happening. If I do this manually there’s about 300 lines of code separating the two sections. That’s a lot of annoying scrolling!
Instead what you can do is create bookmarks at these locations. You can do this one of two ways. First set your cursor at the desired location in code. You can set bookmarks by going to the Edit menu –>Bookmarks->Toggle Bookmark. The other way is when you have your cursor set you can use the keyboard shortcut of Ctrl+K, Ctrl+K to add a bookmark. When you’ve created a bookmark you’ll see a small grey/white box in the left side window. Now scroll to other locations in code you’d like a bookmark and repeat this process.
To navigate between Bookmarks you can use the keyboard shortcut Ctrl+K, Ctrl+N to go to next one or Ctrl+K, Ctrl+P to navigate to previous. Now you can quickly and easily jump between sections of code! Happy coding.