2017年8月5日星期六

7 Things Developers Should Know About SQL Server


7. SQL functions rarely perform well.

Good developers like to reuse code by putting it into functions, and then calling those functions from multiple places.  That’s a great practice in the app tier, but it has huge performance drawbacks in the database tier.
Check out Paul White’s excellent post on Forcing a Parallel Query Plan – in particular, the list of things that produce a serial zone in the plan.  Most functions will cause your query to go single-threaded.  Sad trombone.
If you do want to reuse code, consider stored procedures and views instead.  (Granted, they can come with their own performance drawbacks, but I’m just trying to get you started on the right foot as quickly as possible here, and functions are a broken foot.)

6. “WITH (NOLOCK)” doesn’t actually mean no locking.

At some point in your career, you’re going to start using WITH (NOLOCK) on everything because it gets your query results faster.  That’s not necessarily a bad idea, but it can come with some surprising side effects that Kendra discusses in her “There’s Something About Nolock” video.  I’m going to focus on one of them here, though.
When you query a table – even WITH (NOLOCK) – you take out a schema stability lock.  No one else can change that table or indexes until your query is finished.  That doesn’t sound like a big deal until you need to drop an index, but you can’t because people are constantly querying a table, and they think there’s no overhead as long as they use WITH (NOLOCK).
There’s no silver bullet here, but start by reading about SQL Server’s isolation levels – I bet READ COMMITTED SNAPSHOT ISOLATION is an even better choice for your app.  It gets you consistent data with less blocking hassles.

5. Use 3 connection strings in your app.

I know, you’ve only got one SQL Server today, but trust me, this is worth it.  Set up three connection strings that all point to the same destination today, but down the road, when you need to scale, you’ll be able to set up different database servers to handle each of these:
  1. Connection for Writes & Realtime Reads – this is the connection string you’re already using today, and you think that all data needs to come from here.  You can leave all of your code in place, but as you write new code or touch existing pages, think about changing each query to one of the below connections.
  2. Connection for Data 5-15 Minutes Old – this is for data that can be slightly stale, but still needs to be from today.
  3. Connection for Data as of Yesterday – for management reports and trending data.  If you run an online store, you might pull reviews from here, for example, and tell users that their reviews take a day to publish.
That first connection string is the toughest one to scale; we don’t have a lot of options in SQL Server to scale out multiple servers that handle writes.  (We do have options – they’re just painful to implement and manage.)  The lower-tier connection strings 2 and 3 are much, much easier and cheaper to scale.  For more about this technique, check out my 3 Favorite Connection String Tips.

4. Use a staging/apptempdb database.

Your app probably uses the database for some scratch work – processing, sorting, loading, caching, etc.  It wouldn’t break your heart if this data disappeared, but you’d like to keep the table structures around permanently.  Today, you’re doing this work in your main application database.
Create a separate database – call it MyAppTemp – and do your work in there instead.  Put this database in simple recovery mode, and only back it up once daily.  Don’t hassle with high availability or disaster recovery on this database.
This technique accomplishes a lot of really cool scalability stuff.  It minimizes the changes to the main app database, which means you get faster transaction log backups and differential backups for it.  If you’re log shipping this database to a disaster recovery site, your important data will arrive faster – and not be impeded by all the scratch work.  You can even use different storage for these different databases – perhaps cheap local SSD for MyAppTemp, keeping your shared storage connection free for the critical production stuff.

3. Yesterday’s articles and books are often wrong today.

SQL Server has been out for over a decade, and a lot has changed over the years.  Unfortunately, the old material isn’t updated to cover what’s happening today.  Even today’s material from reputable sources is often wrong – take this critique of Microsoft’s Performance Tuning SQL Server guide.  Fellow Microsoft Certified Master Jonathan Kehayias points out a bunch of really bad advice that comes straight from a Microsoft document.
When you read something that sounds like good advice, I like to try the Anti-Doctor-Phil strategy.  Dr. Phil preaches that you should love every idea for fifteen minutes.  Instead, try hating it – try to disprove what you read before you put it into production.  Even when advice is generally good, it might not be good advice for your own environment.  (Yes, that includes my advice too.)

2. Avoid ORDER BY; sort in the app instead.

To sort your query results, SQL Server burns CPU time.  SQL Server Enterprise Edition goes for about $7,000 per CPU core – not per processor, but per core.  A two-socket, 6-core-each server rings up at around $84k – and that’s just the licensing costs, not the hardware costs.  You can buy a heck of a lot of application servers (even ones with 256GB or more of memory) for $84k.
Consume all of the query results as fast as possible into memory in your app, and then sort.  Your application is already designed in a way that you can scale out multiple app servers to distribute CPU load, whereas your database server…is not.
UPDATE: I’ve gotten a lot of comments wailing about how the app only needs ten rows of a ten million row dataset.  Sure, if you’re doing TOP 10, you’ll need an order by – but how about reworking the query to avoid juggling so much data?  And if the data sounds like too much for the app server to sort, it’s probably causing work on the SQL Server too.  We talk about how to find those queries in the webcast listed at the bottom of this post.  Also, keep in mind that I said “Avoid ORDER BY”, not “Never use ORDER BY”.  I use ORDER BY myself too – but if I can avoid that work in the very expensive data tier, I’ll avoid it.  That’s what avoid means.
(This part here is where the MySQL and PostgreSQL guys start screaming about how you can avoid licensing costs altogether with open source databases.)  (This part here is where you would expect me to have a witty retort, but I don’t.  If you’re building a brand new app and you’re choosing a database, read my StackOverflow answer on which database handles the most load.)

