Joint primary key (joint index) Index Analysis in SQL Server (MySQL)

Source: Internet
Author: User

Joint primary key (joint index) Index Analysis in SQL Server (MySQL)

Some people have recently asked this question, but they have not studied the specific use logic of the joint index before. They have read many articles and obtained some conclusions after tests.

Test environment: SQL Server 2008 R2

The test result is similar to that of the MySql joint index query mechanism. It can be considered that MySQL is the same principle.

========================================================== ================

The concept of joint index: when several fields in the system are frequently to be queried, and the data volume is large, reaching the million level, multiple fields can be indexed.

Rules:

1. The leftmost principle. Based on the index field, and from left to right (where field is very important, from left to right)

2. Or does not use the Union Index

3. the query field in the where statement contains all index fields. The field order is irrelevant and can be sorted randomly.

4. If the data volume is small, indexes are generally not used. The database itself determines whether to use indexes automatically.

========================================================== ==================

Test script (some scripts from other authors ):

/* Create a test data table */create table MyTestTable (id varchar (10) not null, parent varchar (40) not null, addtime datetime default (getdate ()), intcolumn int default (10), bitcolumn bit default (1 )) go/* add tens of thousands of random string test data in minutes */declare @ count int = 3557643 declare @ I int = 0 declare @ id varchar (10), @ parent varchar (40) while (@ I <@ count) beginselect @ id = left (newid (), 10) if (@ I % 20 = 0) beginselect @ parent = left (newid (), 40) endinsert MyTestTable (id, parent) values (@ id, @ parent) select @ I = @ I + 1 endgo
/× Query test without index creation ×/
Declare @ beginTime datetime = getdate () declare @ elapsedSecond int = 0 select * from MyTestTable where parent = 'f92d6a9d-hangzhou' and id = 'fd1_7f4-1' select @ elapsedSecond = DATEDIFF (MICROSECOND, @ beginTime, GETDATE () print 'query data consumption in microseconds when no index is created 'print @ elapsedSecondselect @ beginTime = GETDATE () select * from MyTestTable where parent = 'f535c18f-BD48-4D45-88DF-9653BB9B422D 'select @ elapsedSecond = DATEDIFF (MICROSECOND, @ beginTime, GETDATE ()) print 'data consumption in the second column when no index is created 'print @ elapsedSecond
/* Create Index */alter table MyTestTable add constraint PK_id_parent primary key (id asc, parent asc)/* query after index creation */declare @ beginTime datetime = getdate () declare @ elapsedSecond int = 0 select * from MyTestTable where parent = 'f92d6a9d-hangzhou' and id = 'fd1_7f4-1' select @ elapsedSecond = DATEDIFF (MICROSECOND, @ beginTime, GETDATE () print 'query data consumption in microseconds during index creation 'print @ elapsedSecond select @ beginTime = GETDATE () select * from MyTestTable where parent = 'f92d6a9d-4E9E-4980-8B46-8AD938CEDCB4 'select @ elapsedSecond = DATEDIFF (MICROSECOND, @ beginTime, GETDATE ()) print 'data consumption in the second column after indexing 'print @ elapsedSecond
/* Index use test conclusion */select * from MyTestTable where id = 'fd1_7f4-1' -- use the index select * from MyTestTable where id = 'fd1_7f4-1' and parent = 'f92d6a9d -Comment 'and intcolumn> 0 -- use the index select * from MyTestTable where id = 'fd1_7f4-1' and intcolumn> 0 and parent = 'f92d6a9d-Comment' -- use the index select * from myTestTable where id = 'fd1_7f4-1 'and intcolumn> 0 -- use the index select * from MyTestTable where parent = 'f92d6a9d-hangzhou' and id = 'fd1_7f4-1' -- use the index select * from MyTestTable where parent = 'f92d6a9d-hangzhou' and intcolumn> 0 -- select * from MyTestTable where parent = 'f92d6a9d-hangzhou' or id = 'fd1_7f4-1' -- no index

If you have any questions, please leave a message!

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.