参数描述

MySQL中不同的版本优化器会有很多新特性,比如MRR、BKA等,optimizer_switch这个参数就是控制查询优化器怎样使用这些特性。很多情况下我们会根据自身的需求去设置optimizer_switch满足我们的需求。前段时间客户的环境中遇到一个奇怪的问题,select count(*)显示返回是有数据但是select * 返回是空结果集,最终的原因就是因为optimizer_switch设置引起了一个BUG。这里和大家分享下防止大家也遇到同样的坑。

案例分析

环境描述

数据库版本MySQL5.6.35

SQL语句

  1. select*from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;

分析过程

当时凌晨4点左右客户打来电话告知数据库查询不到数据了非常着急,我们赶到了现场,当时的现象是这样的:

  1. select*from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;

这条语句查询返回的结果集是空,但是开发人员和我们说数据库中是有数据的,我当时不相信就执行了一下:

  1. select*from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;
  2. Emptyset(0.41 sec)
  3. select count(*)from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;
  4. +----------+
  5. | count(*)|
  6. +----------+
  7. |475|
  8. +----------+
  9. 1 row inset(0.41 sec)

当时也是比较慌张了,count(*)显示返回475条记录但是select *却返回空结果集。。。 
想了一下SQL语句有一层嵌套,我看看里面这个SQL是否有问题,测试后发现内层语句可以正常返回,加上外层语句时就会出现这种情况。询问了应用人员系统刚迁移过来,在原系统没有这种情况,快速连到原系统上执行同样的语句对比一下两边的执行计划: 
原系统

  1. explain select*from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;
  2. +----+-------------+------------+--------+------------------------+-------------------+---------+-------------------+-------+-----------------------------+
  3. | id | select_type | table | type | possible_keys | key | key_len |ref| rows |Extra|
  4. +----+-------------+------------+--------+------------------------+-------------------+---------+-------------------+-------+-----------------------------+
  5. |1| PRIMARY |<derived2>|ref|<auto_key0>|<auto_key0>|153|const|10|Usingwhere;Using filesort |
  6. |2| DERIVED | o | range | idx_orderdatetime | idx_orderdatetime |6| NULL |46104|Using index condition |
  7. |2| DERIVED | mm | eq_ref | PRIMARY,idx_memberid | PRIMARY |8| mall.o.buyerid |1| NULL |
  8. |2| DERIVED | ms |ref| idx_userid | idx_userid |9| mall.o.salerid |1| NULL |
  9. |2| DERIVED | mmt | eq_ref | PRIMARY,idx_merchantid | PRIMARY |8| mall.o.salerid |1| NULL |
  10. |2| DERIVED | ma | eq_ref | PRIMARY | PRIMARY |8| mall.o.activityid |1| NULL |
  11. |2| DERIVED | md |ref| idx_activityid | idx_activityid |8| mall.ma.actid |1| NULL |
  12. +----+-------------+------------+--------+------------------------+-------------------+---------+-------------------+-------+-----------------------------+
  13. 7 rows inset(0.00 sec)

新系统

  1. explain select*from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;
  2. +----+-------------+------------+--------+------------------------+-------------------+---------+-------------------+-------+----------------------------------+
  3. | id | select_type | table | type | possible_keys | key | key_len |ref| rows |Extra|
  4. +----+-------------+------------+--------+------------------------+-------------------+---------+-------------------+-------+----------------------------------+
  5. |1| PRIMARY |<derived2>|ref|<auto_key0>|<auto_key0>|153|const|10|Usingwhere;Using filesort |
  6. |2| DERIVED | o | range | idx_orderdatetime | idx_orderdatetime |6| NULL |46104|Using index condition;Using MRR |
  7. |2| DERIVED | mm | eq_ref | PRIMARY,idx_memberid | PRIMARY |8| mall.o.buyerid |1| NULL |
  8. |2| DERIVED | ms |ref| idx_userid | idx_userid |9| mall.o.salerid |1| NULL |
  9. |2| DERIVED | mmt | eq_ref | PRIMARY,idx_merchantid | PRIMARY |8| mall.o.salerid |1| NULL |
  10. |2| DERIVED | ma | eq_ref | PRIMARY | PRIMARY |8| mall.o.activityid |1| NULL |
  11. |2| DERIVED | md |ref| idx_activityid | idx_activityid |8| mall.ma.actid |1| NULL |
  12. +----+-------------+------------+--------+------------------------+-------------------+---------+-------------------+-------+----------------------------------+
  13. 7 rows inset(0.00 sec)

