Bug #74054 Limit 1 returns second row not first one!
Submitted: 24 Sep 2014 14:46 Modified: 26 Sep 2014 16:13
Reporter: Pete French Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server Severity:S2 (Serious)
Version:5.6.20 OS:FreeBSD (9.3-STABLE)
Assigned to: CPU Architecture:Any

[24 Sep 2014 14:46] Pete French
Description:
I have a query which produces two rows when run, ordered by several columns in descending order. When a 'limit 1' is applied to this query it does not return the first row, it retruns the second. Note that any 'limit' clause always returns one row - the second - so if I do 'limot 2' then I do not see both rows, only the second.

Query is below:

select *
from
        preferred_discounts_perm_lists
where
        (area_code in ('NMX',''))
        and (venue_code in ('PLO/T',''))
        and (area_code <> '' or venue_code = '')
        and (event_code in ('2649',''))
        and (venue_code <> '' or event_code = '')
        and (perf_weekday in ('fri',''))
        and (event_code <> '' or perf_weekday = '')
        and (perf_yyyymmdd in ('20141003',''))
        and (perf_weekday <> '' or perf_yyyymmdd = '')
        and (perf_hhmm in ('1945',''))
        and (perf_yyyymmdd <> '' or perf_hhmm = '')
        and (perf_type_code in ('202655',''))
        and (perf_hhmm <> '' or perf_type_code = '')
        and (mod_group in ('standard_affiliate',''))
        and (backend_system = 'nimax')
        and (promo_code = '')
order by area_code desc, venue_code desc, event_code desc, perf_weekday desc, perf_yyyymmdd desc, perf_hhmm desc, perf_type_code desc, mod_group desc
limit 1;

How to repeat:
Attached is a uuencoded dump of the table to which the above query applies. You can load this into an empty database and run the above query with and without the limit clause to see the result.

begin 644 pdl.sql.gz
M'XL("-[-(E0"`W!D;"YS<6P`[5U=EYI,$K[>^16]5Y/,ZT1`_,J>G!-&T6&#
M8`3'-W-#&&%F/%&9!9SL[*_?YAL56M#"Y"(YB2D1ZJFN>KJ[&IKNZVLT>E.^
MBLC8K%X037V@&PCU%XYK+QY0\T/K`T/5T*-EHX%MFC=*O_N!1N_TE=%BWU]<
M7^._Z-9RW(_(,>T7<^TNUB;"?_JZJS_HCOD1K7YJR\6KZ9UX?=0?[TK%M%]-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V$AM.?:BE`[<YN*D`A
M",^1"\"81;>O"#>KBG?EZL52/Y;X6!K$TC"6;F-)N(HVNI&X8A8J_K9(7/C2
M(=LJ2L0`Y-_^KEZ^^"7$8]JD7X^V(LMG!/W'XJ3WDJE"?]8N617`Y.Q<4P44
M:0>V8^'ZDZ`#9]MTMPP=I_L<O-LE7E]_71AH@*A6G>K4O2D>92W8WGTTWFON
MX.ZC!?5O4?`L)<ICRUG`2?R!,T`2@W?KV6Z[S#Y5DE"&MB20+-(,@91O,0;*
MXJR6"DIW'N&@]),X50HC>A3"=CN-,K3Y"@2R%5DHI7G>A])/\OYAC&?]L>Q>
MBC/^)DC):KXH''Z:3$0)G0ZL==_KP`#9;C\>)%QRY388`;79;JMX-(JT;85A
MME,_OT`C$4Q]*MH2/P-3ZV\VC\V>VZ:Q<".$X60,AI!-*,@RY#/J%)1VD?C>
MYZL_=E][7MK>UGXJB7+O2W"U$BJEJ092>!6IPHC7[F6)__39FUH<?_4OC$ZE
M_5.5KZ(VDOOAF=$W_\3`3IKUSQO($UX82AK&UWJW?.^+$ERQ?WS_VJDD?)WR
M6Y=M'4JNB*SJW7(3KH>]H^%O6D\4>$D-+LSZY=#U$UZ9BJJ2I2#\*4.#+(J<
M*LB2UI,EB>]Y8J@@XY?4]73B5TE6>25QK/\U",'U->IO5B]H;JU>EJ9K&LA:
9A_.UN]<,BVCV8[/]L4E=_!_JYA^Y1HP`````
`
end

Suggested fix:
If you embed the select as a subselect and apply the limit outside then it works - but this is a workaround and not a rreal fix. It looks as if it is to do with indexes, as the limited query uses an index, but the unlimted one does not.
[25 Sep 2014 10:29] Hartmut Holzgraefe
I can't reproduce this with 5.6.20, result set without LIMIT:

