A way to solve the slow speed of SQL

Source: Internet
Author: User
Tags insert
Solve the speed below is my years of experience in the summary: Network programming and database to deal with. Dealing with a database is always about getting in touch with SQL. How to make your SQL run faster, this article introduces several effective methods: Method One, try to use complex SQL instead of a simple stack of SQL. For the same transaction, a complex SQL completion is more efficient than a bunch of simple SQL finishes. When you have multiple queries, be good at using joins. Ors=objdbc.execute (' select * from books ') for (;!ors.eof; Ors.movenext ()) {Ors2=objdbc.execute (' select * from Authors whe RE authorid= ' +ors (' Authorid '). value+ '); Response.Write (ORs (' Title '). value+ ' +ors2 (' Name ') + ' <br> '); is slower than the following code: Ors=objdbc.execute (' SELECT books.title,authors.name from Books JOIN Authors on authors.authorid= Books.authorid '); for (;!ors.eof; Ors.movenext ()) {Response.Write (ORs (' Title '). value+ ' +ors (' Name ') + ' <br> '); Try to avoid using updatable Recordset Ors=objdbc.execute (' SELECT * from Authors WHERE authorid=17 ', (some flags)); ORs (' Name ') = ' Karl Karlsson '; Ors.update (); Slower than the following code: Objdbc.execute (' UPDATE Authors SET name= ' Karl Karlsson ' WHERE authorid=17 '); Method III, when updating the database, try to use batch update to make all of the SQL into a large batch of SQL, and run; This is much more efficient than updating data one by one. This also satisfies your need for transaction processing: (in JScript) strsql= '; strsql+= ' SET xact_abort on\n '; strsql+= ' BEGINTransaction\n '; strsql+= ' INSERT into Orders (ordid,custid,orddat) VALUES (' 9999 ', ' 1234 ', GETDATE ()) \ n '; strsql+= ' INSERT into Orderrows (ordid,ordrow,item,qty) VALUES (' 9999 ', ' + ', ' G4385 ', 5) \ n '; strsql+= ' INSERT into Orderrows (ordid,ordrow,item,qty) VALUES (' 9999 ', ' r ', ' G4726 ', 1) \ n '; strsql+= ' COMMIT transaction\n '; strsql+= ' SET xact_abort off\n '; Objdbc.execute (strSQL); Where the SET xact_abort off statement tells SQL Server to cancel a transaction that has been completed if an error is encountered during the following transaction processing. Method Iv. Database Indexing Method v. Avoid making the text field too large when the value of the string is not fixed, it is better to use varchar than char. I've seen an example of a program where the field is defined as text (255), but his value is often only 20 characters. This data table has 50k records, so that the database is very large, large database is necessarily slower.   


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.