两边的执行计划不同的地方就是新系统使用了MRR,数据库的版本都是5.6.20之后的小版本号没有差很多应该不会出现这种情况。 
想到了optimizer_switch这个参数可以设置mrr特性,是不是有人修改了他,对比了两边optimizer_switch这个参数发现mrr_cost_based这个值设置的不同。快速的将参数设置为一样再次查询:

  1. set optimizer_switch='mrr_cost_based=on';
  2. Query OK,0 rows affected (0.00 sec)
  3. select*from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;

立刻就返回数据了。

小结

mrr_cost_based代表是否使用基于代价的方式去计算使用MRR特性,新的系统中将他设置为off代表不使用基于代价方式而是使用基于规则的,这样设置的原因是考虑到MySQL基于代价的方式比较保守不能使用到MRR这个特性。本身设置这个参数是没有任何问题,只不过是正好将遇mrr_cost_based设置为off时碰到了这么诡异BUG,希望可以帮助到遇到同样问题的朋友们。

参数描述

MySQL中不同的版本优化器会有很多新特性,比如MRR、BKA等,optimizer_switch这个参数就是控制查询优化器怎样使用这些特性。很多情况下我们会根据自身的需求去设置optimizer_switch满足我们的需求。前段时间客户的环境中遇到一个奇怪的问题,select count(*)显示返回是有数据但是select * 返回是空结果集,最终的原因就是因为optimizer_switch设置引起了一个BUG。这里和大家分享下防止大家也遇到同样的坑。

案例分析

环境描述

数据库版本MySQL5.6.35

SQL语句

  1. select*from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;

分析过程

当时凌晨4点左右客户打来电话告知数据库查询不到数据了非常着急,我们赶到了现场,当时的现象是这样的:

  1. select*from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;

这条语句查询返回的结果集是空,但是开发人员和我们说数据库中是有数据的,我当时不相信就执行了一下:

  1. select*from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;
  2. Emptyset(0.41 sec)
  3. select count(*)from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;
  4. +----------+
  5. | count(*)|
  6. +----------+
  7. |475|
  8. +----------+
  9. 1 row inset(0.41 sec)

当时也是比较慌张了,count(*)显示返回475条记录但是select *却返回空结果集。。。 
想了一下SQL语句有一层嵌套,我看看里面这个SQL是否有问题,测试后发现内层语句可以正常返回,加上外层语句时就会出现这种情况。询问了应用人员系统刚迁移过来,在原系统没有这种情况,快速连到原系统上执行同样的语句对比一下两边的执行计划: 
原系统

  1. explain select*from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;
  2. +----+-------------+------------+--------+------------------------+-------------------+---------+-------------------+-------+-----------------------------+
  3. | id | select_type | table | type | possible_keys | key | key_len |ref| rows |Extra|
  4. +----+-------------+------------+--------+------------------------+-------------------+---------+-------------------+-------+-----------------------------+
  5. |1| PRIMARY |<derived2>|ref|<auto_key0>|<auto_key0>|153|const|10|Usingwhere;Using filesort |
  6. |2| DERIVED | o | range | idx_orderdatetime | idx_orderdatetime |6| NULL |46104|Using index condition |
  7. |2| DERIVED | mm | eq_ref | PRIMARY,idx_memberid | PRIMARY |8| mall.o.buyerid |1| NULL |
  8. |2| DERIVED | ms |ref| idx_userid | idx_userid |9| mall.o.salerid |1| NULL |
  9. |2| DERIVED | mmt | eq_ref | PRIMARY,idx_merchantid | PRIMARY |8| mall.o.salerid |1| NULL |
  10. |2| DERIVED | ma | eq_ref | PRIMARY | PRIMARY |8| mall.o.activityid |1| NULL |
  11. |2| DERIVED | md |ref| idx_activityid | idx_activityid |8| mall.ma.actid |1| NULL |
  12. +----+-------------+------------+--------+------------------------+-------------------+---------+-------------------+-------+-----------------------------+
  13. 7 rows inset(0.00 sec)

