Search This Blog

Showing posts with label SQL party. Show all posts
Showing posts with label SQL party. Show all posts

Tuesday, July 12

T-SQL best practice SQL server

SQL server T-SQL best practice


This month’s TSQL Tuesday party is being hosted by Amit Banerjee (Blog | Twitter).
He is working in Microsoft and see here his online activities MSDN. The topic this month is T-SQL best practice. It’s the both DBA and developers Topic/Job.


Transact SQL is the best tool to improve the SQL server overall performance. You write your code efficiently then you no need to spend lots of money to buy extra hardware or RAM etc... So the coders/developers have to know the best practice. He or She does good job(writing correct codes) then, There is no work for the DBAs. There is lots of T-SQL best practice available on the Internet. Here I’m going to share mine. The first 15 bullet points are personally i have faced and tuned in my environment. I will have planned to write each posts for all 15 points with examples.


T-SQL best practice

DOs and Don’Ts



  •    Check/Validate your conditions out of the loop (While/If) statement.
  •    Specify correct data type size 

Ask the customer and specify the correct data type size what they really need. Otherwise you will end up with problem when your data grow larger.

E.X

For inserting 4 digit character don’t create VARCHAR (400) just create VARCHAR (4)

  •    Always write a query using seek able operator in your WHERE clause.


  •    Try to avoid Functions and date time functions when you write Quires i.e. Sproc .
  •    Avoid an index hints

Use the index hint if you really know it’ll improve the performance otherwise avoids it.

 

           SELECT * FROM emp  WITH (INDEX(ix_n)) WHERE n ='server'
  •    Don’t create too much indexes.

Create the indexes it’s really useful otherwise don’t. I have seen a table have 5 non clustered indexes with mostly same column definitions for particular table/SP and the table size is 4 GB and the indexes size are nearly 9 GB.

  •    Don’t use wildcard character at the beginning of the word

Always write seekable codes.



           SELECT ID FROM <table_name> WHERE NAME LIKE 'muthu%'

           SELECT ID FROM <table_name> WHERE NAME LIKE '%kumar'

  •    Normalize your tables

The best design will give the best performance. Normalize your tables before going to your project. I.e. my second point (Specify correct data type size etc...)

  •    Try to write set based queries minimize iteration/Cursors as much as you can.
  •    Use joins instead of sub query

Use the joins instead of sub query it’ll give better performance.

            SELECT * FROM TBL WHERE N IN (SELECT N FROM TBL1)

            SELECT A.* FROM TBL A JOIN TBL1 B

            ON (A.N=B.N)

  •    Use the batch statement if you’re going to delete huge number of records from the table

Write a batch statement if you’re going to delete huge records it’ll minimize the log file size. Most importantly inform to the DBA team before going to delete otherwise we will get a call from customer the DB has been down.

  •    Use ANSI-Standard Join clauses instead of the old style joins

            SELECT e.no,ed.name

            FROM employee e, employee_details ed,

            WHERE ed.name ='muthu'

            SELECT e.no,ed.name

            FROM employee e INNER JOIN employee_details ed

            ON emp.no=ed.no

            WHERE ed.name ='muthu'

  •    Test your data and the indexes after migrating one server to another server especially 2000  to 2005/2008.
  •    Create a filter index if you know the query often fetch the unchanged data from a big table.

A well-designed filtered index can improve query performance, reduce storage costs, and reduce maintenance too. 

           
           CREATE NONCLUSTERED INDEX I_fi_test

           ON dbo.test (ID,joinDate,Relievingdate)

           WHERE joinDate > '2001/01/01';

  •   Use NO LOCK hint if you have no problem with dirty reads.


  •   Create a clustered index (Primary key) for all mostly used tables

Try to create a clustered index for all the tables. I had seen most of my big tables are not defragmented because of the tables are created primary key with NON clustered index. I asked reason they told we don't know who has created (If you really don't know tuning ask the DBA).

  •    Use SELECT 1 instead of SELECT *

Use SELECT 1 when you’re checking the records are available in your table.

IF EXISTS (SELECT 1 FROM <table_name> WHERE id=100)

  •    In general rule retrieve the columns as you want don’t use “SELECT *”

Write a required column in SELECT statement as you want don’t SELECT all the columns.



E.X

           SELECT * FROM <table_name>

           SELECT c1,c2 FROM <table_name>

  •    Don’t write your procedure name starting with “SP_”

SQL server searches all the producers one by one starting with “SP_” in the database including system procedure then finally found yours. It causes minor overhead.

  •    Use WHERE condition as much as possible

            SELECT * FROM emp WHERE Name ='SERVER'

  •    Use TRY-Catch for error handling

See my procedure has written using TRY-Catch.

  •    Try to avoid dynamic SQL

It'll generate SQL injections if you're not write it effectively.


  •    Always write your codes/SProcs with descriptions and comments.

I hope this best practice will help for the developers/coders. Thanks to Amit has given this great topic.

Tuesday, June 14

SQL server Disaster Recovery plan

SQL server Disaster Recovery Options

