Mysql索引失效原因(lonn,隐联直最)

1.where语句中or左右两边有非索引列,索引失效

使用or并不是一定会使索引失效,这需要看or左右两边的查询列都是索引列。

or左右两边的字段都有索引,索引不失效

explain select * from t_user_info where account = 'LeeJanic';
+----+-------------+-------------+-------+---------------+---------------+---------+-------+------+-------+
| id | select_type | table       | type  | possible_keys | key           | key_len | ref   | rows | Extra |
+----+-------------+-------------+-------+---------------+---------------+---------+-------+------+-------+
|  1 | SIMPLE      | t_user_info | const | index_account | index_account | 99      | const |    1 | NULL  |
explain select * from t_user_info where device = '123123123';
+----+-------------+-------------+------+-------------------------+-------------------------+---------+-------+------+-----------------------+
| id | select_type | table       | type | possible_keys           | key                     | key_len | ref   | rows | Extra                 |
+----+-------------+-------------+------+-------------------------+-------------------------+---------+-------+------+-----------------------+
|  1 | SIMPLE      | t_user_info | ref  | index_device_devicetype | index_device_devicetype | 768     | const |    1 | Using index condition |
explain select * from t_user_info where device = '123123123' or  account = 'LeeJanic';
+----+-------------+-------------+-------------+---------------------------------------+---------------------------------------+---------+------+------+----------------------------------------------------------------------+
| id | select_type | table       | type        | possible_keys                         | key                                   | key_len | ref  | rows | Extra                                                                |
+----+-------------+-------------+-------------+---------------------------------------+---------------------------------------+---------+------+------+----------------------------------------------------------------------+
|  1 | SIMPLE      | t_user_info | index_merge | index_account,index_device_devicetype | index_device_devicetype,index_account | 768,99  | NULL |    2 | Using sort_union(index_device_devicetype,index_account); Using where |

or左右两边有非索引列,索引失效

explain select * from t_user_info where account = 'LeeJanic';
+----+-------------+-------------+-------+---------------+---------------+---------+-------+------+-------+
| id | select_type | table       | type  | possible_keys | key           | key_len | ref   | rows | Extra |
+----+-------------+-------------+-------+---------------+---------------+---------+-------+------+-------+
|  1 | SIMPLE      | t_user_info | const | index_account | index_account | 99      | const |    1 | NULL  |
explain select * from t_user_info where ip = '10.1.1.1';
+----+-------------+-------------+------+---------------+------+---------+------+---------+-------------+
| id | select_type | table       | type | possible_keys | key  | key_len | ref  | rows    | Extra       |
+----+-------------+-------------+------+---------------+------+---------+------+---------+-------------+
|  1 | SIMPLE      | t_user_info | ALL  | NULL          | NULL | NULL    | NULL | 3925707 | Using where |
explain select * from t_user_info where account = 'LeeJanic' or ip = '10.1.1.1';
+----+-------------+-------------+------+---------------+------+---------+------+---------+-------------+
| id | select_type | table       | type | possible_keys | key  | key_len | ref  | rows    | Extra       |
+----+-------------+-------------+------+---------------+------+---------+------+---------+-------------+
|  1 | SIMPLE      | t_user_info | ALL  | index_account | NULL | NULL    | NULL | 3925709 | Using where |

结论

  1. 对于索引列account+or+非索引ip情况,假设它走了account的索引,但是走到ip的时候,它还得全表扫描,也就是需要全表扫描+索引扫描+合并。那还不如直接全表扫描,一遍完事。
  2. 如果两边都走索引,那么就可能会走索引

2.where语句中索引使用了负向查询,可能会导致索引

负向查询包括:NOT、!=、<>、!<、!>、NOT IN、NOT LIKE等

<>、!=等运算符号

explain select * from t_user_info where user_id != 100000;
+----+-------------+-------------+-------+---------------+---------+---------+------+---------+-------------+
| id | select_type | table       | type  | possible_keys | key     | key_len | ref  | rows    | Extra       |
+----+-------------+-------------+-------+---------------+---------+---------+------+---------+-------------+
|  1 | SIMPLE      | t_user_info | range | PRIMARY       | PRIMARY | 4       | NULL | 1962899 | Using where |
explain select * from t_user_info where user_id <> 1000000;
+----+-------------+-------------+-------+---------------+---------+---------+------+---------+-------------+
| id | select_type | table       | type  | possible_keys | key     | key_len | ref  | rows    | Extra       |
+----+-------------+-------------+-------+---------------+---------+---------+------+---------+-------------+
|  1 | SIMPLE      | t_user_info | range | PRIMARY       | PRIMARY | 4       | NULL | 1962907 | Using where |
explain select * from t_user_info where account != '123';
+----+-------------+-------------+-------+---------------+--------
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
帮我看一下这段代码有什么问题 clear all; fname='G:\CMIP6 data\map_hed\ACCESS-CM2\ssp126.xlsx'; [data]=xlsread(fname); lat = ncread('G:\CMIP6 data\CMIP6_china\Precipitation\ACCESS-CM2 (Australia)\pr_day_ACCESS-CM2_ssp126_r1i1p1f1_gn_20150101-21001231_v20191108.nc','lat'); lon = ncread('G:\CMIP6 data\CMIP6_china\Precipitation\ACCESS-CM2 (Australia)\pr_day_ACCESS-CM2_ssp126_r1i1p1f1_gn_20150101-21001231_v20191108.nc','lon'); % [x,y]=meshgrid(lon,lat); filename4=('E:\XB\xibei\NewFolder\xeibei84.shp'); Shape=shaperead(filename4); Sx=Shape.X;Sy=Shape.Y; R=m_shaperead('E:\XB\xibei\xb_wang');clf; close all a=find(lon>=70 & lon<=140); b=find(lat>=20 & lat<=60); lon_num=length(a);lat_num=length(b); lonn=lon(a,:);latt=lat(b,:); % D=num2cell(data); for i=1 for g=1:length(lon); x=lon(g); for h=1:length(lat); y=lat(h); U=inpolygon(x,y,Sy,Sx); if U==0 data(g,h,:)=nan; end end end end set(gcf,'Position',[0.1 0.1 1500 1000]); [X,Y]=meshgrid(lonn,latt);hold on; m_proj('miller','lon',[70 110],'lat',[30 50]); uu=m_pcolor(X,Y,data'); shading interp; set(uu,'edgecolor','none') % m_grid('linewi',2,'linest','none','xtick',[70:5:115],'ytick',[30:5:50],'fontsize',22,'linewidth',2); % WBGYR % colorbar % h=colorbar('eastoutside'); colormap('autumn'); colorbar; % set(h,'ticks',[-0.1:0.05:0.3],'linewidth',2,'fontsize',22); % caxis([-0.1 0.3]); for v=1:length(R.ncst) m_line(R.ncst{v}(:,1),R.ncst{v}(:,2),'Color','k','Linewidth',0.5); end hold on; % title(' ','fontsize',25); % saveas(figure(1),'spatial.tif') % close all %
最新发布
06-06

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值