Originally posted on Idera – http://sqlserverperformance.idera.com/tsql-optimization/understanding-sql-server-statistics/
“Statistics provides tools that you need in order to react intelligently to information you hear or read” – David Lane, 2003
If there’s an upcoming election and you are running for office and getting ready to go from town to town city to city with your flyers, you will want to know approximately how many flyers you’re going to bring.
If you’re the coach of a sports team, you will want to know your players’ stats before you decide who to play when, and against who. You will often play a matchup game, even if you have 20 players, you might be allowed to play just 5 at a time, and you will want to know which of your players will best match up to the other team’s roster. And you don’t want to interview them one by one at game time (table scan), you want to know, based on their statistics, who your best bets are.
Just like the election candidate or the sports coach, SQL Server tries to use statistics to “react intelligently” in its query optimization. Knowing number of records, density of pages, histogram, or available indexes help the SQL Server optimizer “guess” more accurately how it can best retrieve data. A common misnomer is that if you have indexes, SQL Server will use those indexes to retrieve records in your query. Not necessarily. If you create, let’s say, an index to a column City and <90% of the values are ‘Vancouver’, SQL Server will most likely opt for a table scan instead of using the index if it knows these stats.
Today has been quite an eventful day for me, feels like a mishmash of stuff happened today. Definitely worth a blog post.
Here are some of the things I learned and did today.
And man it amazes me how many smart smart helpful people are out there 🙂
I possibly might need to work on SQL Server Service Broker. I think this might be the right tool for a project, but I need to be certain so I need to give it a test drive.
Thanks to Aaron Bertrand (blog | twitter) for pointing me to these excellent excellent resources on SQL Server Broker.
Using OPENXML you can import XML documents into an XML variable, or into a table that has XML columns. The following is an example:
1: -- ==========================================================================
2: -- Object : OPENXML1.sql
3: -- Object Type : Script
4: -- Description : Various examples
5: -- Developer : Donabel Santos
6: -- Origin : 2008/08/17
7: -- Last Modified: 2008/10/04
8: -- Notes :
9: -- ==========================================================================
11: -- bulk load
12: -- For this example, XML file must be saved in C:
13: -- XML file also should specify UTF-8 encoding, ie:
14: -- <?xml version="1.0" encoding="UTF-8"?>
16: DECLARE @XMLTable TABLE
18: xmlcol XML
21: INSERT INTO @XMLTable(xmlcol)
26: SELECT *
27: FROM OPENROWSET(BULK 'c:invoice.xml',SINGLE_BLOB) AS Invoices
28: ) AS Invoices(InvoicesXML)
30: SELECT *
31: FROM @XMLTable