SQL Server Performance Optimization

Source: Internet
Author: User

Today, a netizen came to me to give him the original system database optimization query speed, personal summary of several points to the optimization of SQL Server

1, SQL query statement optimization, such as: can use external connection query out as far as possible do not use the internal connection ..., these are not nonsense, if I use this to optimize for him, then I will have to understand his system of business or something, spend more time, it is not worth. So plan to optimize it by the following 2 points:

2, to add an index to the table, according to the actual system

3, after the system test passed, the foreign KEY constraint delete (because he is already running the system, if not using foreign keys to perform some triggers, this will certainly work)

SQL allows you to query the foreign key name of the specified table, eg. querying foreign keys in the SalesOrder table in db

Select Name

From Sys.foreign_key_columns F joins sys.objects O on f.constraint_object_id=o.object_id
where f.parent_object_id=object_id (' [dbo].[ SalesOrder] ')

This allows you to see the foreign keys in the SalesOrder table (if any)

But if so, I do not have to query every table, Google to find a solution, with a SQL statement to do all, as follows:

--using Sysreferences--Delete foreign keyDECLARE @SQL VARCHAR( -)DECLARECur_fkCURSORLOCAL forSELECT 'ALTER TABLE ['+ object_name(Fkeyid)+ '] Drop constraint' + object_name(ConstID) fromsysreferences--Delete all foreign keysOPENCUR_FKFETCHCur_fk into @SQL while @ @FETCH_STATUS =0BEGINEXEC(@SQL)FETCHCur_fk into @SQLENDCLOSECUR_FKdeallocateCur_fk

Doing this directly will delete all foreign key constraints in the DB

SQL Server Performance Optimization

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.