新系统

  1. explain select*from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;
  2. +----+-------------+------------+--------+------------------------+-------------------+---------+-------------------+-------+----------------------------------+
  3. | id | select_type | table | type | possible_keys | key | key_len |ref| rows |Extra|
  4. +----+-------------+------------+--------+------------------------+-------------------+---------+-------------------+-------+----------------------------------+
  5. |1| PRIMARY |<derived2>|ref|<auto_key0>|<auto_key0>|153|const|10|Usingwhere;Using filesort |
  6. |2| DERIVED | o | range | idx_orderdatetime | idx_orderdatetime |6| NULL |46104|Using index condition;Using MRR |
  7. |2| DERIVED | mm | eq_ref | PRIMARY,idx_memberid | PRIMARY |8| mall.o.buyerid |1| NULL |
  8. |2| DERIVED | ms |ref| idx_userid | idx_userid |9| mall.o.salerid |1| NULL |
  9. |2| DERIVED | mmt | eq_ref | PRIMARY,idx_merchantid | PRIMARY |8| mall.o.salerid |1| NULL |
  10. |2| DERIVED | ma | eq_ref | PRIMARY | PRIMARY |8| mall.o.activityid |1| NULL |
  11. |2| DERIVED | md |ref| idx_activityid | idx_activityid |8| mall.ma.actid |1| NULL |
  12. +----+-------------+------------+--------+------------------------+-------------------+---------+-------------------+-------+----------------------------------+
  13. 7 rows inset(0.00 sec)

两边的执行计划不同的地方就是新系统使用了MRR,数据库的版本都是5.6.20之后的小版本号没有差很多应该不会出现这种情况。 
想到了optimizer_switch这个参数可以设置mrr特性,是不是有人修改了他,对比了两边optimizer_switch这个参数发现mrr_cost_based这个值设置的不同。快速的将参数设置为一样再次查询:

  1. set optimizer_switch='mrr_cost_based=on';
  2. Query OK,0 rows affected (0.00 sec)
  3. select*from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;.

立刻就返回数据了。

小结

mrr_cost_based代表是否使用基于代价的方式去计算使用MRR特性,新的系统中将他设置为off代表不使用基于代价方式而是使用基于规则的,这样设置的原因是考虑到MySQL基于代价的方式比较保守不能使用到MRR这个特性。本身设置这个参数是没有任何问题,只不过是正好将遇mrr_cost_based设置为off时碰到了这么诡异BUG,希望可以帮助到遇到同样问题的朋友们。

参数描述

MySQL中不同的版本优化器会有很多新特性,比如MRR、BKA等,optimizer_switch这个参数就是控制查询优化器怎样使用这些特性。很多情况下我们会根据自身的需求去设置optimizer_switch满足我们的需求。前段时间客户的环境中遇到一个奇怪的问题,select count(*)显示返回是有数据但是select * 返回是空结果集,最终的原因就是因为optimizer_switch设置引起了一个BUG。这里和大家分享下防止大家也遇到同样的坑。

案例分析

环境描述

数据库版本MySQL5.6.35

SQL语句

  1. select*from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;

分析过程

当时凌晨4点左右客户打来电话告知数据库查询不到数据了非常着急,我们赶到了现场,当时的现象是这样的:

  1. select*from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;

