sql management for it users - accesso technology group · 2011-09-28 · • sql server transaction...

47
SQL Management for IT Users Intermediate Level John Tappero, Software Engineer Andrew Glasfeld, Software Engineer

Upload: others

Post on 10-Jul-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

SQL Management for IT Users Intermediate Level

John Tappero, Software Engineer Andrew Glasfeld, Software Engineer

Page 2: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

2

• Overview of SQL Server 2005/2008. • SQL Server tools. • Common administrative tasks. • Tips and diagnostic advice for reliability and performance. • Scaling SQL Server.

Class objectives

Page 3: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

3

• It is a relational database management system (RDBMS). • Maintains valid relationships between data. • Ensures that data is stored correctly. • Can recover data when the system fails.

What is SQL Server?

Page 4: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

4

• It uses “client/server” architecture. • This separates the workload into tasks that run on server computers

and those that run on client computers. • The “client” is responsible for business logic and presenting data to

the user. • SQL Server (the “server”) manages the databases and allocates the

available server resources.

What is SQL Server? (cont’d)

Page 5: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

5

• Microsoft SQL Server uses Transact-SQL (T-SQL). • SQL is a set of commands to retrieve, modify or delete information.

The American National Standards Institute (ANSI) has defined the standards for SQL.

• T-SQL is Microsoft's version of SQL. • T-SQL supports the ANSI SQL specification, but more.

What is SQL Server? (cont’d)

Page 6: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

6

• What’s new in SQL Server 2005 • SQL Server Management Studio. • SQL Server 2005 SP1 added Database Mirroring.

• What’s new in SQL Server 2008

• Transparent data encryption. • Enhanced performance and data auditing. • Enhanced database mirroring. • Performance data collection. • Backup compression. • Resource governor.

What is SQL Server? (cont’d)

Page 7: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

7

• What is SSMS? • A utility to perform system management tasks on SQL Server and its

databases. • What can you do with SSMS?

• Set up SQL Server login accounts. • Back up your main SiriusSQL database. • Restore your main SiriusSQL database. • Detach your main SiriusSQL database. • Attach your main SiriusSQL database. • Export SQL Server logs. • Query Window: Run any valid T-SQL code (scripts).

SQL Server Management Studio (SSMS)

Page 8: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

8

• Backups are the second most important task. • Restoring a backup is the most important. • Note: The following are only suggestions.

Better backups – understanding the details

Page 9: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

9

• SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an LDF extension.

• File Groups are a database that is broken up into multiple physical files. Each of these have an NDF extension.

• Siriusware broke up the MDF into three File Group files called SiriusSQL.MDF, SiriusSQL_Transact.NDF, SiriusSQL_Logs.NDF

• The transaction log is still SiriusSQL.LDF.

Transaction Logs and File Groups

Page 10: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

10

• Don't confuse SQL Server Transaction Logs with the files SiriusSQL_Transact.NDF and SiriusSQL_Logs.NDF.

• Don’t confuse SQL Server Transaction Logs with the tables in the SiriusSQL database called “transact”, “gst_log” and “acc_log”.

Transaction Logs and File Groups (cont’d)

Page 11: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

11

• The Recovery Model is set as a property of each database. • SQL Server 2000 defaulted to the "Simple" recovery model. • SQL Server 2005 and 2008 defaults to the "Full" recovery model. • What does Microsoft think is best for you today?

Types of Recovery Models

Page 12: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

12

• Transaction Log is automatically pruned (truncated) immediately after a transaction is committed (every basic INSERT, UPDATE and DELETE statement).

• This means that the LDF file never grows. • This is the default for your SiriusSQL database.

Simple Recovery Model

Page 13: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

13

• So why doesn't everyone use Simple? • Because you are more at risk in the event of failure. • Choose Simple to keep your life simple.

Simple Recovery Model (cont’d)

Page 14: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

14

• This is the same as Full recovery model except bulk operations are not logged.

• Code in Siriusware's software does not perform bulk operations, so Bulk Logged is essentially the same as Full Recovery.

Bulk Logged Recovery Model

Page 15: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

15

• This logs all database activity and KEEPS it in the log until a transaction log backup (or a transaction log truncate) takes place.

• Recover data right up to the moment of failure. • First restore your first full backup, and then restore each transaction log

backup.

Full Recovery Model

Page 16: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

16

• Requires periodic (i.e. nightly) full database backups AND • Requires frequent (i.e. hourly) transaction log backups. • Don’t use this model unless you have a full-time DBA on staff.

Full Recovery Model (cont’d)

Page 17: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

17

• Is a complete snapshot backup of the database. • They can take up as much space as the MDF and NDF files.