1. SQL Server has built-in zero-impact instrumentation tools.

SQL Server’s dynamic management views (DMVs) can tell you all kinds of killer stuff like:
  • Which SQL statements are causing the most load on your server
  • Which indexes are wasting space and slowing down inserts/updates/deletes
  • How fast storage is responding to requests on a database-by-database level (and even more fine-grained than that)
  • Where your server’s bottleneck is, like CPU, disk, network, locking, etc
All you have to know is where to look, and we’ll show you.

How to Make Slow SQL Servers Go Faster

How to Make Slow SQL Servers Go Faster

You’re a developer, DBA, or sysadmin stuck with a Microsoft SQL Server that isn’t going fast enough. We’ll show you how to make it fly with these steps:
  1. Measure how fast the server is going now.
  2. Performance tune the queries.
  3. Performance tune the indexes.
  4. Performance tune SQL Server’s settings.
  5. Performance tune the hardware.
  6. Finally, if you have to buy new servers, get a best practices setup checklist, and load test before going live.
Let’s get into the details of each step.

1. Measuring SQL Server’s Speed and Bottlenecks

Here’s our favorite techniques:
  • sp_BlitzFirst® – this free tool is like SQL Server’s speedometer. It shows you how fast SQL Server is going, and which wait types are preventing you from going faster.
  • SQL Server Perfmon Tutorial – how to set up Perfmon, what SQL Server Perfmon counters to track, and what the indicators mean.
  • Watch Brent Tune Servers – at Microsoft Ignite 2015, he took several SQL Server workloads, found the bottleneck, and then tweaked various settings for quick fixes.
Then, before you start changing things, ask what parts of the server you’re allowed to change. The Manager’s Guide to Tuning SQL Server gives you a simple checklist of what parts you can tweak, change, or replace completely.

2. Performance Tune the T-SQL Queries

If you’ve got an in-house app, and you’re allowed to tweak the queries, here’s a few ways to find the culprits:
Once you’ve found the queries you need to tune, here’s our favorite resources:

3. Performance Tune the Indexes

If you want to make the database structures more efficient so SQL Server works less, here’s our resources:
  • sp_BlitzIndex® – run this in your database for a free sanity check. It shows missing indexes, unused indexes, duplicates, heaps, and more, and explains why they’re killing your performance.
  • Indexing resources – our favorite blog posts and videos about tuning indexes.
  • SQL Server Book Recommendations – okay, the books aren’t free, but our recommendations are. We pick the best beginner, advanced, and performance tuning books.
  • Table partitioning – this feature seems like a great way to split up big tables, but it comes with some big gotchas.
  • How to tune indexes – and make SQL Server queries go faster.

4. Performance Tune SQL Server’s Settings

Surprisingly, a lot of SQL Server’s default settings can lead to bad performance. Let’s talk through what you need to do:
  1. Run sp_Blitz® on the server. It’s a free health check that catches many common performance bottlenecks.
  2. Check our SQL Server Setup Checklist – some simple configuration tweaks can get you 20-30% performance increases right from the start without spending any extra money.
  3. Review the Poor Performance Checklist – Jeremiah’s 5 things that fix bad SQL Server performance.
  4. Sysadmin’s Guide to SQL Server Memory – why isn’t sqlserver.exe using much memory? How can you tell if it needs more?

5. Performance Tune the Hardware

Hey, sometimes, throwing hardware at it is the fastest, easiest, and cheapest fix.

6. Building and Load Testing New SQL Servers

When you’re planning how big of a server to build, start with Brent’s video from Microsoft TechEd 2012, Building the Fastest SQL Servers. He explains the two common access patterns for databases (OLTP and data warehousing) and gives you resources on what hardware to pick for each.
For data warehouses, go to the Microsoft Fast Track Landing Page, but BEFORE YOU CLICK, this page has a lot of marketing stuff, and you need to know what you’re looking for. Focus on the Reference Configurations and Configuration Guides.  There are some vendor-neutral ones from Microsoft, and then there’s vendor-specific guides from Dell, HP, IBM, etc. You may have to scroll all the way to the bottom to see the vendor-specific stuff.
Once the server is built, here’s our favorite resources for load testing and setup:
  • How Fast is Your Storage? – I show you how to use the free CrystalDiskMark tool to do a fast load test on solid state drives and SANs.
  • How to Test SAN Performance with SQLIO – SQLIO is probably the worst-named tool in history: it has nothing to do with databases. It’s still really useful to test bottlenecks though.

sql question

https://career.guru99.com/top-50-sql-question-answers/