这条语句查询返回的结果集是空,但是开发人员和我们说数据库中是有数据的,我当时不相信就执行了一下:

  1. select*from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;
  2. Emptyset(0.41 sec)
  3. select count(*)from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;
  4. +----------+
  5. | count(*)|
  6. +----------+
  7. |475|
  8. +----------+
  9. 1 row inset(0.41 sec)

当时也是比较慌张了,count(*)显示返回475条记录但是select *却返回空结果集。。。 
想了一下SQL语句有一层嵌套,我看看里面这个SQL是否有问题,测试后发现内层语句可以正常返回,加上外层语句时就会出现这种情况。询问了应用人员系统刚迁移过来,在原系统没有这种情况,快速连到原系统上执行同样的语句对比一下两边的执行计划: 
原系统

  1. explain select*from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;
  2. +----+-------------+------------+--------+------------------------+-------------------+---------+-------------------+-------+-----------------------------+
  3. | id | select_type | table | type | possible_keys | key | key_len |ref| rows |Extra|
  4. +----+-------------+------------+--------+------------------------+-------------------+---------+-------------------+-------+-----------------------------+
  5. |1| PRIMARY |<derived2>|ref|<auto_key0>|<auto_key0>|153|const|10|Usingwhere;Using filesort |
  6. |2| DERIVED | o | range | idx_orderdatetime | idx_orderdatetime |6| NULL |46104|Using index condition |
  7. |2| DERIVED | mm | eq_ref | PRIMARY,idx_memberid | PRIMARY |8| mall.o.buyerid |1| NULL |
  8. |2| DERIVED | ms |ref| idx_userid | idx_userid |9| mall.o.salerid |1| NULL |
  9. |2| DERIVED | mmt | eq_ref | PRIMARY,idx_merchantid | PRIMARY |8| mall.o.salerid |1| NULL |
  10. |2| DERIVED | ma | eq_ref | PRIMARY | PRIMARY |8| mall.o.activityid |1| NULL |
  11. |2| DERIVED | md |ref| idx_activityid | idx_activityid |8| mall.ma.actid |1| NULL |
  12. +----+-------------+------------+--------+------------------------+-------------------+---------+-------------------+-------+-----------------------------+
  13. 7 rows inset(0.00 sec)

新系统

  1. explain select*from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;
  2. +----+-------------+------------+--------+------------------------+-------------------+---------+-------------------+-------+----------------------------------+
  3. | id | select_type | table | type | possible_keys | key | key_len |ref| rows |Extra|
  4. +----+-------------+------------+--------+------------------------+-------------------+---------+-------------------+-------+----------------------------------+
  5. |1| PRIMARY |<derived2>|ref|<auto_key0>|<auto_key0>|153|const|10|Usingwhere;Using filesort |
  6. |2| DERIVED | o | range | idx_orderdatetime | idx_orderdatetime |6| NULL |46104|Using index condition;Using MRR |
  7. |2| DERIVED | mm | eq_ref | PRIMARY,idx_memberid | PRIMARY |8| mall.o.buyerid |1| NULL |
  8. |2| DERIVED | ms |ref| idx_userid | idx_userid |9| mall.o.salerid |1| NULL |
  9. |2| DERIVED | mmt | eq_ref | PRIMARY,idx_merchantid | PRIMARY |8| mall.o.salerid |1| NULL |
  10. |2| DERIVED | ma | eq_ref | PRIMARY | PRIMARY |8| mall.o.activityid |1| NULL |
  11. |2| DERIVED | md |ref| idx_activityid | idx_activityid |8| mall.ma.actid |1| NULL |
  12. +----+-------------+------------+--------+------------------------+-------------------+---------+-------------------+-------+----------------------------------+
  13. 7 rows inset(0.00 sec)

两边的执行计划不同的地方就是新系统使用了MRR,数据库的版本都是5.6.20之后的小版本号没有差很多应该不会出现这种情况。 
想到了optimizer_switch这个参数可以设置mrr特性,是不是有人修改了他,对比了两边optimizer_switch这个参数发现mrr_cost_based这个值设置的不同。快速的将参数设置为一样再次查询:

  1. set optimizer_switch='mrr_cost_based=on';
  2. Query OK,0 rows affected (0.00 sec)
  3. select*from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;

