参数描述

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. SpringCloud报错: "Field discoveryClient in com.controller.DcController required a bean of type 'com.netflix.discovery.DiscoveryClient' that could not be found."

    SpringCloud报错: "Field discoveryClient in com.controller.DcController required a bean of type 'c ...

  2. 原生js实现一个简单的轮播图

    想锻炼一下自己的原生js能力可以从写一个轮播图开始,轮播图的运用想必大家都知道吧,好了废话不多说,开始记笔记了,一些需要注意的点,我都在代码中标注了 首先是构造html: <div id=&qu ...

  3. 从上往下打印二叉树(python)

    题目描述 从上往下打印出二叉树的每个节点,同层节点从左至右打印. # -*- coding:utf-8 -*- # class TreeNode: # def __init__(self, x): # ...

  4. TabError的解决方法

    问题:Python文件运行时报TabError: inconsistent use of tabs and spaces in indentation 原因:说明Python文件中混有Tab和Spac ...

  5. CentOS 7 单用户模式+救援模式

    CentOS 7 单用户模式+救援模式 CentOS 7 单用户模式+救援模式.有时候大家可能会忘记自己的root密码,或者错误(命令输入错误,命令位置输入有误等)编辑了一个/etc目录下的核心文件导 ...

  6. 最长公共子序列hdu1503

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1503 题意:给你两个字符串,把这两个字符串合并,使合并之后的字符串最短,并且合并之后的字符之间的相对位 ...

  7. 图片Bitmap在本地的存储与读取 File

    将Bitmap存储到本地: public void SaveImage(Bitmap image, String user_id){ //照片通常存在DCIM文件夹中 String sdCardDir ...

  8. 【mybatis基础】mybatis开发dao两种方法

    mybatis是一个支持普通SQL查询,存储过程和高级映射的优秀的持久层的框架,是apache下的顶级项目.mybatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.mybat ...

  9. Java 的静态工厂方法

    本文转载自:https://www.jianshu.com/p/ceb5ec8f1174 序:什么是静态工厂方法 Effective Java 2.1 静态工厂方法与构造器不同的第一优势在于,它们有名 ...

  10. Python爬虫项目--爬取猫眼电影Top100榜

    本次抓取猫眼电影Top100榜所用到的知识点: 1. python requests库 2. 正则表达式 3. csv模块 4. 多进程 正文 目标站点分析 通过对目标站点的分析, 来确定网页结构,  ...