mysql复合索引
1、首先synchronied是一个修饰符,而ReentrantLock是一个类。2、synchronied加锁释放锁是jvm自动的,而ReentrantLock是要主动lock,unlock,容易死锁,所以在使用的地方在finally里面做unlock。3、synchronied是非公平锁,多个线程自旋等待拿到锁。ReentrantLock可是公平也可以是非公平,可以通过创建的时候设置参数参考文
·
mysql中经常会用到复合索引,查询效率快,创建符合索引如下:
ALTER TABLE `table_name` ADD INDEX (`col1`,`col2`,`col3`);
复合索引的本质(最左匹配原则):
上面创建的复合索引col1,col2,col3,本质上是创建了三个索引,col1;col1、col2;col1、col2、col3。如图:
col1 | col2 | col3 |
col1 | ||
col1+col2 | ||
col1+col2+col3 |
查询案例:知道这些规则,便好sql优化,不过最好的是能走覆盖索引(查询的内容在索引里)
select * from tableName where col1=a and col3=c;
-- 这个是走索引的,不过仅仅走了col1
select * from tableName where col1=a;
-- 这个是走索引的,走了col1
select * from tableName where col1=a and col3=c and col2=b;
-- 这个是走col1、col2、col3;where 后面的顺序不受影响,mysql查询优化器会优化的。
select * from tableName where col1=a and col3=c;
-- 这个是走索引的,不过仅仅走了col1
更多推荐
已为社区贡献2条内容
所有评论(0)