Full Database Backup

Page 18: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

18

• Backs up only what is different since the last Full Backup. • They grow larger throughout the day as more data changes. • After the next full backup, the backups begin to be small again. • They are small and happen quickly, so you can do them frequently. • Allows restoration much closer to the time of failure.

Differential Backup

Page 19: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

19

• Minimal fuss backup scheme for a small or medium sized venue: • Perform a Full backup every night. • Perform Differential backups every hour starting at 9:00am until an hour

after sales points are closed. • Interactive example

Backup suggestion

Page 20: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

20

• First restore your most recent Full backup • Then restore the latest Differential backup (if you performed any

Differential backups). • Two scenarios – one where the database name appears in the list of

databases, and one where the name doesn’t appear.

Restoring a backup

Page 21: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

21

• Simply right-click on the database name, tasks, Restore Database. • It will conveniently select the most recent Full backup as well as the most

recent Differential backup to restore.

Restoring when database name is available

Page 22: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

22

• Right-click on the word "databases" and choose "restore databases". • First, enter the new database name (i.e. SiriusSQL). • Choose "From Device" instead of the "Database“. • You must select a "device" to restore from. • Add the full backup file to the device list. • IMPORTANT: If you also have a Differential backups, select "Leave the

database non-operational" in the Options tab. • Also, you may change the path to where you are restoring, if the path has

changed. You must change the path for all four files in the backup set. • Click OK. Now restore the differential (if any). • Repeat the process, but this time for the differential. • Note, you may need to modify all of your workstation ODBC connections.

Restoring when the name is not available

Page 23: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

23

• You can set up backups in a maintenance plan as well. • Used to reorganize the indexes so performance is increased. • Also used to check the database integrity. • Use the Maintenance Plan Wizard to easily create maintenance plans. • Additionally, you can use the Maintenance plan designer in SQL Server

2005.

Maintenance Plans

Page 24: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

24

• Note: SQL Server 2005 requires you to have the SQL Server Integration Services (SSIS).

• In SQL Server 2005, select "New Maintenance Plan Wizard" in 2005, unless you are an experienced user.

• Interactive example

Maintenance Plans (cont’d)

Page 25: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

25

• Windows Authentication Mode or Mixed Mode. • Windows Authentication Mode -> only logins that use Windows security

credentials are supported. • Mixed Mode -> Windows Authentication mode PLUS its own set of SQL

Server login accounts. • Siriusware modules use Mixed Mode authentication. • Use Windows Authentication Mode for other user access.

Managing User Security

Page 26: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

26

• Uses “trusted connections” to SQL Server. • SQL Server will not recognize users or groups that you have deleted and

recreated in Windows. • Trusted domains also work with SQL Server. • First create the Windows groups and users. • Need Windows administrator rights. • Use SSMS to grant access.

Windows Authentication Mode

Page 27: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

27

• SQL Server manages it’s own user list. • No SQL Server login account = no access. • Use SSMS to create a new user under the Security / Logins folder.

SQL Server Mixed Mode

Page 28: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

28

• This is a special login account, with a password of “sirius”. • It has only “Public” permissions to the SiriusSQL database. • Nobody should ever use the “sirius” user account! • Don’t change access rights to the “sirius” login account! • Need access to the SiriusSQL database? Set up your own access.

The “sirius” login account

Page 29: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

29

• Commonly Used Problem Solving Tools • Profiler and the default trace • Notepad • Event Viewer

SQL Server Troubleshooting

Page 30: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

30

• Information Gathering Phase • Step 1: Gather the facts. • Step 2: Test in different environments. • Step 3: Review the SQL Server Error Log and Windows Event logs. • Step 4: Review the default trace. • Step 5: Review your own change log and Siriusware release notes.

• Analysis Phase

• Review all the data you collected.

SQL Server Troubleshooting

Page 31: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

31

• Why is my SQL Server running slow? • Your server or network is not properly scaled for your workload. • Backups or other maintenance operations occurring during business

times. • User queries from 3rd party products (i.e. Excel).

• Users may not be experienced using these tools.

• Can create locked rows, pages or tables.

• These tools are sometimes file-based or table-based.

SQL Server Performance

Page 32: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

32

• Screen savers. • Network issues. • Report Manager reports with broad criteria. • No Limits on Max4Sale items. • Circular referencing for modifiers on modifiers. • Terminal Services, or Citrix software.

Some reasons for server slowness (cont’d)

Page 33: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

33

• Given all this, how do you figure out what the problem is? • Observation and deduction. • Try to tie your symptoms to an event or pattern.

Why is my SQL Server running slow?

Page 34: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

34

