3 principles for adding a MySQL index

Source: Internet
Author: User
Tags count create index end execution insert sql mysql mysql index

One, the importance of indexing

Indexes are used to quickly find rows that have a specific value in a column. Without indexing, MySQL must start with the 1th record and read through the entire table until the relevant rows are found. The larger the table, the more time it takes. If the query column in the table has an index, MySQL can quickly reach a location to find the middle of the data file, there is no need to look at all the data. Note that if you need to access most rows, sequential reads are much faster because we avoid disk searches at this time.

If you use the Xinhua dictionary to find "Zhang" the Chinese character, do not use the table of contents, you may want to take the first page of the Xinhua dictionary to find the last page, it may cost two hours. The thicker the dictionary, the more time you spend. Now you use the table of contents to find "Zhang" the Chinese character, Zhang's first letter is z,z beginning of the Chinese characters from more than 900 pages, with this clue, you look for a Chinese character may only a minute, this shows the importance of the index. But the more the index is built, the better, of course not, if the catalogue of a book is divided into several levels, I think you will faint.

Ii. preparations for the work

 
 
  1. Prepare two test sheets
  2. mysql> CREATE TABLE ' test_t ' (
  3. -> ' id ' int (one) not NULL auto_increment,
  4. -> ' num ' int (one) not NULL default ' 0 ',
  5. -> ' d_num ' varchar () not NULL default ' 0 ',
  6. -> PRIMARY KEY (' id ')
  7. ->) Engine=myisam DEFAULT Charset=utf8 auto_increment=1;
  8. Query OK, 0 rows affected (0.05 sec)
  9. mysql> CREATE TABLE ' test_test ' (
  10. -> ' id ' int (one) not NULL auto_increment,
  11. -> ' num ' int (one) not NULL default ' 0 ',
  12. -> PRIMARY KEY (' id ')
  13. ->) Engine=myisam DEFAULT Charset=utf8 auto_increment=1;
  14. Query OK, 0 rows affected (0.05 sec)
  15. Create a stored procedure that is convenient for inserting data
  16. Mysql> delimiter
  17. Mysql> CREATE PROCEDURE i_test (PA int (one), tab varchar (30))
  18. -> begin
  19. -> declare max_num int (one) default 100000;
  20. -> declare i int default 0;
  21. -> declare rand_num int;
  22. -> declare Double_num char;
  23. ->
  24. -> if tab!= ' Test_test ' then
  25. -> select COUNT (ID) into the max_num from test_t;
  26. -> while I < PA do
  27. -> if Max_num < 100000 then
  28. -> Select CAST (rand () *100 as unsigned) into rand_num;
  29. -> Select Concat (rand_num,rand_num) into Double_num;
  30. -> INSERT INTO test_t (num,d_num) values (rand_num,double_num);
  31. -> End If;
  32. -> Set i = i +1;
  33. -> End While;
  34. -> Else
  35. -> select COUNT (ID) into the max_num from Test_test;
  36. -> while I < PA do
  37. -> if Max_num < 100000 then
  38. -> Select CAST (rand () *100 as unsigned) into rand_num;
  39. -> INSERT into test_test (num) values (rand_num);
  40. -> End If;
  41. -> Set i = i +1;
  42. -> End While;
  43. -> End If;
  44. -> End
  45. Query OK, 0 rows Affected (0.00 sec)
  46. Mysql> delimiter;
  47. Mysql> Show variables like "%pro%"; Check to see if the record execution of the profiling is not open, the default is not open
  48. +---------------------------+-------+
  49. Variable_name Value
  50. +---------------------------+-------+
  51. Profiling off
  52. Profiling_history_size 15
  53. Protocol_version 10
  54. Slave_compressed_protocol off
  55. +---------------------------+-------+
  56. 4 rows in Set (0.00 sec)
  57. Mysql> set profiling=1; After opening, it is to compare the execution time after the index is added
  58. Query OK, 0 rows Affected (0.00 sec)

Third, the example

