Denis Gobo : Internalshttp://sqlblog.com/blogs/denis_gobo/archive/tags/Internals/default.aspxTags: InternalsenCommunityServer 2.1 SP2 (Build: 61129.1)Interview With Craig Freedman About Indexing, Query Plans And Performancehttp://sqlblog.com/blogs/denis_gobo/archive/2008/05/23/6953.aspxFri, 23 May 2008 23:39:00 GMT21093a07-8b3d-42db-8cbf-3350fcbf5496:6953Denis Gobo3http://sqlblog.com/blogs/denis_gobo/comments/6953.aspxhttp://sqlblog.com/blogs/denis_gobo/commentrss.aspx?PostID=6953http://sqlblog.com/blogs/denis_gobo/rsscomments.aspx?PostID=6953<P>I asked&nbsp;for some names of people who you would like to see interviewed and Craig Freedman's name popped up a couple of times. I contacted Craig and he was kind enough to take time out from his busy schedule to answer these questions.&nbsp;&nbsp;So, here are the questions.</P>
<P><STRONG>What are the most important things a person can do to master SQL Server?</STRONG><BR><BR>Use the product! &nbsp;I recall once hearing an expert explain that it takes about ten years of practice to master a skill – and this refers to any skill whether it is mastering a software product, a sport, a musical instrument, etc. &nbsp;Mentors, books, and classes certainly help whether you are just getting started or expanding your existing skill set, but the reality is there is no substitute for hands on experience.<BR><BR><STRONG>What are the most important things a person can do to master Transact-SQL?<BR></STRONG><BR>See my answer to the last question!<BR><BR>One thing that is worth pointing out is how different SQL (not the “Transact” part – just the query language itself) is from most other programming languages. &nbsp;Most languages like C, C++, C#, VB, and JavaScript are procedural. &nbsp;This is how programmers are taught to think. &nbsp;First do A, then do B, then do C, etc. &nbsp;Moreover, because most procedural languages share the same common elements, programming skills are relatively transferable from one language to another. &nbsp;A good programmer can learn multiple languages fairly quickly.<BR><BR>SQL is much different. &nbsp;SQL is a declarative language. &nbsp;You tell the database what you want without specifying how to get it. &nbsp;It’s a different paradigm and years of experience with procedural languages will do little to help you master SQL. &nbsp;To become a SQL expert, you need to train yourself to think differently.<BR><BR><STRONG>What SQL Server books are on your bookshelf?</STRONG><BR><BR>Surprisingly (or perhaps not surprisingly depending on your point of view), I have only Kalen’s “Inside SQL Server 2005” series. &nbsp;Much of what I need to know about SQL Server I learn either by checking Books Online, asking other developers (okay, I’m lucky that I have this option!), reviewing the source code (again, lucky me!), and searching the web.<BR><BR><STRONG>What new technologies introduced in SQL Server 2005 do you think are the most beneficial for performance?<BR></STRONG><BR>This is an interesting question to ask a developer. &nbsp;When asked this question, most developers will tend to migrate toward the features or technologies that they work on and understand. &nbsp;However, like most developers, I rarely use the product (other than for testing purposes) and I’m not a consultant. &nbsp;So I actually have few strong opinions about what really works or doesn’t work from a customer perspective. &nbsp;Instead, I prefer to hear from customers what they really like and dislike and what they would like to see in the product.<BR><BR>That said, I do think that the 64-bit and 64-way support that went mainstream with SQL Server 2005 is critical for large enterprise customers who need the most scalable and largest servers. &nbsp;The DMVs that were added to SQL Server 2005 also have made a huge difference in identifying and addressing a host of performance issues. &nbsp;Of course there were also a whole lot of smaller features that, depending on your application or workload, could make a huge difference for you but I couldn’t possibly name them all here.<BR><BR><STRONG>What new technologies in SQL Server 2008 do you think are the most beneficial for performance?<BR>Name three things that are new in SQL Server 2008 that you find are the most valuable?</STRONG><BR><BR>I’m going to take these two questions together. &nbsp;As with SQL Server 2008, there are many new features that depending on your application could have a major impact: compression, plan freezing (which should make service packs and upgrades far less painful going forward), the new MERGE statement, star join performance, parallel scans of partitioned tables, and the list goes on.<BR><BR><STRONG>What is the biggest mistake people make when looking at a query plan?</STRONG><BR><BR>I’m not sure about the biggest mistake. &nbsp;What I would do is to encourage everyone to learn as much as possible about how to read query plans, to understand the different types of operators and how they work, and to be careful not to jump to any conclusions before fully understanding a query, schema, and plan. &nbsp;These skills are essential for evaluating whether a query plan is good or bad or whether a better plan exists.<BR><BR>There are many things that can go wrong with query plans. &nbsp;For example, a bad plan could be due to cardinality and costing errors, missing indexes, bad join orders, a poor choice of join or aggregation types, too little memory, etc. &nbsp;At the same time, there are few, if any, absolute rights and wrongs. &nbsp;Sometimes scans are better while other times seeks are better. &nbsp;There is no best join type and no join type is inherently bad.<BR><BR><STRONG>Why did you decide to co-author Inside Microsoft SQL Server 2005: Query Tuning and Optimization?<BR></STRONG><BR>After I started blogging, Kalen approached me to see whether I was interested in contributing any of my material to her book. &nbsp;I’d never authored a book before and was really excited about the opportunity to try something new. &nbsp;So, I jumped at the opportunity. &nbsp;It was a lot of work but a great experience. &nbsp;Thanks, Kalen!<BR><BR><STRONG>Can you name some best practices in terms of creating indexes?</STRONG><BR><BR>Most importantly, before you start trying to tune your indexes, I think you need to know your workload, establish performance requirements and service level agreements, and understand your query plans. &nbsp;For example, is your workload mostly read only (in which case the extra indexes may be mostly free aside from the disk space requirements) or a mix of reads and writes (so that the extra indexes need to be maintained)? &nbsp;Is it more important for your reporting queries to run quickly or for your data loads to complete in the minimum time? &nbsp;Is this an OLTP system or a data warehouse?<BR><BR>Once you know what you want to accomplish, you can focus on where you can get the biggest gains for the minimum cost. &nbsp;Generally, it’s best to have a clustered index (rather than a heap). &nbsp;If you create a heap and one non-clustered index, you have two objects to maintain. &nbsp;If you create a single clustered index, you have only one object to maintain. &nbsp;Better yet, clustered indexes tend to perform better than heaps for scans.<BR><BR>Look for large scans with highly selective filters. &nbsp;Consider whether an index might enable a much more efficient seek. &nbsp;Look for sorts that might be replaced by an ordered scan. &nbsp;Look for bookmark lookups over many rows that might be eliminated by adding included columns. &nbsp;Consider whether creating a different clustered index might eliminate more bookmark lookups. &nbsp;If you have foreign key constraints, consider whether you need indexes on the foreign keys to support the constraint validation efficiently. &nbsp;While experimenting, beware that creating new indexes may change plans in unexpected ways. &nbsp;In SQL Server 2005, the missing indexes DMV and the Database Tuning Advisor (DTA) can help identify possible candidate indexes, but beware as they tend to be rather aggressive with their suggestions.<BR><BR>Finally, try not to overdo it. &nbsp;Factor the cost of maintaining indexes into your decisions. &nbsp;Make sure that all of your indexes and all of the keys and included columns are really used. &nbsp;Get rid of any extraneous indexes.<BR><BR><STRONG>Since we now have partitioning functions is there still a need for partitioned views?</STRONG><BR><BR>In almost all cases, partitioned tables are more efficient, easier to manage and use, and overall the best choice. &nbsp;However, there are still two cases where partitioned views can be useful. &nbsp;The first case is for distributed partitioned views. &nbsp;Partitioned tables cannot span multiple SQL Server instances while partitioned views can. &nbsp;The second case is for tables where the partitions are heavily skewed. &nbsp;With partitioned tables, the query plan shares the same operator(s) for each partition. &nbsp;This design results in efficient compilation times and compact plans. &nbsp;With partitioned views, the optimizer is free to optimize each partition independently and, if the partitions differ in terms of cardinality or cost, to choose different operators for each partition. &nbsp;This design leads to longer compilation times and larger plans but can yield more efficient plans if the partitions are not uniform.<BR><BR><STRONG>Are you working on any upcoming books?</STRONG><BR><BR>No. &nbsp;After completing my chapters for “Inside SQL Server 2005: Query Tuning and Optimization,” I realized just how much work it is to write a book. &nbsp;As I said, it was a great experience and I’m grateful for the opportunity, but I’ve decided to prioritize and put my family first!<BR><BR><STRONG>When should you use a clustered index and when should you use a non clustered index?</STRONG><BR><BR>As I mentioned earlier, it’s almost always best to have a clustered index rather than a heap. &nbsp;For the clustered index, try to choose a key that does not get updated. &nbsp;Updating the clustered index key tends to lead to more fragmentation and to expensive update plans that touch every index. &nbsp;Since the clustered index covers all columns in the table, you’ll also probably want to look at how you can choose the key to minimize bookmark lookups or sorts. &nbsp;For example, if you have a set of common queries that all seek on a particular key and then touch a large number of columns and/or rows in the table, it may be best to choose that key for the clustered index.<BR><BR>If you have a query that seeks on a different key, runs less frequently, or only touches a small number of columns and/or rows, that might be a better candidate for a non-clustered index. &nbsp;Remember that you can include extra columns in a non-clustered index and avoid a bookmark lookup. &nbsp;But, don’t forget that a bookmark lookup is not necessarily a problem over a small set of rows.<BR><BR><STRONG>How much does fragmentation affect query performance?</STRONG><BR><BR>It depends. &nbsp;For rotating media, random I/Os are much slower than sequential I/Os. &nbsp;The problem with fragmentation is that it turns what would be sequential I/Os into random I/Os. &nbsp;Fragmentation won’t affect performance if your workload already includes mostly random I/Os (for example, lots of small index seeks), if most of your data fits in memory so there is little or no I/O at all, or if your data is stored on SSDs where random and sequential I/Os perform comparably. &nbsp;However, fragmentation could hurt performance if your workload includes many sequential table or index scans.<BR><BR>Unfortunately, the problem doesn’t stop there. &nbsp;SQL Server reports logical fragmentation. &nbsp;In most real world scenarios, the details of the physical storage subsystem are abstracted and hidden from SQL Server by all of the layers of hardware and software including the Windows file system and device drivers, SANs, and even drive firmware. &nbsp;A file that is logically sequential may be stored in a physically fragmented manner while a table that is fragmented may be cached in SAN memory. &nbsp;In the end, defragmenting an index may or may not be necessary and may or may not help.<BR><BR><STRONG>What tools are available for developers to use when doing query performance tuning</STRONG><BR><BR>The only tools I’m familiar with are those that ship with SQL Server: Management Studio, Profiler, DMVs, DTA, and the SQL Server 2005 Performance Dashboard Reports. &nbsp;You can do quite a lot with these tools. &nbsp;You can view query plans and try out different alternatives. &nbsp;You can identify expensive queries and possible index alternatives.</P><img src="http://sqlblog.com/aggbug.aspx?PostID=6953" width="1" height="1">IndexingInternalsInterviewquery planDid You Know SQL Server Has A Black Box Like An Airplane?http://sqlblog.com/blogs/denis_gobo/archive/2007/06/03/1395.aspxSun, 03 Jun 2007 19:33:00 GMT21093a07-8b3d-42db-8cbf-3350fcbf5496:1395Denis Gobo1http://sqlblog.com/blogs/denis_gobo/comments/1395.aspxhttp://sqlblog.com/blogs/denis_gobo/commentrss.aspx?PostID=1395http://sqlblog.com/blogs/denis_gobo/rsscomments.aspx?PostID=1395Paul Randal writes:
<BLOCKQUOTE>"Kimberly mentioned that SQL Server has a 'black-box' trace, similar to an aircraft flight-recorder, which I'd never heard of. It's an internal trace that has the last 5MB of various trace events and it's dumped to a file when SQL Server crashes. This can be really useful if you're troubleshooting an issue that causing SQL Server to crash or someone or something is telling SQL Server to shutdown and its unclear who or what is doing it." </BLOCKQUOTE>Read here how to turn it on: <A href="http://blogs.msdn.com/sqlserverstorageengine/archive/2007/06/03/sql-server-s-black-box.aspx">http://blogs.msdn.com/sqlserverstorageengine/archive/2007/06/03/sql-server-s-black-box.aspx</A><img src="http://sqlblog.com/aggbug.aspx?PostID=1395" width="1" height="1">InternalsSQL ServerTrace