Tag: sql server

  • Check Twice, Update Once

    I was reading through this blog from the SQL Server PSS team (a must-follow), and found this note about the SQL Server Best Practices Analyzer (BPA). This isa tool that can examine an installation of SQL Server and report on any deviations from best practices according to Microsoft, as well as warn you of potential issues with your configuration. I had used it with previous versions of SQL Server and it helped me quickly determine if I’d missed anything during setup.

    Now I haven’t much thought about running this regularly. I’d assume this is something I just run once after installation to be sure I’ve configured, but the blog gives me a good reason to run this regularly, and especially whenever I have an issue. In the entry, it talks about not only noting that an error being returned by SQL Server and noting it’s fixed in a CU, but that a trace flag is also needed. Running the BPA would inform the user that the trace flag needs to be enabled.

    I think it’s great that this type of feature is being designed into a tool like the BPA that can really help a DBA. Knowing that this trace flag needs to be enabled is the type of knowledge that someone might gain with experience, but it’s short lived. At least it should be as I would hope the issue is corrected in a Service Pack w/o the need for a trace flag.

    Tools like this don’t eliminate the need for DBAs. We still need people that can make the decision about whether to apply this, perform testing, and more. These tools just make it easier for us to do our jobs more efficiently.

    Steve Jones

    Update: as noted in the discussion, the MS Baseline Configuration Analyzer is required for the BPA tool. MS BCA v2.0

  • The Cloud is Nothing Special

    In the news this week, there was a major announcement. Buck Woody (blog | @BuckWoody), longtime Microsoft employee and favorite speaker of many in the community, bought an iPad. That in and of itself, with Buck’s frequent touting of Microsoft products, is amazing, and Buck wrote about it on his blog, noting that Windows Azure is platform independent. He even has a picture of a NASA app, something built on Azure, running on his new toy.

    This week, SQL Azure finally gets its own web site, showcasing SQL Server in the cloud. I know lots of DBAs, and IT people in general might be down on computing in the cloud, but I’m not. I think that there are some problems, some applications, that will benefit from cloud computing. Not every instance of SQL Server should be moved to the cloud, but some will work better in the cloud. If nothing else, being able to use cloud services is another evolution of the hosted environments that so many small companies use today. You can get the services you need without the need to do as much administration or setup.

    For most of us, the services that we might use on the Internet are platform independent. We don’t care that Facebook uses Cassandra, or Google uses BigTable, or that TractorByNet uses whatever technology they use. We just want our particular service to work, and it doesn’t mattter what the underlying technology is.

    I think it’s the same with cloud computing. Ultimately it doesn’t matter if it’s Azure, EC2, or some other service. Just like Brent Ozar views virtualization, I would bet that some of the time a cloud service is “good enough” to use.

  • Common SQL Server Mistakes – SELECT *

    I’ve been trying to work on some new presentations so that I have a variety, including some spares, when I go to events. One of the topics that I think has some value, especially for .NET and sysadmin groups, is a list of common mistakes, how to fix them, and why they’re bad.
    I was going to call this Common Developer Mistakes, but I’m not sure that would go over well at Developer events, and I see DBAs making these mistakes along with Windows admins.
    I decided to build a series of blog posts as I work through the presentation to document some of the issues, and help me work through speaking points. Please feel free to comment.
    SELECT * Is For Short Term Use Only
    The first mistake that I often see in application code is that too often people write things like

    SELECT * 
    FROM Sales.Customer

    .csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, “Courier New”, courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }

    What does this do? If I run this in my 2008 AdventureWorks database, I get something like this:

     SelectStar_b

    You can see that I end up with multiple columns (CustomerID, TerritoryID, CustomerType, rowguid, ModifiedDate). That’s handy, and cool, and allows me to get all the data in the table.

    But do I really need it?

    In most applications, my guess is that we don’t. Why do we need TerritoryID? That’s a foreign key to the SalesTerritory table, and typically what I want instead is the SalesTerritory.Name column instead of the ID value.

    I could do this:

    SELECT *
    FROM Sales.Customer c
    INNER JOIN Sales.SalesTerritory t
    ON c.TerritoryID = t.TerritoryID

    but that’s any better. Now I’ve returned even more columns, 10 more to be exact, including TerritoryID twice, once from each table. In AdventureWorks, this is 19k rows, and at a minimum this query has returned 19k rows x 8 bytes (int data type) too much data. That doesn’t sound like a lot, but what if this runs in your application 500 times a day? That’s a lot of wasted:

    • bandwidth
    • disk access
    • memory from caching
    • CPU work on the server AND client

    I would also guess that most of the time when you access a customer, you don’t even want all the rows. Likely you want to filter this somehow, and you will with a WHERE clause, but it’s still wasted time and resources.

    We know that the database often is a bottleneck. It’s a shared resource, it’s one machine, and it doesn’t scale as easy as multiple clients or web servers, or even developers, so we should avoid wasting resources when we don’t have to.

    What Do You Do?

    Here’s what I recommend:

    You can write this, and it’s what I often do:

    SELECT TOP 2
    *
    FROM Sales.Customer

    And I get a limited result set:

    SelectStar_e

    Why is this better? I do this so I can easily see the column names. I can then include those in my SELECT statement, with a quick rewrite.

    SelectStar_f

    I could also quickly use the Object Explorer to find the columns like this:

    SelectStar_g

    And you can right click, and choose “script” and “as SELECT”

    SelectStar_h

    and paste the code into your query window. The results would look something like this:

    SelectStar_i

    Alternatively, my employer, Red Gate Software, makes a fantastic product called SQL Prompt that will help you quickly grab columns. For me, I can do an SSF, and get a SELECT * FROM and then choose the table:

    SelectStar_c

    Not that I see the columns to the right. I could also just select the table with a tab and then if I remove my askterisk, I get a list of columns I can easily pick:

    SelectStar_d

    SQL Prompt makes this easier, but it isn’t that hard to just do this by hand. You could easily grab the columns you need from SSMS and add them to queries.

    The database is a limited resource, even if you have a 256 core server with 1024GB of RAM. You still want to query the data you need and only return what’s necessary. A little more effort when building code will pay off later with much better performing applications.

    References
    A few links from other people that see this as an issue as well.

  • You Don’t Need Log Backups

    You don’t need log backups. You don’t. They aren’t required for SQL Server to function.

    The place when you don’t need log backups is when you have a database operating in the Simple recovery model. In this model, your transaction log will clear out on a regular basis, every few minutes, and the space will be reused.

    I saw a post recently where someone said they had a database that only changed once a day. Data was loaded once a day and otherwise the database was read only. In that case, you:

    • Need log backups if you are in Full or Bulk-Logged recovery models
    • Do not need log backups if you are in simple mode.

    It doesn’t matter how many changes you have, if you don’t have log backups in full recovery, you’ll eventually have an out of control log growth situation.