立刻就返回数据了。

小结

mrr_cost_based代表是否使用基于代价的方式去计算使用MRR特性,新的系统中将他设置为off代表不使用基于代价方式而是使用基于规则的,这样设置的原因是考虑到MySQL基于代价的方式比较保守不能使用到MRR这个特性。本身设置这个参数是没有任何问题,只不过是正好将遇mrr_cost_based设置为off时碰到了这么诡异BUG,希望可以帮助到遇到同样问题的朋友们。

参数描述

MySQL中不同的版本优化器会有很多新特性,比如MRR、BKA等,optimizer_switch这个参数就是控制查询优化器怎样使用这些特性。很多情况下我们会根据自身的需求去设置optimizer_switch满足我们的需求。前段时间客户的环境中遇到一个奇怪的问题,select count(*)显示返回是有数据但是select * 返回是空结果集,最终的原因就是因为optimizer_switch设置引起了一个BUG。这里和大家分享下防止大家也遇到同样的坑。

案例分析

环境描述

数据库版本MySQL5.6.35

SQL语句

  1. select*from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;

分析过程

当时凌晨4点左右客户打来电话告知数据库查询不到数据了非常着急,我们赶到了现场,当时的现象是这样的:

  1. select*from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;

这条语句查询返回的结果集是空,但是开发人员和我们说数据库中是有数据的,我当时不相信就执行了一下:

  1. select*from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;
  2. Emptyset(0.41 sec)
  3. select count(*)from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;
  4. +----------+
  5. | count(*)|
  6. +----------+
  7. |475|
  8. +----------+
  9. 1 row inset(0.41 sec)

当时也是比较慌张了,count(*)显示返回475条记录但是select *却返回空结果集。。。 
想了一下SQL语句有一层嵌套,我看看里面这个SQL是否有问题,测试后发现内层语句可以正常返回,加上外层语句时就会出现这种情况。询问了应用人员系统刚迁移过来,在原系统没有这种情况,快速连到原系统上执行同样的语句对比一下两边的执行计划: 
原系统

  1. explain select*from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;
  2. +----+-------------+------------+--------+------------------------+-------------------+---------+-------------------+-------+-----------------------------+
  3. | id | select_type | table | type | possible_keys | key | key_len |ref| rows |Extra|
  4. +----+-------------+------------+--------+------------------------+-------------------+---------+-------------------+-------+-----------------------------+
  5. |1| PRIMARY |<derived2>|ref|<auto_key0>|<auto_key0>|153|const|10|Usingwhere;Using filesort |
  6. |2| DERIVED | o | range | idx_orderdatetime | idx_orderdatetime |6| NULL |46104|Using index condition |
  7. |2| DERIVED | mm | eq_ref | PRIMARY,idx_memberid | PRIMARY |8| mall.o.buyerid |1| NULL |
  8. |2| DERIVED | ms |ref| idx_userid | idx_userid |9| mall.o.salerid |1| NULL |
  9. |2| DERIVED | mmt | eq_ref | PRIMARY,idx_merchantid | PRIMARY |8| mall.o.salerid |1| NULL |
  10. |2| DERIVED | ma | eq_ref | PRIMARY | PRIMARY |8| mall.o.activityid |1| NULL |
  11. |2| DERIVED | md |ref| idx_activityid | idx_activityid |8| mall.ma.actid |1| NULL |
  12. +----+-------------+------------+--------+------------------------+-------------------+---------+-------------------+-------+-----------------------------+
  13. 7 rows inset(0.00 sec)

