SQL Server database query optimization skills

Source: Internet
Author: User
Tags sca ide hard drive

SQL Server database query tips:

Problem type: When the ACCESS database field contains a Japanese Katakana or other unknown characters, the query will prompt a memory overflow.

Solution: Modify the query statement.

 
 
  1. SQL = "select * from Ada where Alice like '%" & abigale & "% '"
  2. Change
  3. SQL = "select * from Ada"
  4. Rs. Filter = "Alice like '%" & abigale & "% '"

SQL Server database query Tip 2:

Problem type: how to use a simple method to implement multi-Keyword queries similar to Baidu (Separate multiple keywords with spaces or other symbols ).

Solution:

 
 
  1. '// Use spaces to separate query strings
  2. Ck = Split (abigale ,"")
  3. '// Obtain the split quantity
  4. Sck = ubound (CK)
  5. SQL = "select * Ada where"
  6. Query in a field
  7. ForI = 0ToSck
  8. SQL = SQL & tempjoinword &"("&_
  9. "Alice like '" & CK (I) & "% ')"
  10. Tempjoinword = "and"
  11. Next
  12. Query both fields
  13. ForI = 0ToSck
  14. SQL = SQL & tempjoinword &"("&_
  15. "Alice like '" & CK (I) & "%' or "&_
  16. "Alice1 like '" & CK (I) & "% ')"
  17. Tempjoinabigale = "and"
  18. Next

SQL Server Database Query Technique 3: Several skills to improve query efficiency

1. Try not to use or. Using or will cause full table scanning, which will greatly reduce the query efficiency.

2. After practical verification, charindex () does not improve the query efficiency better than the preceding % like, And charindex () will make the index ineffective (referring to sqlserver database)

3. Alice like '% "& abigale &" %' will disable the index

Like '"& abigale &" %' will make the index take effect (remove the % symbol above)

(Sqlserver database)

4. Differences between '% "& abigale &" %' and '"& abigale &" %' during query:

For example, your field content is

'% "& Abigale &" %': All strings are matched, and the results are displayed no matter whether you check the "Mart" or "SCA.

'"& Abigale &" %': only matches the preceding string. For example, if "Mart" is queried, the result is displayed only when "SCA" is queried.

5. field extraction should follow the principle of "how much is required and how much is requested" to avoid "select *". Try to use "select Field 1, Field 2, Field 3 ........ ". Practice has proved that the data extraction speed will be improved for every few fields extracted. The speed of improvement depends on the size of the discarded field.

6. Order by is the most efficient sorting by clustered index columns. Only one clustered index can be created for one sqlserver data table. The default value is ID. You can also change it to another field.

7. Create an appropriate index for your table. Creating an index can increase your query speed by dozens or hundreds of times. (Sqlserver database)

The following is a query efficiency analysis between index creation and no index creation:

SQL Server Index and query efficiency analysis.

Table news

Field

ID: Automatic ID

Title: article title

Author: Author

Content: Content

Star: Priority

Addtime: Time

Record: 1 million

Test Machine: P4 2.8/1 GB memory/IDE Hard Drive

 

Solution 1:

Primary Key ID. The default value is clustered index. No other non-clustered indexes are created.

 
 
  1. select * from News where Title like '%"&abigale&"%'
  2. or Author like '%"&abigale&"%' order by Id desc

Fuzzy search by field title and author, sorted by ID

Query time: 50 seconds

Solution 2:

Primary Key ID. The default value is clustered index.

Create a non-clustered index on title, author, and star

 
 
  1. select * from News where Title like '"&abigale&"%'
  2. or Author like '"&abigale&"%' order by Id desc

Fuzzy search by field title and author, sorted by ID

Query time: 2-2.5 seconds

Solution 3:

Primary Key ID. The default value is clustered index.

Create a non-clustered index on title, author, and star

 
 
  1. select * from News where Title like '"&abigale&"%'
  2. or Author like '"&abigale&"%' order by Star desc

Fuzzy search from the title and author fields, sorted by star

Query time: 2 seconds

Solution 4:

Primary Key ID. The default value is clustered index.

 
 
  1. Create a non-clustered index on title, author, and star
  2. Select*FromNewsWhereTitle like '"& abigale &" %'
  3. Or author like '"& abigale &" %'

Fuzzy search from the title and author fields. unordered query time: 1.8-2 seconds

Solution 5:

Primary Key ID. The default value is clustered index.

Create a non-clustered index on title, author, and star

 
 
  1. Select*FromNewsWhereTitle like '"& abigale &" %'
  2. Or
  3. Select*FromNewsWhereAuthor like '"& abigale &" %'

Retrieved from the title or author field. unordered query time: 1 second

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.