• So, what if there is no discernable pattern? • You must then act while the problem is occurring. • Running a trace can help.

Why is my SQL Server running slow?

Page 35: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

35

• Task Manager • Processes tab: Sort by the CPU column descending. • Processes tab: Add a “CPU Time” column. • Performance tab: “Commit Charge total” versus “Physical Memory

total”. • Performance tab: “Available Physical Memory” < 4MB.

Useful methods for finding bottlenecks

Page 36: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

36

• Disk Reads vs. Disk Writes • Is your disk I/O mostly reads or mostly writes? • Knowing the ratio of reads to writes of your server can help. • Heavy on writes -> avoid RAID 5 and use RAID 10 instead. • Heavy on reads -> RAID 5 is good. • Processes tab: Add “I/O Read Bytes” and “I/O Write Bytes” columns. • Give it 2 to 4 days to log the reads and writes. • If reads are roughly double or more than the writes -> RAID 5. • If writes are double or more than the reads -> RAID 10.

Useful methods for finding bottlenecks

Page 37: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

37

• The Performance Monitor • Under Windows Administrative Tools, run the Performance Monitor.

Add and view the following counters: • SQLServer: Buffer Manager: Buffer cache hit ratio. Should be at or above 90.

• SQLServer: Buffer Manager: Free pages. Should not have a sustained value of 4 or less for more than 2 seconds.

• SQL Server: Buffer Manager: Page Life Expectancy. Should be above 350.

• Memory: Available Bytes. Should be 10MB or more.

• SQLServer: SQL Statistics: Batch Requests/Sec. Over 1000 batch requests per second indicates a very busy SQL Server, and could mean CPU bottlenecks. But this is a relative number, and the bigger your hardware, the more batch requests per second SQL Server can handle.

Useful methods for finding bottlenecks

Page 38: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

38

• Current SQL Server Activity • This shows you who is on your system and what is currently

happening within SQL Server. • sp_who2 and sp_lock3

• Determines who is on your system, what (if any) locks they have on your data and how much CPU time they used.

• Sp_who2 is built into SQL Server. • Sp_lock3 is a user created enhancement to Microsoft’s sp_lock2

stored procedure.

Useful methods for finding bottlenecks

Page 39: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

39

• Profiler • Profiler is a Graphical Interface to "SQL Trace". • Traces "record" SQL activity. • Leave it running to “catch” the culprit.

Useful methods for finding bottlenecks

Page 40: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

40

• Keep the database statistics up to date • SQL Server uses a cost-based optimizer. • Statistics are used to create an execution plan for a particular query. • Statistics are maintained on each table. • Use the UPDATE STATISTICS command or the sp_updatestats system

stored procedure.

Useful methods for finding bottlenecks

Page 41: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

41

• Be sure the “sirius” user account login has only “Public” permission to SiriusSQL and SiriusSQL_Training.

• Limit user access to the live data and don't make a “general” login. • If you have web applications that access the data, be sure to give them

their own login too for the same reason (don’t use the “sa” login).

Preventing issues with a little security

Page 42: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

42

• Before considering scaling SQL Server, first optimize your applications and queries.

• This part is already done for you. • If things are still slow, scaling your SQL Server may be your next option. • There are two ways to scale SQL Server. You can “scale up” or “scale out”.

Scaling your SQL Server

Page 43: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

43

• Invest in better/faster hardware. • Good approach if your bottlenecks are hardware related. • Memory bottlenecks may be resolved by adding additional memory or by

upgrading existing memory. • SQL Server 2005 -> remove the 2GB Virtual Address Space limit by using

the /3GB switch in the boot.ini file. • Consider faster storage subsystems, multiple disk controllers, or

implementing RAID.

Scaling Up

Page 44: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

44

• Multiple computers with data partitioned or replicated across them. • Requires much more planning than scaling up. • See http://blogs.msdn.com/sqlcat/archive/2008/06/12/sql-server-scale-

out.aspx for a list of good articles on scaling out.

Scaling Out

Page 45: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

45

1. Optimize the application before scaling 2. Address historical and reporting data 3. Scale up 4. Scale out when scaling up does not suffice

Should I scale up or scale out?

Page 46: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

46

• Microsoft SQL Server Books On-Line and Transact-SQL help files. • http://www.sql-server-performance.com • http://www.learnitfirst.com/Course/152/SQL-Server-2005-DBA.aspx • http://www.sqlservercentral.com • http://www.simple-talk.com/sql

• Thanks for attending!

Other resources

Page 47: SQL Management for IT Users - accesso Technology Group · 2011-09-28 · • SQL Server Transaction Logs record all database activity as it comes through. Transaction Logs have an

Thank You!