新系统

  1. explain select*from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;
  2. +----+-------------+------------+--------+------------------------+-------------------+---------+-------------------+-------+----------------------------------+
  3. | id | select_type | table | type | possible_keys | key | key_len |ref| rows |Extra|
  4. +----+-------------+------------+--------+------------------------+-------------------+---------+-------------------+-------+----------------------------------+
  5. |1| PRIMARY |<derived2>|ref|<auto_key0>|<auto_key0>|153|const|10|Usingwhere;Using filesort |
  6. |2| DERIVED | o | range | idx_orderdatetime | idx_orderdatetime |6| NULL |46104|Using index condition;Using MRR |
  7. |2| DERIVED | mm | eq_ref | PRIMARY,idx_memberid | PRIMARY |8| mall.o.buyerid |1| NULL |
  8. |2| DERIVED | ms |ref| idx_userid | idx_userid |9| mall.o.salerid |1| NULL |
  9. |2| DERIVED | mmt | eq_ref | PRIMARY,idx_merchantid | PRIMARY |8| mall.o.salerid |1| NULL |
  10. |2| DERIVED | ma | eq_ref | PRIMARY | PRIMARY |8| mall.o.activityid |1| NULL |
  11. |2| DERIVED | md |ref| idx_activityid | idx_activityid |8| mall.ma.actid |1| NULL |
  12. +----+-------------+------------+--------+------------------------+-------------------+---------+-------------------+-------+----------------------------------+
  13. 7 rows inset(0.00 sec)

两边的执行计划不同的地方就是新系统使用了MRR,数据库的版本都是5.6.20之后的小版本号没有差很多应该不会出现这种情况。 
想到了optimizer_switch这个参数可以设置mrr特性,是不是有人修改了他,对比了两边optimizer_switch这个参数发现mrr_cost_based这个值设置的不同。快速的将参数设置为一样再次查询:

  1. set optimizer_switch='mrr_cost_based=on';
  2. Query OK,0 rows affected (0.00 sec)
  3. select*from(select o.orderid,o.orderdatetime,o.orderstatus,o.price,o.expway,o.paytype,o.fee,o.ordertype,o.realid,mm.account,ms.shopname,mmt.organcode, o.activitype,o.channelcode, ma.activitytag,md.tagtip from mall_order o left join mall_member mm on o.buyerid=mm.memberid left join mall_shop ms on o.salerid=ms.userid left join mall_merchant mmt on mmt.merchantid=o.salerid left join mall_activity ma on o.activityid=ma.actid left join mall_direct_activity md on ma.actid=md.actid where1=1and o.orderdatetime >='2017-03-01 01:40:03'and o.orderdatetime <='2017-03-25 01:40:03')as tab where tab.organcode ='805000' order by orderdatetime desc limit 10;.

立刻就返回数据了。

小结

mrr_cost_based代表是否使用基于代价的方式去计算使用MRR特性,新的系统中将他设置为off代表不使用基于代价方式而是使用基于规则的,这样设置的原因是考虑到MySQL基于代价的方式比较保守不能使用到MRR这个特性。本身设置这个参数是没有任何问题,只不过是正好将遇mrr_cost_based设置为off时碰到了这么诡异BUG,希望可以帮助到遇到同样问题的朋友们。