This month’s TSQL Tuesday party is being hosted by Allen Kinsel (Blog | Twitter) and the topic this month is disaster recovery. It’s a DBA Topic/Job.




The disaster recovery is really “It depends”.

What is disaster recovery?

Disaster recovery is called DR. hmmm...
Disaster recovery is the best option for the business to minimize their data loss and downtime. The SQL server has number of native options. But, as I already told everything is depends upon your recovery time objective RTO and recovery point objective RPO.

That is how much data can you afford to lose? How long can you wait? How much money do you ready to spend?

If you have answer for these questions then, you can choose your best DR plan.

Who knows what will happen tomorrow?
I have seen most of the company don’t have at least simple DR setup (My previous company too), Like keep the backup in safe DR site. The reason is they didn’t face any disaster yet now.

Truncating and deleting the 'critical tables' also consider big disaster.so guys keep the backup,backup and backup. Before going to setup your DR plan ask the questions to DR Guru's via online (Like twitter, SSC and MSDN) and also read the DR Guru Paul Randal's white papers it will help.

I have recommended to watching the brentozar‘s DR video. Its amazing video and it covers the basics DR plan.

Seriously i have no idea about Geo cluster.Thanks to @sqlsoldier and @SQLpoolboy  for their twitter reply about the Geo cluster method.

For understandable I will classify the disaster recovery (DR) into three types.I am not going in depth just I will cover the basics of high availability HA and disaster recovery DR.

1. Data center disaster
2. Server/Drive disaster
3. Database/Drive disaster
Backup/Restore

Backup restore is common method its simple(less expensive) and good option for disaster recovery but it’s depends upon your backup strategy and recovery model. The backup restore will work for all the above three DR methods. The DBA job is keep the data as much as safe and application availability(Zero down time). That is test and keep your database backups safely try to automate the backup test and move them automatically to DR site.


The backup restore does not good choose for big databases (TB) in case of any disaster, because the restoration takes more time to bring the database online.However you can speed up the backup restoration using the following SQL native features.
Clustering

Clustering is a combination of one or more servers it will automatically allow one physical server to take over the tasks of another physical server that has failed. 

Clustering is best option it provides a minimum downtime (like 5 minutes) and data loss in case any data center(Geo) or server failure.
  • Clustering needs extra hardware/server and it's more expensive.
Database mirroring

Database mirroring introduced in 2005 onwards. Database Mirroring maintain an exact copy of a database on a different server. It has automatic fail over option and mainly helps to increase the database availability.
  • Database mirroring only works FULL recovery model.
  • This needs two instances.
  • Mirror database always in restoring state.
For more details read Robert L. Davis's Pro SQL Server 2008 Mirroring book.
 
Log shipping

Log shipping is the process of automating the full database backup and transaction log on a production server and then automatically restores them on to the secondary (standby) server.


  • Log shipping will work either Full or Bulk logged recovery model.
  • You can also configure log shipping in single SQL instance.
  • Stand by database can be either restoring or read only (standby).
  • The manual fail over is required to bring the database online.
  • Some data can be lost (15 minutes).


Peer-to-Peer Transactional Replication

Peer-to-peer transactional replication is designed for applications that might read or might modify the data in any database that participates in replication. Additionally, if any servers that host the databases are unavailable, you can modify the application to route traffic to the remaining servers. The remaining servers contain same copies of the data.






  • Peer-to-peer replication is available only in SQL Server 2005
    enterprise edition.

    For more looks the Microsoft link: http://msdn.microsoft.com/en-us/library/ms151196(v=sql.90).aspx











  • Note: Database mirroring,logshipping and replication also treated as DR if it's located in different data center. We can use both the HA and DR combination to setup the disaster recovery.

    Again everything is "It Depends". Read Paul Randal's white papers before going to configure the DR.

    Wednesday, May 4

    Meme Monday: I Got 99 SQL Problems And the Disk Ain’t One

    Meme Monday: I Got 99 SQL Problems And the Disk Ain’t One

        I am a bit late to this party. I have seen this post from Grant's blog now only. Thanks to Thomas LaRock such a great topic.

    Here is mine:
    1.Too many indexes on a table
    Developers didn't check their codes and an execution plan too. They just go and create the indexes (Duplicate indexes too).

    2.Improper memory setting
    Improper min,max memory setting especially for 64-bit servers.

    3.Improperly configured transaction log
    Whenever i got new server, I have seen log backups are failed because, there is no current database backup.

    BACKUP LOG cannot be performed because there is no current database backup.

    4.Poor backup strategy
    No SLA for the backups (data) from the customer/Manager.

    5.Company Didn't believe the DBAs
    The important (wealth) project was not running fine. The company suddenly called a consultants even they didn't consider the DBA but, finally the problem solved by the DBA.

    6.Rebuilding an index runs first then shrinks the database
    I have seen in most of the servers a rebuild jobs runs first then shrink database runs next.

    7.Linked server issue
    Often i got call from teams the linked server was not working fine. Its return timeout error.

    8.Poorly written queries using a Non SARG-able

    9.No Standard operating procedures