1, single table data is too small, index will affect speed

 
 
  1. Mysql> call I_test (' test_t '); Insert 10 pieces into the test_t table
  2. Query OK, 1 row affected (0.02 sec)
  3. Mysql> Select num from test_t where num!=0;
  4. mysql> explain select num from test_t where num!=0\g;
  5. 1. Row ***************************
  6. Id:1
  7. Select_type:simple
  8. table:test_t
  9. Type:all
  10. Possible_keys:null
  11. Key:null
  12. Key_len:null
  13. Ref:null
  14. Rows:10
  15. Extra:using where
  16. 1 row in Set (0.00 sec)
  17. ERROR:
  18. No query specified
  19. Mysql> CREATE index num_2 on test_t (num);
  20. Query OK, rows affected (0.19 sec)
  21. Records:10 duplicates:0 warnings:0
  22. Mysql> Select num from test_t where num!=0;
  23. mysql> explain select num from test_t where num!=0\g;
  24. 1. Row ***************************
  25. Id:1
  26. Select_type:simple
  27. table:test_t
  28. Type:index
  29. Possible_keys:num_2
  30. Key:num_2
  31. Key_len:4
  32. Ref:null
  33. Rows:10
  34. Extra:using where; Using Index
  35. 1 row in Set (0.00 sec)
  36. ERROR:
  37. No query specified
  38. Mysql> Show Profiles;
  39. +----------+------------+---------------------------------------------+
  40. query_id Duration Query
  41. +----------+------------+---------------------------------------------+
  42. 1 0.00286325 Call I_test (Ten, ' test_t ')//Insert 10 data
  43. 2 0.00026350 Select num from test_t where num!=0
  44. 3 0.00022250 explain select num from test_t where num!=0
  45. 4 0.18385400 CREATE index num_2 on test_t (num)//CREATE INDEX
  46. 5 0.00127525 Select num from test_t where num!=0///index is almost 0.2 times times less than the index used
  47. 6 0.00024375 explain select num from test_t where num!=0
  48. +----------+------------+---------------------------------------------+
  49. 6 rows in Set (0.00 sec)

Explain:

ID: Indicates the order in which SQL executes

Select_type:simple,primary,union,dependent union,union result,subquery,dependent SUBQUERY, Derived different query statements will have different select_type

Table: Represents the name of a lookup

Type: The index type is used, or there is no index used. Efficiency from high to low const, EQ_REG, ref, range, index and all, in fact, this root of your SQL is directly related to the writing, for example: can use the primary key on the primary key, where the conditions after the index, If the only plus unique index and so on

Possible_keys: Possible indexes

Key: Using Indexes

Key_len: Using the length of the index

Ref: Use which column or constant to select rows from the table with the key, typically in a multiple-table federated query.

Rows: Number of rows found

Extra: Extra Notes

A while ago wrote a blog about MySQL distinct and group by who better, there are friends in the message, said the test results root I did the test results are different, at that time I explained that, today there is time, in the case of the situation, more intuitive expression of the working principle of the index.

After the 2,where condition, the order by, the group by and so on filter, the following fields preferably indexed. According to the actual situation, choose Primary Key, UNIQUE, index index, etc., but not the more the better, to moderate.

3, the Union query, subqueries and other table operations when the connected fields to be indexed

 
 
  1. Mysql> call I_test (' test_test '); Insert 10 data into the Test_test table
  2. Query OK, 1 row affected (0.02 sec)
  3. Mysql> explain select A.num as num1,b.num as num2 from test_t as a-left join TES
  4. T_test as B on a.num=b.num\g;
  5. 1. Row ***************************
  6. Id:1
  7. Select_type:simple
  8. Table:a
  9. Type:index
  10. Possible_keys:null
  11. Key:num_2
  12. Key_len:4
  13. Ref:null
  14. Rows:10
  15. Extra:using Index
  16. 2. Row ***************************
  17. Id:1
  18. Select_type:simple
  19. Table:b
  20. Type:ref
  21. Possible_keys:num_1
  22. Key:num_1
  23. Key_len:4
  24. Ref:bak_test.a.num//bak_test is the name of the database, A.num is a field of test_t
  25. rows:1080
  26. Extra:using Index
  27. 2 rows in Set (0.01 sec)
  28. ERROR:
  29. No query specified

When the volume of data is particularly large, it is best not to use federated queries, even if you have indexed them.

It's just a little bit of a personal summary.




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.