The most confusing order of composite indexes is the order of index columns, which depends not only on queries using this index, but also on sorting and grouping. Some time ago I posted a post: where condition order and compound index field order. Interested friends may wish to participate in the discussion. Today, I want to share my opinion. In the application development stage, [selectivity] is our primary consideration. Please refer to the figure below:
When SQL Performance problems occur, you may need to pay attention to the following: 1. random IO2. sorting (order by) 3. group by or distinct. In this case, you don't have to worry about [selectivity]. My experience is that when you already have Top n SQL statements, we should give priority to [value distribution] rather than selectivity.
How to determine the value distribution is as follows:
Select * from userresult_f where askid = 800808 and uid = 110996854;
There are two indexes available: 1. idx_1 (askid, uid) 2. Is idx_2 (uid, askid) 1 or 2? Use the [sarg] method:
Mysql> select sum (askid = 800808), sum (uid = 110996854) from userresult_f \ G; * *************************** 1. row ************************** sum (askid = 800808 ): 6sum (uid = 110996854): 21 row in set (0.00 sec)
Based on the query output, we should select idx_2
By data Shepherd Good Luck! Fuzhou