[英]How to create Multiple-Column Indexes efficiently?
Let's say I've coded. 假设我已经编码。
create index use_index on tbl_nm (col2 ,col3 ,col4 ,col5);
would use_index be used in 将use_index用于
select * from tbl_nm where col2="something", col5 = "something", col3="something");
Also, we should have created index by ordering the most unique on the left and the most common on the right. 同样,我们应该通过在左侧排序最唯一的代码,在右侧排序最常见的代码来创建索引。 right?
对?
And if I would like to order the query result should I add that column into the index too? 如果我想订购查询结果,我也应该将该列添加到索引中吗?
The index should be at least refs used in common queries first followed by columns search by range. 索引应至少是普通查询中首先使用的引用,然后是按范围搜索列。
So in your example here col2
and col3
will be use this index. 因此,在您的示例中,
col2
和col3
将使用此索引。 but because there is no col4, the searching for col5
won't be as quick. 但是因为没有col4,所以搜索
col5
不会那么快。 ie all col4
items with col2
and col3
matching "something" will be scanned for a matching col5
. 即,
col4
col2
和col3
匹配“某物”的所有col4
项目进行扫描以查找匹配的col5
。
If you where searching for col4
rather than col5
, it would be a binary search to the required item. 如果您在搜索
col4
而不是col5
,那么它将是对所需项的二进制搜索。
Use EXPLAIN {query}
to show what the index usage is. 使用
EXPLAIN {query}
显示索引的用法。
Consideration of most unique vs least in the order of the index isn't really considered. 真正考虑的不是唯一索引,而是索引的唯一性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.