Something about Indexes

Source: Internet
Author: User

1. The two tables have the same ID in connection query. One ID is int type and the other ID is nvarchar.

Select a. Name, sum (B. chengji) from a, B where a. ID = B. ID and A. name not in ('0', null)

Time consumed: 32 seconds

After the guidance of csdn

Improvement:

Select a. Name, sum (B. chengji) from a, B where a. ID = B. ID and A. Name <> '0' and A. Name is not null

Time consumed: 32 seconds

Found so slow

Later, Baidu and Google studied the table structure and found that the IDs and character types in the two tables were slow.

Id nvarchar --> ID int

Then, what?

Select a. Name, sum (B. chengji) from a, B where a. ID = B. ID and A. Name <> '0' and A. Name is not null

Time consumed: 200 milliseconds

////////////////

1. Function for converting characters: Cast (ID as INT) takes precedence over convert (INT, ID)

2. Try not to use not in

3. If there are many columns, use no texists

4. It is faster to add a clustered index to the column data compared with the two tables. (is the clustered index equal to the clustered Index ?)

5. Pay attention to details

---------------- Cute split line --------------

About composite indexes

I have a view like this.

Select. name,. sex,. address, sum (B. qw1), sum (B. qw2), sum (C. qw3), sum (D. qw4) from a, B, c, d Where. id = B. ID and. id = C. ID and. id = D. id group by. name,. sex,. address

Composite indexes should be used here

Because name is the most commonly used sex, address is the second

Therefore, to create a composite index, add a composite index and set an independent index respectively.

Create index ix_name_sex_address _ table on (name, sex, address)
Create index ix_name _ table on table (name)
Create index ix_sex _ table on table (sex)
Create index ix_address _ table on (address)

Reference: http://topic.csdn.net/t/20040330/13/2903545.html

Use exists or

Select * From spt_values where number in (1, 2, 4) Select SPT. * From spt_values as SPT join (select number = 1 Union all select number = 2 Union all select number = 4) B on SPT. number = B. number -- can be abbreviated as -- select SPT. * From spt_values as SPT join (-- select number = 1 -- Union all select 2 -- Union all select 4 --) B on SPT. number = B. number

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.