optimizer_switch引起的诡异问题的更多相关文章

  1. 你还可以再诡异点吗——SQL日志文件不断增长

    前言 今天算是遇到了一个罕见的案例. SQL日志文件不断增长的各种实例不用多说,园子里有很多牛人有过介绍,如果我再阐述这些陈谷子芝麻,想必已会被无数次吐槽. 但这次我碰到的问题确实比较诡异,其解决方式 ...

  2. Delphi编程时候诡异地出现ORA-00937错误,记录解决它的思路和方法

    首先需要说明,这个问题的出现需要几个前提:使用微软的Oracle驱动(使用Oracle自己的驱动不会出现这个问题).使用绑定变量法,使用Format等方式拼接SQL也不会出现这个问题,还有一些诡异的规 ...

  3. 诡异的localhost无法连接

    上午试了localhost发现提示无法连接,ping了下localhost,能够ping通. 重启了Apache,还是无法解决. 试着停止了Apache服务,然后再连接localhost,发现浏览器提 ...

  4. 记SQL SERVER一次诡异的优化

    最近做的项目快上线了,在做了一次压力测试后发现了不少问题,基本都是因为数据量达到一定级别时(预测系统上线10年后的数据量)SQL查询超时,其中有些是因为索引缺失.有些是因为写法不好,这些在有经验的人眼 ...

  5. 诡异的C语言实参求值顺序

    学了这么久的C语言,竟然第一次碰到这么诡异的实参求值顺序问题,大跌眼镜.果然阅读面太少了! #include<iostream> void foo(int a, int b, int c) ...

  6. SSH框架使用中存在的诡异异常

    背景 相信大多数人目前都在使用Spring + Struts2/SpringMVC + Hibernate来构建项目的整体架构,但是在使用中经藏会遇到一些诡异的问题,不知道如果解决,今天我遇到了一个非 ...

  7. Oracle诡异结果调查备忘 - A investigation memo of weird Oracle database search results

    最近需要维护一个差不多十多年前开发的ASP.Net程序,遇到了各种奇奇怪怪的问题,把其中比较难查明的问题记录如下: 问题一: 同样的SQL查询在不同服务器上查询结果不同.在QA环境下,结果完全正常,而 ...

  8. 遭遇flash播放mp3诡异问题

    在部分ie10+flash player 播放mp3,播放第二句话时,中断无法正常播放,(客户的机器上),自己公司的机器测试了几个,都没发现这个问题.其它浏览器(chrome,firefox)也没发现 ...

  9. 番外特别篇之 为什么我不建议你直接使用UIImage传值?--从一个诡异的相册九图连读崩溃bug谈起

    关于"番外特别篇" 所谓"番外特别篇",就是系列文章更新期间内,随机插入的一篇文章.目前我正在更新的系列文章是 实现iOS图片等资源文件的热更新化.但是,这两天 ...

随机推荐

  1. vue-router导航守卫,限制页面访问权限

    在项目开发过程中,经常会需要登录.注册.忘记密码等,也有很多页面是需要登录后才能访问,有些页面是无需登录就可以访问的,那么vue是怎么来限制这些访问权限问题的呢? vue-router导航守卫的bef ...

  2. pta l2-6(树的遍历)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 题意:给出一个二叉树的结点数目n, ...

  3. 第二章 向量(d3)有序向量:Fibonacci查找

  4. suse glibcxx版本过高问题

    实际开发中发现,suse11虽然glibc版本很低,只有2.11.3,但是glibcxx版本很高,达到了3.4.19.这里我需要降低glibcxx版本.所谓glibcxx版本,即libstdc++.s ...

  5. JS中DOM以及BOM

    一.bom对象 1screen对象 console.log(screen.width);// 屏幕宽度 console.log(screen.height);// 屏幕高度 console.log(s ...

  6. iOS耳机监听

    1 .插入耳机的时候并没有切换到耳机播放 仍然是扬声器播放 2 .当一开始手机上已经插入耳机时 ,这时候开启音频播放时 仍然是扬声器播放 因此今天主要谈的就是从这两个问题: 先来解决第一个问题:其实解 ...

  7. cloud server ribbon 自定义策略配置

    虽然ribbon默认为我们提供了多钟负载均衡策略,但有时候我们仍然需要自定义符合自身业务逻辑的规则 使用配置文件的方式:我们只需要在配置文件中添加配置 serviceId.ribbon.NFLoadB ...

  8. Xcode 去掉控制台无用打印信息

    1. 2.在Environment Variables增加一键值对 OS_ACTIVITY_MODE = disable 转自:https://blog.csdn.net/HelloWorld_198 ...

  9. 关于Laravel框架

    第1讲-Laravel介绍 1.1 什么是Laravel laravel是目前一个比较主流的框架,现在很多互联网的公司都在使用该框架.该框架的前身是symfony框架 Laravel的定位就是做一个简 ...

  10. linux下Redis主从复制

    Redis的主从配置比起MySQL主从配置简单多了,而且Redis主从复制中一个主服务可以有多个从服务,一个从服务又可以有多个从服务. MySQL主从配置http://www.cnblogs.com/ ...