*************************** 1. row ***************************
                      promo_code: 
                  backend_system: nimax
                       area_code: NMX
                      venue_code: PLO/T
                      event_code: 2649
                    perf_weekday: 
                   perf_yyyymmdd: 
                       perf_hhmm: 
                  perf_type_code: 
                       mod_group: standard_affiliate
             discounts_perm_list: +TSW/IL2/1
         pkg_discounts_perm_list: 
              close_in_day_limit: 0
    close_in_discounts_perm_list: 
close_in_pkg_discounts_perm_list: 
               far_out_day_limit: 0
     far_out_discounts_perm_list: 
 far_out_pkg_discounts_perm_list: 
                         comment: 
*************************** 2. row ***************************
                      promo_code: 
                  backend_system: nimax
                       area_code: 
                      venue_code: 
                      event_code: 
                    perf_weekday: 
                   perf_yyyymmdd: 
                       perf_hhmm: 
                  perf_type_code: 
                       mod_group: standard_affiliate
             discounts_perm_list: +TSW/IAR*,+TSW/ING*,+TSW/INR*,+TSW/PRX/*,+TSW/IA3/*,+TSW/IA4/*,+TSW/IA2/*,+TSW/A11/*,+TSW/A12/*,+TSW/FUL/*, +TSW/OPG/*,+TSW/SPN/*,+TSW/TAW/*,+TSW/TS1*,-TSW/TS2*,+TSW/TS5/1,+TSW/TS6/1,+TSW/TSP/1,+TSW/BEE/*,+TSW/TSW*,+TSW/AFF*,+TSW/AF1*,+TSW/IN1/*,+TSW/IN2/*,+TSW/IN3/*,+TSW/IN4/*,+TSW/IN5/*,+TSW/IN6/*,+TSW/SIF/*,+TSW/PRV/*,+TSW/TS3/*,+TSW/TAF/1
         pkg_discounts_perm_list: 
              close_in_day_limit: 0
    close_in_discounts_perm_list: 
close_in_pkg_discounts_perm_list: 
               far_out_day_limit: 0
     far_out_discounts_perm_list: 
 far_out_pkg_discounts_perm_list: 
                         comment: 
2 rows in set (0.01 sec)

and with LIMIT 1:

*************************** 1. row ***************************
                      promo_code: 
                  backend_system: nimax
                       area_code: NMX
                      venue_code: PLO/T
                      event_code: 2649
                    perf_weekday: 
                   perf_yyyymmdd: 
                       perf_hhmm: 
                  perf_type_code: 
                       mod_group: standard_affiliate
             discounts_perm_list: +TSW/IL2/1
         pkg_discounts_perm_list: 
              close_in_day_limit: 0
    close_in_discounts_perm_list: 
close_in_pkg_discounts_perm_list: 
               far_out_day_limit: 0
     far_out_discounts_perm_list: 
 far_out_pkg_discounts_perm_list: 
                         comment: 
1 row in set (0.00 sec)

LIMIT 2 -> same result as without LIMIT
[25 Sep 2014 10:31] Hartmut Holzgraefe
Explain plans are the same in both cases here, only differ in "rows" estimate (256 vs 287):

*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: preferred_discounts_perm_lists
         type: index
possible_keys: PRIMARY,backend_system
          key: PRIMARY
      key_len: 1399
          ref: NULL
         rows: 287
        Extra: Using where
1 row in set (0.00 sec)
[25 Sep 2014 10:33] Hartmut Holzgraefe
even when adding FORCE INDEX (backend_system) I'm still getting the same results with and without LIMIT ...
[25 Sep 2014 15:08] Pete French
Which version of FreeBSD are you using, and are you using the compiled version from the pkg repository ? This is perfectly repeatable for me on our systems. I haven't tried it on other versions of FreeBSD though.

I will try and setup a separate test system elsewhere and see what results I get from that...
[26 Sep 2014 15:13] Pete French
Ok, tried again this morning - I still have the bug on my table, but droping the table and reloading the data makes it behave correctly. Sorry! I assume that when I did the test of loading the data I forgot to switch to the fresh database before running the query.

Still odd, but looks like a corrupt innodb table I guess. You can close this...
[26 Sep 2014 16:13] MySQL Verification Team
Thank you for confirming, I couldn't repeat with 5.6.20/21 too.
Per [26 Sep 15:13] Pete French, marking this as not a bug, please feel free to open if you are hitting this frequently.

Thanks,
Umesh