The meaning of creating a 'joint index' in mysql

The meaning of creating a 'joint index' in mysql

From: The meaning of mysql joint index

question?
For what needs, create a 'joint index'? What is the most practical benefit? If it is to query data faster, is it OK to have a single-column index? Why is there a 'joint index'?

one

Simply put, there are two main reasons:

  • "One to three". A composite index of (a, b, c) is built, then it is actually equivalent to building three indexes (a), (a, b), (a, b, c), because each additional index will increase the write operation. overhead and disk space overhead. For tables with large amounts of data, this is a lot of overhead!

  • Covering index. The same has a composite index (a,b,c), if there is the following sql: select a,b,c from table where a=1 and b = 1. Then MySQL can obtain data directly by traversing the index without returning to the table, which reduces a lot of random IO operations. Reducing io operations, special random io is actually the main optimization strategy of dba. Therefore, in real practical applications, covering index is one of the main optimization methods to improve performance.

  • The more index columns, the less data is filtered out by the index. A table with 1000W pieces of data has the following sql: select * from table where a = 1 and b =2 and c = 3, assuming that each condition can filter out 10% of the data, if there is only a single-valued index, then pass this The index can filter out 1000W*10%=100w pieces of data, and then return to the table to find the data that matches b=2 and c=3 from the 100w pieces of data, then sort and paginate; if it is a compound index, filter out the data through the index 1000w *10% *10% *10%=1w, then sort and paginate, which is more efficient, you can see at a glance

two

The following table has three keys: a, b, and c

create table test(
    a int,
    b int,
    c int,
);

If we
need to perform a lot of composite queries like select * from test where a=10, b>50, c>20
this , we may need to create a joint index containing [a,b,c], and a separate index on [a][b][c] is not enough. (You can think of an index as a sorted list). Creating an index of (a, b, c) is equivalent to sorting by a, b, c (the collation is

  if(X.a>Y.a)
    return '>';
  else if(X.a<Y.a)
    return '<';
   else if(X.b>Y.b)
    return '>';
   else if (X.b<Y.b)
    return '<';
   else if (X.c>Y.c)
    return  '>'
   else if (X.c<Y.c)
    return  '<'
   esle
    return '=='

Sorting by a and sorting by b respectively is different from sorting by c respectively.

The order of abc is also very important, sometimes it can be acb, or bca and so on.
If a joint index of (a, b, c) is created, the query efficiency is as follows:

  优: select * from test where a=10 and b>50
  差: select * from test where a>50
  
  优: select * from test order by a
  差: select * from test order by b
  差: select * from test order by c
  
  优: select * from test where a=10 order by a
  优: select * from test where a=10 order by b
  差: select * from test where a=10 order by c
  
  优: select * from test where a>10 order by a
  差: select * from test where a>10 order by b
  差: select * from test where a>10 order by c
  
  优: select * from test where a=10 and b=10 order by a
  优: select * from test where a=10 and b=10 order by b
  优: select * from test where a=10 and b=10 order by c
  
  优: select * from test where a=10 and b=10 order by a
  优: select * from test where a=10 and b>10 order by b
  差: select * from test where a=10 and b>10 order by c

The following is represented by the diagram

Three attention

If the column is of varchar type in mysql, please do not use int type to access the
following
zz_deals table where product_id is of varchar type

mysql> explain select * from zz_deals where qq_shop_id = 64230 and product_id = '38605906667' ;
+----+-------------+-------------+------+------------------------------+------------------------------+---------+-------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+------+------------------------------+------------------------------+---------+-------------+------+-------------+
| 1 | SIMPLE | zz_deals | ref | by_product_id_and_qq_shop_id | by_product_id_and_qq_shop_id | 156 | const,const | 1 | Using where |
+----+-------------+-------------+------+------------------------------+------------------------------+---------+-------------+------+-------------+
1 row in set (0.00 sec)

mysql> explain select * from zz_deals where qq_shop_id = 64230 and product_id = 38605906667 ;
+----+-------------+-------------+------+------------------------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+------+------------------------------+------+---------+------+------+-------------+
| 1 | SIMPLE | zz_deals | ALL | by_product_id_and_qq_shop_id | NULL | NULL | NULL | 17 | Using where |
+----+-------------+-------------+------+------------------------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324725465&siteId=291194637