1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495
|
drop table if exists t1,t2;
drop view if exists v1,v2,v3,v4;
set @exit_optimizer_switch=@@optimizer_switch;
set @exit_join_cache_level=@@join_cache_level;
set @exit_join_buffer_size=@@join_buffer_size;
set optimizer_switch='derived_merge=on,derived_with_keys=on';
set @save_optimizer_switch=@@optimizer_switch;
set join_cache_level=1;
create table t1(f1 int, f11 int);
create table t2(f2 int, f22 int);
insert into t1 values(1,1),(2,2),(3,3),(5,5),(9,9),(7,7);
insert into t1 values(17,17),(13,13),(11,11),(15,15),(19,19);
insert into t2 values(1,1),(3,3),(2,2),(4,4),(8,8),(6,6);
insert into t2 values(12,12),(14,14),(10,10),(18,18),(16,16);
Tests:
for merged derived tables
explain for simple derived
explain select * from (select * from t1) tt;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 11
select * from (select * from t1) tt;
f1 f11
1 1
2 2
3 3
5 5
9 9
7 7
17 17
13 13
11 11
15 15
19 19
explain for multitable derived
explain extended select * from (select * from t1 join t2 on f1=f2) tt;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 11 100.00
1 SIMPLE t2 ALL NULL NULL NULL NULL 11 100.00 Using where; Using join buffer (flat, BNL join)
Warnings:
Note 1003 select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f11` AS `f11`,`test`.`t2`.`f2` AS `f2`,`test`.`t2`.`f22` AS `f22` from `test`.`t1` join `test`.`t2` where `test`.`t2`.`f2` = `test`.`t1`.`f1`
select * from (select * from t1 join t2 on f1=f2) tt;
f1 f11 f2 f22
1 1 1 1
3 3 3 3
2 2 2 2
explain for derived with where
explain extended
select * from (select * from t1 where f1 in (2,3)) tt where f11=2;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 11 100.00 Using where
Warnings:
Note 1003 select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f11` AS `f11` from `test`.`t1` where `test`.`t1`.`f11` = 2 and `test`.`t1`.`f1` in (2,3)
select * from (select * from t1 where f1 in (2,3)) tt where f11=2;
f1 f11
2 2
join of derived
explain extended
select * from (select * from t1 where f1 in (2,3)) tt join
(select * from t1 where f1 in (1,2)) aa on tt.f1=aa.f1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 11 100.00 Using where
1 SIMPLE t1 ALL NULL NULL NULL NULL 11 100.00 Using where; Using join buffer (flat, BNL join)
Warnings:
Note 1003 select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f11` AS `f11`,`test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f11` AS `f11` from `test`.`t1` join `test`.`t1` where `test`.`t1`.`f1` = `test`.`t1`.`f1` and `test`.`t1`.`f1` in (1,2) and `test`.`t1`.`f1` in (2,3)
select * from (select * from t1 where f1 in (2,3)) tt join
(select * from t1 where f1 in (1,2)) aa on tt.f1=aa.f1;
f1 f11 f1 f11
2 2 2 2
flush status;
explain extended
select * from (select * from t1 where f1 in (2,3)) tt where f11=2;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 11 100.00 Using where
Warnings:
Note 1003 select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f11` AS `f11` from `test`.`t1` where `test`.`t1`.`f11` = 2 and `test`.`t1`.`f1` in (2,3)
show status like 'Handler_read%';
Variable_name Value
Handler_read_first 0
Handler_read_key 0
Handler_read_last 0
Handler_read_next 0
Handler_read_prev 0
Handler_read_retry 0
Handler_read_rnd 0
Handler_read_rnd_deleted 0
Handler_read_rnd_next 0
flush status;
select * from (select * from t1 where f1 in (2,3)) tt where f11=2;
f1 f11
2 2
show status like 'Handler_read%';
Variable_name Value
Handler_read_first 0
Handler_read_key 0
Handler_read_last 0
Handler_read_next 0
Handler_read_prev 0
Handler_read_retry 0
Handler_read_rnd 0
Handler_read_rnd_deleted 0
Handler_read_rnd_next 12
for merged views
create view v1 as select * from t1;
create view v2 as select * from t1 join t2 on f1=f2;
create view v3 as select * from t1 where f1 in (2,3);
create view v4 as select * from t2 where f2 in (2,3);
explain for simple views
explain extended select * from v1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 11 100.00
Warnings:
Note 1003 select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f11` AS `f11` from `test`.`t1`
select * from v1;
f1 f11
1 1
2 2
3 3
5 5
9 9
7 7
17 17
13 13
11 11
15 15
19 19
explain for multitable views
explain extended select * from v2;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 11 100.00
1 SIMPLE t2 ALL NULL NULL NULL NULL 11 100.00 Using where; Using join buffer (flat, BNL join)
Warnings:
Note 1003 select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f11` AS `f11`,`test`.`t2`.`f2` AS `f2`,`test`.`t2`.`f22` AS `f22` from `test`.`t1` join `test`.`t2` where `test`.`t2`.`f2` = `test`.`t1`.`f1`
select * from v2;
f1 f11 f2 f22
1 1 1 1
3 3 3 3
2 2 2 2
explain for views with where
explain extended select * from v3 where f11 in (1,3);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 11 100.00 Using where
Warnings:
Note 1003 select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f11` AS `f11` from `test`.`t1` where `test`.`t1`.`f11` in (1,3) and `test`.`t1`.`f1` in (2,3)
select * from v3 where f11 in (1,3);
f1 f11
3 3
explain for joined views
explain extended
select * from v3 join v4 on f1=f2;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 11 100.00 Using where
1 SIMPLE t2 ALL NULL NULL NULL NULL 11 100.00 Using where; Using join buffer (flat, BNL join)
Warnings:
Note 1003 select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f11` AS `f11`,`test`.`t2`.`f2` AS `f2`,`test`.`t2`.`f22` AS `f22` from `test`.`t1` join `test`.`t2` where `test`.`t2`.`f2` = `test`.`t1`.`f1` and `test`.`t1`.`f1` in (2,3) and `test`.`t1`.`f1` in (2,3)
select * from v3 join v4 on f1=f2;
f1 f11 f2 f22
3 3 3 3
2 2 2 2
flush status;
explain extended select * from v4 where f2 in (1,3);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 11 100.00 Using where
Warnings:
Note 1003 select `test`.`t2`.`f2` AS `f2`,`test`.`t2`.`f22` AS `f22` from `test`.`t2` where `test`.`t2`.`f2` in (1,3) and `test`.`t2`.`f2` in (2,3)
show status like 'Handler_read%';
Variable_name Value
Handler_read_first 0
Handler_read_key 0
Handler_read_last 0
Handler_read_next 0
Handler_read_prev 0
Handler_read_retry 0
Handler_read_rnd 0
Handler_read_rnd_deleted 0
Handler_read_rnd_next 0
flush status;
select * from v4 where f2 in (1,3);
f2 f22
3 3
show status like 'Handler_read%';
Variable_name Value
Handler_read_first 0
Handler_read_key 0
Handler_read_last 0
Handler_read_next 0
Handler_read_prev 0
Handler_read_retry 0
Handler_read_rnd 0
Handler_read_rnd_deleted 0
Handler_read_rnd_next 12
for materialized derived tables
explain for simple derived
explain extended select * from (select * from t1 group by f1) tt;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 11 100.00
2 DERIVED t1 ALL NULL NULL NULL NULL 11 100.00 Using temporary; Using filesort
Warnings:
Note 1003 /* select#1 */ select `tt`.`f1` AS `f1`,`tt`.`f11` AS `f11` from (/* select#2 */ select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f11` AS `f11` from `test`.`t1` group by `test`.`t1`.`f1`) `tt`
select * from (select * from t1 having f1=f1) tt;
f1 f11
1 1
2 2
3 3
5 5
9 9
7 7
17 17
13 13
11 11
15 15
19 19
explain showing created indexes
explain extended
select * from t1 join (select * from t2 group by f2) tt on f1=f2;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 11 100.00 Using where
1 PRIMARY <derived2> ref key0 key0 5 test.t1.f1 1 100.00
2 DERIVED t2 ALL NULL NULL NULL NULL 11 100.00 Using temporary; Using filesort
Warnings:
Note 1003 /* select#1 */ select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f11` AS `f11`,`tt`.`f2` AS `f2`,`tt`.`f22` AS `f22` from `test`.`t1` join (/* select#2 */ select `test`.`t2`.`f2` AS `f2`,`test`.`t2`.`f22` AS `f22` from `test`.`t2` group by `test`.`t2`.`f2`) `tt` where `tt`.`f2` = `test`.`t1`.`f1`
select * from t1 join (select * from t2 group by f2) tt on f1=f2;
f1 f11 f2 f22
1 1 1 1
2 2 2 2
3 3 3 3
explain showing late materialization
flush status;
explain select * from t1 join (select * from t2 group by f2) tt on f1=f2;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 11 Using where
1 PRIMARY <derived2> ref key0 key0 5 test.t1.f1 1
2 DERIVED t2 ALL NULL NULL NULL NULL 11 Using temporary; Using filesort
show status like 'Handler_read%';
Variable_name Value
Handler_read_first 0
Handler_read_key 0
Handler_read_last 0
Handler_read_next 0
Handler_read_prev 0
Handler_read_retry 0
Handler_read_rnd 0
Handler_read_rnd_deleted 0
Handler_read_rnd_next 0
flush status;
select * from t1 join (select * from t2 group by f2) tt on f1=f2;
f1 f11 f2 f22
1 1 1 1
2 2 2 2
3 3 3 3
show status like 'Handler_read%';
Variable_name Value
Handler_read_first 0
Handler_read_key 11
Handler_read_last 0
Handler_read_next 3
Handler_read_prev 0
Handler_read_retry 0
Handler_read_rnd 11
Handler_read_rnd_deleted 0
Handler_read_rnd_next 36
for materialized views
drop view v1,v2,v3;
create view v1 as select * from t1 group by f1;
create view v2 as select * from t2 group by f2;
create view v3 as select t1.f1,t1.f11 from t1 join t1 as t11 where t1.f1=t11.f1
having t1.f1<100;
explain for simple derived
explain extended select * from v1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 11 100.00
2 DERIVED t1 ALL NULL NULL NULL NULL 11 100.00 Using temporary; Using filesort
Warnings:
Note 1003 /* select#1 */ select `v1`.`f1` AS `f1`,`v1`.`f11` AS `f11` from `test`.`v1`
select * from v1;
f1 f11
1 1
2 2
3 3
5 5
7 7
9 9
11 11
13 13
15 15
17 17
19 19
explain showing created indexes
explain extended select * from t1 join v2 on f1=f2;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 11 100.00 Using where
1 PRIMARY <derived2> ref key0 key0 5 test.t1.f1 1 100.00
2 DERIVED t2 ALL NULL NULL NULL NULL 11 100.00 Using temporary; Using filesort
Warnings:
Note 1003 /* select#1 */ select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f11` AS `f11`,`v2`.`f2` AS `f2`,`v2`.`f22` AS `f22` from `test`.`t1` join `test`.`v2` where `v2`.`f2` = `test`.`t1`.`f1`
select * from t1 join v2 on f1=f2;
f1 f11 f2 f22
1 1 1 1
2 2 2 2
3 3 3 3
explain extended
select * from t1,v3 as v31,v3 where t1.f1=v31.f1 and t1.f1=v3.f1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 11 100.00 Using where
1 PRIMARY <derived2> ref key0 key0 5 test.t1.f1 10 100.00
1 PRIMARY <derived3> ref key0 key0 5 test.t1.f1 10 100.00
3 DERIVED t1 ALL NULL NULL NULL NULL 11 100.00
3 DERIVED t11 ALL NULL NULL NULL NULL 11 100.00 Using where; Using join buffer (flat, BNL join)
2 DERIVED t1 ALL NULL NULL NULL NULL 11 100.00
2 DERIVED t11 ALL NULL NULL NULL NULL 11 100.00 Using where; Using join buffer (flat, BNL join)
Warnings:
Note 1003 /* select#1 */ select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f11` AS `f11`,`v31`.`f1` AS `f1`,`v31`.`f11` AS `f11`,`v3`.`f1` AS `f1`,`v3`.`f11` AS `f11` from `test`.`t1` join `test`.`v3` `v31` join `test`.`v3` where `v31`.`f1` = `test`.`t1`.`f1` and `v3`.`f1` = `test`.`t1`.`f1`
flush status;
select * from t1,v3 as v31,v3 where t1.f1=v31.f1 and t1.f1=v3.f1;
f1 f11 f1 f11 f1 f11
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
5 5 5 5 5 5
9 9 9 9 9 9
7 7 7 7 7 7
17 17 17 17 17 17
13 13 13 13 13 13
11 11 11 11 11 11
15 15 15 15 15 15
19 19 19 19 19 19
show status like 'Handler_read%';
Variable_name Value
Handler_read_first 0
Handler_read_key 22
Handler_read_last 0
Handler_read_next 22
Handler_read_prev 0
Handler_read_retry 0
Handler_read_rnd 0
Handler_read_rnd_deleted 0
Handler_read_rnd_next 60
explain showing late materialization
flush status;
explain select * from t1 join v2 on f1=f2;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 11 Using where
1 PRIMARY <derived2> ref key0 key0 5 test.t1.f1 1
2 DERIVED t2 ALL NULL NULL NULL NULL 11 Using temporary; Using filesort
show status like 'Handler_read%';
Variable_name Value
Handler_read_first 0
Handler_read_key 0
Handler_read_last 0
Handler_read_next 0
Handler_read_prev 0
Handler_read_retry 0
Handler_read_rnd 0
Handler_read_rnd_deleted 0
Handler_read_rnd_next 0
flush status;
select * from t1 join v2 on f1=f2;
f1 f11 f2 f22
1 1 1 1
2 2 2 2
3 3 3 3
show status like 'Handler_read%';
Variable_name Value
Handler_read_first 0
Handler_read_key 11
Handler_read_last 0
Handler_read_next 3
Handler_read_prev 0
Handler_read_retry 0
Handler_read_rnd 11
Handler_read_rnd_deleted 0
Handler_read_rnd_next 36
explain extended select * from v1 join v4 on f1=f2;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 11 100.00 Using where
1 PRIMARY <derived2> ref key0 key0 5 test.t2.f2 1 100.00
2 DERIVED t1 ALL NULL NULL NULL NULL 11 100.00 Using where; Using temporary; Using filesort
Warnings:
Note 1003 /* select#1 */ select `v1`.`f1` AS `f1`,`v1`.`f11` AS `f11`,`test`.`t2`.`f2` AS `f2`,`test`.`t2`.`f22` AS `f22` from `test`.`v1` join `test`.`t2` where `v1`.`f1` = `test`.`t2`.`f2` and `test`.`t2`.`f2` in (2,3)
explain format=json select * from v1 join v4 on f1=f2;
EXPLAIN
{
"query_block": {
"select_id": 1,
"cost": "COST_REPLACED",
"nested_loop": [
{
"table": {
"table_name": "t2",
"access_type": "ALL",
"loops": 1,
"rows": 11,
"cost": "COST_REPLACED",
"filtered": 100,
"attached_condition": "t2.f2 in (2,3) and t2.f2 is not null"
}
},
{
"table": {
"table_name": "<derived2>",
"access_type": "ref",
"possible_keys": ["key0"],
"key": "key0",
"key_length": "5",
"used_key_parts": ["f1"],
"ref": ["test.t2.f2"],
"loops": 11,
"rows": 1,
"cost": "COST_REPLACED",
"filtered": 100,
"materialized": {
"query_block": {
"select_id": 2,
"cost": "COST_REPLACED",
"filesort": {
"sort_key": "t1.f1",
"temporary_table": {
"nested_loop": [
{
"table": {
"table_name": "t1",
"access_type": "ALL",
"loops": 1,
"rows": 11,
"cost": "COST_REPLACED",
"filtered": 100,
"attached_condition": "t1.f1 in (2,3)"
}
}
]
}
}
}
}
}
}
]
}
}
select * from v1 join v4 on f1=f2;
f1 f11 f2 f22
3 3 3 3
2 2 2 2
merged derived in merged derived
explain extended select * from (select * from
(select * from t1 where f1 < 7) tt where f1 > 2) zz;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 11 100.00 Using where
Warnings:
Note 1003 select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f11` AS `f11` from `test`.`t1` where `test`.`t1`.`f1` > 2 and `test`.`t1`.`f1` < 7
select * from (select * from
(select * from t1 where f1 < 7) tt where f1 > 2) zz;
f1 f11
3 3
5 5
materialized derived in merged derived
explain extended select * from (select * from
(select * from t1 where f1 < 7 group by f1) tt where f1 > 2) zz;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY <derived3> ALL NULL NULL NULL NULL 11 100.00 Using where
3 DERIVED t1 ALL NULL NULL NULL NULL 11 100.00 Using where; Using temporary; Using filesort
Warnings:
Note 1003 /* select#1 */ select `tt`.`f1` AS `f1`,`tt`.`f11` AS `f11` from (/* select#3 */ select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f11` AS `f11` from `test`.`t1` where `test`.`t1`.`f1` < 7 and `test`.`t1`.`f1` > 2 group by `test`.`t1`.`f1`) `tt` where `tt`.`f1` > 2
select * from (select * from
(select * from t1 where f1 < 7 group by f1) tt where f1 > 2) zz;
f1 f11
3 3
5 5
merged derived in materialized derived
explain extended select * from (select * from
(select * from t1 where f1 < 7) tt where f1 > 2 group by f1) zz;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 11 100.00
2 DERIVED t1 ALL NULL NULL NULL NULL 11 100.00 Using where; Using temporary; Using filesort
Warnings:
Note 1003 /* select#1 */ select `zz`.`f1` AS `f1`,`zz`.`f11` AS `f11` from (/* select#2 */ select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f11` AS `f11` from `test`.`t1` where `test`.`t1`.`f1` > 2 and `test`.`t1`.`f1` < 7 group by `test`.`t1`.`f1`) `zz`
select * from (select * from
(select * from t1 where f1 < 7) tt where f1 > 2 group by f1) zz;
f1 f11
3 3
5 5
materialized derived in materialized derived
explain extended select * from (select * from
(select * from t1 where f1 < 7 group by f1) tt where f1 > 2 group by f1) zz;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 11 100.00
2 DERIVED <derived3> ALL NULL NULL NULL NULL 11 100.00 Using where; Using temporary; Using filesort
3 DERIVED t1 ALL NULL NULL NULL NULL 11 100.00 Using where; Using temporary; Using filesort
Warnings:
Note 1003 /* select#1 */ select `zz`.`f1` AS `f1`,`zz`.`f11` AS `f11` from (/* select#2 */ select `tt`.`f1` AS `f1`,`tt`.`f11` AS `f11` from (/* select#3 */ select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f11` AS `f11` from `test`.`t1` where `test`.`t1`.`f1` < 7 and `test`.`t1`.`f1` > 2 group by `test`.`t1`.`f1`) `tt` where `tt`.`f1` > 2 group by `tt`.`f1`) `zz`
explain format=json select * from (select * from
(select * from t1 where f1 < 7 group by f1) tt where f1 > 2 group by f1) zz;
EXPLAIN
{
"query_block": {
"select_id": 1,
"cost": "COST_REPLACED",
"nested_loop": [
{
"table": {
"table_name": "<derived2>",
"access_type": "ALL",
"loops": 1,
"rows": 11,
"cost": "COST_REPLACED",
"filtered": 100,
"materialized": {
"query_block": {
"select_id": 2,
"cost": "COST_REPLACED",
"filesort": {
"sort_key": "tt.f1",
"temporary_table": {
"nested_loop": [
{
"table": {
"table_name": "<derived3>",
"access_type": "ALL",
"loops": 1,
"rows": 11,
"cost": "COST_REPLACED",
"filtered": 100,
"attached_condition": "tt.f1 > 2",
"materialized": {
"query_block": {
"select_id": 3,
"cost": "COST_REPLACED",
"filesort": {
"sort_key": "t1.f1",
"temporary_table": {
"nested_loop": [
{
"table": {
"table_name": "t1",
"access_type": "ALL",
"loops": 1,
"rows": 11,
"cost": "COST_REPLACED",
"filtered": 100,
"attached_condition": "t1.f1 < 7 and t1.f1 > 2"
}
}
]
}
}
}
}
}
}
]
}
}
}
}
}
}
]
}
}
select * from (select * from
(select * from t1 where f1 < 7 group by f1) tt where f1 > 2 group by f1) zz;
f1 f11
3 3
5 5
mat in merged derived join mat in merged derived
explain extended select * from
(select * from (select * from t1 where f1 < 7 group by f1) tt where f1 > 2) x
join
(select * from (select * from t1 where f1 < 7 group by f1) tt where f1 > 2) z
on x.f1 = z.f1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY <derived3> ALL NULL NULL NULL NULL 11 100.00 Using where
1 PRIMARY <derived5> ref key0 key0 5 tt.f1 1 100.00
5 DERIVED t1 ALL NULL NULL NULL NULL 11 100.00 Using where; Using temporary; Using filesort
3 DERIVED t1 ALL NULL NULL NULL NULL 11 100.00 Using where; Using temporary; Using filesort
Warnings:
Note 1003 /* select#1 */ select `tt`.`f1` AS `f1`,`tt`.`f11` AS `f11`,`tt`.`f1` AS `f1`,`tt`.`f11` AS `f11` from (/* select#3 */ select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f11` AS `f11` from `test`.`t1` where `test`.`t1`.`f1` < 7 and `test`.`t1`.`f1` > 2 and `test`.`t1`.`f1` > 2 group by `test`.`t1`.`f1`) `tt` join (/* select#5 */ select `t1`.`f1` AS `f1`,`t1`.`f11` AS `f11` from `test`.`t1` where `t1`.`f1` < 7 and `t1`.`f1` > 2 and `t1`.`f1` > 2 group by `t1`.`f1`) `tt` where `tt`.`f1` = `tt`.`f1` and `tt`.`f1` > 2 and `tt`.`f1` > 2
explain format=json select * from
(select * from (select * from t1 where f1 < 7 group by f1) tt where f1 > 2) x
join
(select * from (select * from t1 where f1 < 7 group by f1) tt where f1 > 2) z
on x.f1 = z.f1;
EXPLAIN
{
"query_block": {
"select_id": 1,
"cost": "COST_REPLACED",
"nested_loop": [
{
"table": {
"table_name": "<derived3>",
"access_type": "ALL",
"loops": 1,
"rows": 11,
"cost": "COST_REPLACED",
"filtered": 100,
"attached_condition": "tt.f1 > 2 and tt.f1 > 2 and tt.f1 is not null",
"materialized": {
"query_block": {
"select_id": 3,
"cost": "COST_REPLACED",
"filesort": {
"sort_key": "t1.f1",
"temporary_table": {
"nested_loop": [
{
"table": {
"table_name": "t1",
"access_type": "ALL",
"loops": 1,
"rows": 11,
"cost": "COST_REPLACED",
"filtered": 100,
"attached_condition": "t1.f1 < 7 and t1.f1 > 2 and t1.f1 > 2"
}
}
]
}
}
}
}
}
},
{
"table": {
"table_name": "<derived5>",
"access_type": "ref",
"possible_keys": ["key0"],
"key": "key0",
"key_length": "5",
"used_key_parts": ["f1"],
"ref": ["tt.f1"],
"loops": 11,
"rows": 1,
"cost": "COST_REPLACED",
"filtered": 100,
"materialized": {
"query_block": {
"select_id": 5,
"cost": "COST_REPLACED",
"filesort": {
"sort_key": "t1.f1",
"temporary_table": {
"nested_loop": [
{
"table": {
"table_name": "t1",
"access_type": "ALL",
"loops": 1,
"rows": 11,
"cost": "COST_REPLACED",
"filtered": 100,
"attached_condition": "t1.f1 < 7 and t1.f1 > 2 and t1.f1 > 2"
}
}
]
}
}
}
}
}
}
]
}
}
flush status;
select * from
(select * from (select * from t1 where f1 < 7 group by f1) tt where f1 > 2) x
join
(select * from (select * from t1 where f1 < 7 group by f1) tt where f1 > 2) z
on x.f1 = z.f1;
f1 f11 f1 f11
3 3 3 3
5 5 5 5
show status like 'Handler_read%';
Variable_name Value
Handler_read_first 0
Handler_read_key 2
Handler_read_last 0
Handler_read_next 2
Handler_read_prev 0
Handler_read_retry 0
Handler_read_rnd 4
Handler_read_rnd_deleted 0
Handler_read_rnd_next 33
flush status;
merged in merged derived join merged in merged derived
explain extended select * from
(select * from
(select * from t1 where f1 < 7 ) tt where f1 > 2 ) x
join
(select * from
(select * from t1 where f1 < 7 ) tt where f1 > 2 ) z
on x.f1 = z.f1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 11 100.00 Using where
1 SIMPLE t1 ALL NULL NULL NULL NULL 11 100.00 Using where; Using join buffer (flat, BNL join)
Warnings:
Note 1003 select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f11` AS `f11`,`t1`.`f1` AS `f1`,`t1`.`f11` AS `f11` from `test`.`t1` join `test`.`t1` where `t1`.`f1` = `test`.`t1`.`f1` and `test`.`t1`.`f1` > 2 and `test`.`t1`.`f1` < 7 and `test`.`t1`.`f1` > 2 and `test`.`t1`.`f1` < 7
select * from
(select * from
(select * from t1 where f1 < 7 ) tt where f1 > 2 ) x
join
(select * from
(select * from t1 where f1 < 7 ) tt where f1 > 2 ) z
on x.f1 = z.f1;
f1 f11 f1 f11
3 3 3 3
5 5 5 5
materialized in materialized derived join
materialized in materialized derived
explain extended select * from
(select * from
(select * from t1 where f1 < 7 group by f1) tt where f1 > 2 group by f1) x
join
(select * from
(select * from t1 where f1 < 7 group by f1) tt where f1 > 2 group by f1) z
on x.f1 = z.f1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 11 100.00 Using where
1 PRIMARY <derived4> ref key0 key0 5 x.f1 1 100.00
4 DERIVED <derived5> ALL NULL NULL NULL NULL 11 100.00 Using where; Using temporary; Using filesort
5 DERIVED t1 ALL NULL NULL NULL NULL 11 100.00 Using where; Using temporary; Using filesort
2 DERIVED <derived3> ALL NULL NULL NULL NULL 11 100.00 Using where; Using temporary; Using filesort
3 DERIVED t1 ALL NULL NULL NULL NULL 11 100.00 Using where; Using temporary; Using filesort
Warnings:
Note 1003 /* select#1 */ select `x`.`f1` AS `f1`,`x`.`f11` AS `f11`,`z`.`f1` AS `f1`,`z`.`f11` AS `f11` from (/* select#2 */ select `tt`.`f1` AS `f1`,`tt`.`f11` AS `f11` from (/* select#3 */ select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f11` AS `f11` from `test`.`t1` where `test`.`t1`.`f1` < 7 and `test`.`t1`.`f1` > 2 group by `test`.`t1`.`f1`) `tt` where `tt`.`f1` > 2 group by `tt`.`f1`) `x` join (/* select#4 */ select `tt`.`f1` AS `f1`,`tt`.`f11` AS `f11` from (/* select#5 */ select `t1`.`f1` AS `f1`,`t1`.`f11` AS `f11` from `test`.`t1` where `t1`.`f1` < 7 and `t1`.`f1` > 2 group by `t1`.`f1`) `tt` where `tt`.`f1` > 2 group by `tt`.`f1`) `z` where `z`.`f1` = `x`.`f1`
explain format=json select * from
(select * from
(select * from t1 where f1 < 7 group by f1) tt where f1 > 2 group by f1) x
join
(select * from
(select * from t1 where f1 < 7 group by f1) tt where f1 > 2 group by f1) z
on x.f1 = z.f1;
EXPLAIN
{
"query_block": {
"select_id": 1,
"cost": "COST_REPLACED",
"nested_loop": [
{
"table": {
"table_name": "<derived2>",
"access_type": "ALL",
"loops": 1,
"rows": 11,
"cost": "COST_REPLACED",
"filtered": 100,
"attached_condition": "x.f1 is not null",
"materialized": {
"query_block": {
"select_id": 2,
"cost": "COST_REPLACED",
"filesort": {
"sort_key": "tt.f1",
"temporary_table": {
"nested_loop": [
{
"table": {
"table_name": "<derived3>",
"access_type": "ALL",
"loops": 1,
"rows": 11,
"cost": "COST_REPLACED",
"filtered": 100,
"attached_condition": "tt.f1 > 2",
"materialized": {
"query_block": {
"select_id": 3,
"cost": "COST_REPLACED",
"filesort": {
"sort_key": "t1.f1",
"temporary_table": {
"nested_loop": [
{
"table": {
"table_name": "t1",
"access_type": "ALL",
"loops": 1,
"rows": 11,
"cost": "COST_REPLACED",
"filtered": 100,
"attached_condition": "t1.f1 < 7 and t1.f1 > 2"
}
}
]
}
}
}
}
}
}
]
}
}
}
}
}
},
{
"table": {
"table_name": "<derived4>",
"access_type": "ref",
"possible_keys": ["key0"],
"key": "key0",
"key_length": "5",
"used_key_parts": ["f1"],
"ref": ["x.f1"],
"loops": 11,
"rows": 1,
"cost": "COST_REPLACED",
"filtered": 100,
"materialized": {
"query_block": {
"select_id": 4,
"cost": "COST_REPLACED",
"filesort": {
"sort_key": "tt.f1",
"temporary_table": {
"nested_loop": [
{
"table": {
"table_name": "<derived5>",
"access_type": "ALL",
"loops": 1,
"rows": 11,
"cost": "COST_REPLACED",
"filtered": 100,
"attached_condition": "tt.f1 > 2",
"materialized": {
"query_block": {
"select_id": 5,
"cost": "COST_REPLACED",
"filesort": {
"sort_key": "t1.f1",
"temporary_table": {
"nested_loop": [
{
"table": {
"table_name": "t1",
"access_type": "ALL",
"loops": 1,
"rows": 11,
"cost": "COST_REPLACED",
"filtered": 100,
"attached_condition": "t1.f1 < 7 and t1.f1 > 2"
}
}
]
}
}
}
}
}
}
]
}
}
}
}
}
}
]
}
}
select * from
(select * from
(select * from t1 where f1 < 7 group by f1) tt where f1 > 2 group by f1) x
join
(select * from
(select * from t1 where f1 < 7 group by f1) tt where f1 > 2 group by f1) z
on x.f1 = z.f1;
f1 f11 f1 f11
3 3 3 3
5 5 5 5
merged view in materialized derived
explain extended
select * from (select * from v4 group by 1) tt;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 11 100.00
2 DERIVED t2 ALL NULL NULL NULL NULL 11 100.00 Using where; Using temporary; Using filesort
Warnings:
Note 1003 /* select#1 */ select `tt`.`f2` AS `f2`,`tt`.`f22` AS `f22` from (/* select#2 */ select `test`.`t2`.`f2` AS `f2`,`test`.`t2`.`f22` AS `f22` from `test`.`t2` where `test`.`t2`.`f2` in (2,3) group by 1) `tt`
select * from (select * from v4 group by 1) tt;
f2 f22
2 2
3 3
materialized view in merged derived
explain extended
select * from ( select * from v1 where f1 < 7) tt;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY <derived3> ALL NULL NULL NULL NULL 11 100.00 Using where
3 DERIVED t1 ALL NULL NULL NULL NULL 11 100.00 Using where; Using temporary; Using filesort
Warnings:
Note 1003 /* select#1 */ select `v1`.`f1` AS `f1`,`v1`.`f11` AS `f11` from `test`.`v1` where `v1`.`f1` < 7
explain format=json
select * from ( select * from v1 where f1 < 7) tt;
EXPLAIN
{
"query_block": {
"select_id": 1,
"cost": "COST_REPLACED",
"nested_loop": [
{
"table": {
"table_name": "<derived3>",
"access_type": "ALL",
"loops": 1,
"rows": 11,
"cost": "COST_REPLACED",
"filtered": 100,
"attached_condition": "v1.f1 < 7",
"materialized": {
"query_block": {
"select_id": 3,
"cost": "COST_REPLACED",
"filesort": {
"sort_key": "t1.f1",
"temporary_table": {
"nested_loop": [
{
"table": {
"table_name": "t1",
"access_type": "ALL",
"loops": 1,
"rows": 11,
"cost": "COST_REPLACED",
"filtered": 100,
"attached_condition": "t1.f1 < 7"
}
}
]
}
}
}
}
}
}
]
}
}
select * from ( select * from v1 where f1 < 7) tt;
f1 f11
1 1
2 2
3 3
5 5
merged view in a merged view in a merged derived
create view v6 as select * from v4 where f2 < 7;
explain extended select * from (select * from v6) tt;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 11 100.00 Using where
Warnings:
Note 1003 select `test`.`t2`.`f2` AS `f2`,`test`.`t2`.`f22` AS `f22` from `test`.`t2` where `test`.`t2`.`f2` < 7 and `test`.`t2`.`f2` in (2,3)
select * from (select * from v6) tt;
f2 f22
3 3
2 2
materialized view in a merged view in a materialized derived
create view v7 as select * from v1;
explain extended select * from (select * from v7 group by 1) tt;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 11 100.00
2 DERIVED <derived4> ALL NULL NULL NULL NULL 11 100.00 Using temporary; Using filesort
4 DERIVED t1 ALL NULL NULL NULL NULL 11 100.00 Using temporary; Using filesort
Warnings:
Note 1003 /* select#1 */ select `tt`.`f1` AS `f1`,`tt`.`f11` AS `f11` from (/* select#2 */ select `v1`.`f1` AS `f1`,`v1`.`f11` AS `f11` from `test`.`v1` group by 1) `tt`
select * from (select * from v7 group by 1) tt;
f1 f11
1 1
2 2
3 3
5 5
7 7
9 9
11 11
13 13
15 15
17 17
19 19
join of above two
explain extended select * from v6 join v7 on f2=f1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 11 100.00 Using where
1 PRIMARY <derived5> ref key0 key0 5 test.t2.f2 1 100.00
5 DERIVED t1 ALL NULL NULL NULL NULL 11 100.00 Using where; Using temporary; Using filesort
Warnings:
Note 1003 /* select#1 */ select `test`.`t2`.`f2` AS `f2`,`test`.`t2`.`f22` AS `f22`,`v1`.`f1` AS `f1`,`v1`.`f11` AS `f11` from `test`.`t2` join `test`.`v1` where `v1`.`f1` = `test`.`t2`.`f2` and `test`.`t2`.`f2` < 7 and `test`.`t2`.`f2` in (2,3)
explain format=json select * from v6 join v7 on f2=f1;
EXPLAIN
{
"query_block": {
"select_id": 1,
"cost": "COST_REPLACED",
"nested_loop": [
{
"table": {
"table_name": "t2",
"access_type": "ALL",
"loops": 1,
"rows": 11,
"cost": "COST_REPLACED",
"filtered": 100,
"attached_condition": "t2.f2 < 7 and t2.f2 in (2,3) and t2.f2 is not null"
}
},
{
"table": {
"table_name": "<derived5>",
"access_type": "ref",
"possible_keys": ["key0"],
"key": "key0",
"key_length": "5",
"used_key_parts": ["f1"],
"ref": ["test.t2.f2"],
"loops": 11,
"rows": 1,
"cost": "COST_REPLACED",
"filtered": 100,
"materialized": {
"query_block": {
"select_id": 5,
"cost": "COST_REPLACED",
"filesort": {
"sort_key": "t1.f1",
"temporary_table": {
"nested_loop": [
{
"table": {
"table_name": "t1",
"access_type": "ALL",
"loops": 1,
"rows": 11,
"cost": "COST_REPLACED",
"filtered": 100,
"attached_condition": "t1.f1 < 7 and t1.f1 in (2,3)"
}
}
]
}
}
}
}
}
}
]
}
}
select * from v6 join v7 on f2=f1;
f2 f22 f1 f11
3 3 3 3
2 2 2 2
test two keys
explain select * from t1 join (select * from t2 group by f2) tt on t1.f1=tt.f2 join t1 xx on tt.f22=xx.f1;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 11 Using where
1 PRIMARY <derived2> ref key0 key0 5 test.t1.f1 1
1 PRIMARY xx ALL NULL NULL NULL NULL 11 Using where; Using join buffer (flat, BNL join)
2 DERIVED t2 ALL NULL NULL NULL NULL 11 Using temporary; Using filesort
select * from t1 join (select * from t2 group by f2) tt on t1.f1=tt.f2 join t1 xx on tt.f22=xx.f1;
f1 f11 f2 f22 f1 f11
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
TODO: Add test with 64 tables mergeable view to test fall back to
materialization on tables > MAX_TABLES merge
drop table t1,t2;
drop view v1,v2,v3,v4,v6,v7;
#
# LP bug #794909: crash when defining possible keys for
# a materialized view/derived_table
#
CREATE TABLE t1 (f1 int) ;
INSERT INTO t1 VALUES (149), (150), (224), (29);
CREATE TABLE t2 (f1 int, KEY (f1));
INSERT INTO t2 VALUES (149), (NULL), (224);
CREATE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t1;
EXPLAIN
SELECT * FROM v1 JOIN t2 ON v1.f1 = t2.f1;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 index f1 f1 5 NULL 3 Using where; Using index
1 PRIMARY <derived2> ref key0 key0 5 test.t2.f1 1
2 DERIVED t1 ALL NULL NULL NULL NULL 4
SELECT * FROM v1 JOIN t2 ON v1.f1 = t2.f1;
f1 f1
149 149
224 224
DROP VIEW v1;
DROP TABLE t1,t2;
#
# LP bug #794890: abort failure on multi-update with view
#
CREATE TABLE t1 (a int);
INSERT INTO t1 VALUES (20), (7);
CREATE TABLE t2 (a int);
INSERT INTO t2 VALUES (7), (9), (7);
CREATE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT a FROM t1;
CREATE VIEW v2 AS SELECT t2.a FROM t2, v1 WHERE t2.a=t2.a;
UPDATE v2 SET a = 2;
SELECT * FROM t2;
a
2
2
2
UPDATE t1,v2 SET t1.a = 3;
SELECT * FROM t1;
a
3
3
DELETE t1 FROM t1,v2;
SELECT * FROM t1;
a
DROP VIEW v1,v2;
DROP TABLE t1,t2;
#
# LP bug #802023: MIN/MAX optimization
# for mergeable derived tables and views
#
CREATE TABLE t1 (a int, b int, c varchar(32), INDEX idx(a,b));
INSERT INTO t1 VALUES
(7, 74, 'yyyyyyy'), (9, 97, 'aaaaaaaaa'), (2, 23, 'tt'),
(5, 55, 'ddddd'), (2, 27, 'ss'), (7, 76, 'xxxxxxx'),
(7, 79, 'zzzzzzz'), (9, 92, 'bbbbbbbbb'), (2, 25, 'pp'),
(5, 53, 'eeeee'), (2, 23, 'qq'), (7, 76,'wwwwwww'),
(7, 74, 'uuuuuuu'), (9, 92, 'ccccccccc'), (2, 25, 'oo');
CREATE VIEW v1 AS SELECT * FROM t1;
SELECT MIN(a) FROM t1 WHERE a >= 5;
MIN(a)
5
EXPLAIN
SELECT MIN(a) FROM t1 WHERE a >= 5;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
SELECT MIN(a) FROM (SELECT * FROM t1) t WHERE a >= 5;
MIN(a)
5
EXPLAIN
SELECT MIN(a) FROM(SELECT * FROM t1) t WHERE a >= 5;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
SELECT MIN(a) FROM v1 WHERE a >= 5;
MIN(a)
5
EXPLAIN
SELECT MIN(a) FROM v1 WHERE a >= 5;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
SELECT MAX(b) FROM t1 WHERE a=7 AND b<75;
MAX(b)
74
EXPLAIN
SELECT MAX(b) FROM t1 WHERE a=7 AND b<75;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
SELECT MAX(b) FROM (SELECT * FROM t1) t WHERE a=7 AND b<75;
MAX(b)
74
EXPLAIN
SELECT MAX(b) FROM (SELECT * FROM t1) t WHERE a=7 AND b<75;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
SELECT MAX(b) FROM v1 WHERE a=7 AND b<75;
MAX(b)
74
EXPLAIN
SELECT MAX(b) FROM v1 WHERE a=7 AND b<75;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
DROP VIEW v1;
DROP TABLE t1;
#
# LP bug #800535: GROUP BY query with nested left join
# and a derived table in the nest
#
CREATE TABLE t1 (a int) ;
INSERT INTO t1 VALUES (1), (2);
CREATE TABLE t2 (a int NOT NULL);
INSERT INTO t2 VALUES (1), (2);
CREATE TABLE t3 (a int, b int);
INSERT INTO t3 VALUES (3,3), (4,4);
EXPLAIN EXTENDED
SELECT t.a FROM t1 LEFT JOIN
(t2 t JOIN t3 ON t3.b > 5) ON t.a >= 1
GROUP BY t.a;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00 Using temporary; Using filesort
1 SIMPLE t ALL NULL NULL NULL NULL 2 100.00 Using where
1 SIMPLE t3 ALL NULL NULL NULL NULL 2 100.00 Using where
Warnings:
Note 1003 select `test`.`t`.`a` AS `a` from `test`.`t1` left join (`test`.`t2` `t` join `test`.`t3`) on(`test`.`t`.`a` >= 1 and `test`.`t3`.`b` > 5) where 1 group by `test`.`t`.`a`
SELECT t.a FROM t1 LEFT JOIN
(t2 t JOIN t3 ON t3.b > 5) ON t.a >= 1
GROUP BY t.a;
a
NULL
EXPLAIN EXTENDED
SELECT t.a FROM t1 LEFT JOIN
(( SELECT * FROM t2 ) t JOIN t3 ON t3.b > 5) ON t.a >= 1
GROUP BY t.a;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00 Using temporary; Using filesort
1 SIMPLE t2 ALL NULL NULL NULL NULL 2 100.00 Using where
1 SIMPLE t3 ALL NULL NULL NULL NULL 2 100.00 Using where
Warnings:
Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t1` left join (`test`.`t2` join `test`.`t3`) on(`test`.`t2`.`a` >= 1 and `test`.`t3`.`b` > 5) where 1 group by `test`.`t2`.`a`
SELECT t.a FROM t1 LEFT JOIN
(( SELECT * FROM t2 ) t JOIN t3 ON t3.b > 5) ON t.a >= 1
GROUP BY t.a;
a
NULL
CREATE VIEW v1 AS SELECT * FROM t2;
EXPLAIN EXTENDED
SELECT t.a FROM t1 LEFT JOIN
(v1 t JOIN t3 ON t3.b > 5) ON t.a >= 1
GROUP BY t.a;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00 Using temporary; Using filesort
1 SIMPLE t2 ALL NULL NULL NULL NULL 2 100.00 Using where
1 SIMPLE t3 ALL NULL NULL NULL NULL 2 100.00 Using where
Warnings:
Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t1` left join (`test`.`t2` join `test`.`t3`) on(`test`.`t2`.`a` >= 1 and `test`.`t3`.`b` > 5) where 1 group by `test`.`t2`.`a`
SELECT t.a FROM t1 LEFT JOIN
(v1 t JOIN t3 ON t3.b > 5) ON t.a >= 1
GROUP BY t.a;
a
NULL
DROP VIEW v1;
DROP TABLE t1,t2,t3;
#
# LP bug #803410: materialized view/dt accessed by two-component key
#
CREATE TABLE t1 (a varchar(1)) CHARSET=latin1;
INSERT INTO t1 VALUES ('c');
CREATE TABLE t2 (a varchar(1) , KEY (a)) CHARSET=latin1;
INSERT INTO t2 VALUES ('c'), (NULL), ('r');
CREATE TABLE t3 (a varchar(1), b varchar(1)) CHARSET=latin1;
INSERT INTO t3 VALUES
('e', 'c'), ('c', 'c'), ('c', 'r'), ('g', 'a'), ('b', 'x'), ('b', 'y'),
('h', 'w'), ('d', 'z'), ('k', 'v'), ('j', 's'), ('m', 'p'), ('l', 'q');
CREATE VIEW v1 AS SELECT a, MIN(b) AS b FROM t3 GROUP BY a;
EXPLAIN
SELECT * FROM t1, t2, v1 WHERE t2.a=t1.a AND t2.a=v1.a AND t2.a=v1.b;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 system NULL NULL NULL NULL 1
1 PRIMARY t2 ref a a 4 const 1 Using index
1 PRIMARY <derived2> ref key0 key0 8 const,const 1
2 DERIVED t3 ALL NULL NULL NULL NULL 12 Using temporary; Using filesort
SELECT * FROM t1, t2, v1 WHERE t2.a=t1.a AND t2.a=v1.a AND t2.a=v1.b;
a a a b
c c c c
DROP VIEW v1;
DROP TABLE t1,t2,t3;
#
# LP bug #802845: select from derived table with limit 0
#
SELECT * FROM (SELECT 1 LIMIT 0) t;
1
CREATE TABLE t1 (a int);
INSERT INTO t1 VALUES (7), (1), (3);
SELECT * FROM (SELECT * FROM t1 LIMIT 0) t;
a
DROP TABLE t1;
#
# LP bug #803851: materialized view + IN->EXISTS
#
SET SESSION optimizer_switch='semijoin=off,derived_with_keys=on,materialization=off';
CREATE TABLE t1 (a int, b int);
INSERT INTO t1 VALUES (2,2), (3,3), (1,1);
CREATE TABLE t2 (a int);
INSERT INTO t2 VALUES (1), (2), (1);
CREATE TABLE t3 (a int);
INSERT INTO t3 VALUES (3), (1), (2), (1);
CREATE VIEW v1 AS SELECT a, MAX(b) AS b FROM t1 GROUP BY a;
EXPLAIN EXTENDED
SELECT * FROM t3
WHERE t3.a IN (SELECT v1.a FROM v1, t2 WHERE t2.a = v1.b);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t3 ALL NULL NULL NULL NULL 4 100.00 Using where
2 DEPENDENT SUBQUERY <derived3> ref key1 key1 5 func 1 100.00
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join)
3 DERIVED t1 ALL NULL NULL NULL NULL 3 100.00 Using temporary; Using filesort
Warnings:
Note 1003 /* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where <expr_cache><`test`.`t3`.`a`>(<in_optimizer>(`test`.`t3`.`a`,<exists>(/* select#2 */ select `v1`.`a` from `test`.`v1` join `test`.`t2` where `test`.`t2`.`a` = `v1`.`b` and <cache>(`test`.`t3`.`a`) = `v1`.`a`)))
SELECT * FROM t3
WHERE t3.a IN (SELECT v1.a FROM v1, t2 WHERE t2.a = v1.b);
a
1
2
1
SET SESSION optimizer_switch=@save_optimizer_switch;
DROP VIEW v1;
DROP TABLE t1,t2,t3;
#
# LP bug #804515: materialized derived + ORDER BY
#
CREATE TABLE t1 (f1 varchar(1), f2 varchar(1), KEY (f2)) CHARSET=latin1;
INSERT INTO t1 VALUES
('r','x'), ('x','d'), ('x','r'), ('r','f'), ('x','x');
CREATE TABLE t2 (f1 varchar(1), f2 varchar(1)) CHARSET=latin1;
INSERT INTO t2 VALUES ('s','x');
CREATE TABLE t3 (f1 varchar(1), f2 varchar(1), KEY (f2)) CHARSET=latin1;
INSERT INTO t3 VALUES
(NULL,'x'), (NULL,'f'), ('t','p'), (NULL,'j'), ('g','c');
CREATE TABLE t4 (f1 int, f2 varchar(1), KEY (f2,f1)) CHARSET=latin1;
INSERT INTO t4 VALUES (1,'x'), (5,'r');
EXPLAIN
SELECT t.f1 AS f
FROM (SELECT DISTINCT t1.* FROM t1,t2 WHERE t2.f2 = t1.f2) t,t3,t4
WHERE t4.f2 = t3.f2 AND t4.f2 = t.f1 ORDER BY f;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t4 index f2 f2 9 NULL 2 Using where; Using index; Using temporary; Using filesort
1 PRIMARY <derived2> ref key1 key1 4 test.t4.f2 1
1 PRIMARY t3 ref f2 f2 4 test.t4.f2 1 Using index
2 DERIVED t2 system NULL NULL NULL NULL 1 Using temporary
2 DERIVED t1 ref f2 f2 4 const 2 Using where
SELECT t.f1 AS f
FROM (SELECT DISTINCT t1.* FROM t1,t2 WHERE t2.f2 = t1.f2) t,t3,t4
WHERE t4.f2 = t3.f2 AND t4.f2 = t.f1 ORDER BY f;
f
x
DROP TABLE t1,t2,t3,t4;
#
# LP bug #806431: join over materialized derived with key
#
CREATE TABLE t1 (a int, b int);
INSERT INTO t1 VALUES (0,0),(3,0),(1,0);
CREATE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT a,b FROM t1 ;
SET SESSION optimizer_switch='derived_with_keys=off';
SELECT * FROM t1 AS t JOIN v1 AS v WHERE t.a = v.b AND t.b = v.b;
a b a b
0 0 0 0
0 0 3 0
0 0 1 0
SET SESSION optimizer_switch='derived_with_keys=on';
EXPLAIN
SELECT * FROM t1 AS t JOIN v1 AS v WHERE t.a = v.b AND t.b = v.b;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t ALL NULL NULL NULL NULL 3 Using where
1 PRIMARY <derived2> ref key0 key0 5 test.t.a 1
2 DERIVED t1 ALL NULL NULL NULL NULL 3
SELECT * FROM t1 AS t JOIN v1 AS v WHERE t.a = v.b AND t.b = v.b;
a b a b
0 0 1 0
0 0 3 0
0 0 0 0
SET SESSION optimizer_switch=@save_optimizer_switch;
DROP VIEW v1;
DROP TABLE t1;
#
# LP bug #806477: left join over merged join with
# where condition containing f=f
#
CREATE TABLE t1 (a int NOT NULL);
INSERT INTO t1 VALUES (1), (50), (0);
CREATE TABLE t2 (a int);
CREATE TABLE t3 (a int, b int);
INSERT INTO t3 VALUES (76,2), (1,NULL);
CREATE VIEW v1 AS SELECT * FROM t1;
SELECT t3.b, v1.a
FROM t3 LEFT JOIN (t2, v1) ON t3.a <> 0
WHERE v1.a = v1.a OR t3.b <> 0;
b a
2 NULL
EXPLAIN EXTENDED
SELECT t3.b, v1.a
FROM t3 LEFT JOIN (t2, v1) ON t3.a <> 0
WHERE v1.a = v1.a OR t3.b <> 0;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t3 ALL NULL NULL NULL NULL 2 100.00
1 SIMPLE t2 ALL NULL NULL NULL NULL 0 0.00 Using where
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using where
Warnings:
Note 1003 select `test`.`t3`.`b` AS `b`,`test`.`t1`.`a` AS `a` from `test`.`t3` left join (`test`.`t2` join `test`.`t1`) on(`test`.`t3`.`a` <> 0) where `test`.`t1`.`a` = `test`.`t1`.`a` or `test`.`t3`.`b` <> 0
DROP VIEW v1;
DROP TABLE t1,t2,t3;
#
# LP bug #806510: subquery with outer reference
# to a derived_table/view
#
CREATE TABLE t1 (a int) ;
INSERT INTO t1 VALUES (4), (NULL);
CREATE TABLE t2 (a int) ;
INSERT INTO t2 VALUES (8), (0);
CREATE TABLE t3 (a int, b int) ;
INSERT INTO t3 VALUES (7,8);
CREATE VIEW v1 AS SELECT * FROM t1;
SELECT * FROM t1 t
WHERE EXISTS (SELECT t3.a FROM t3, t2
WHERE t2.a = t3.b AND t.a != 0);
a
4
EXPLAIN
SELECT * FROM t1 t
WHERE EXISTS (SELECT t3.a FROM t3, t2
WHERE t2.a = t3.b AND t.a != 0);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t ALL NULL NULL NULL NULL 2 Using where
2 DEPENDENT SUBQUERY t3 system NULL NULL NULL NULL 1
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where
SELECT * FROM (SELECT * FROM t1) t
WHERE EXISTS (SELECT t3.a FROM t3, t2
WHERE t2.a = t3.b AND t.a != 0);
a
4
EXPLAIN
SELECT * FROM (SELECT * FROM t1) t
WHERE EXISTS (SELECT t3.a FROM t3, t2
WHERE t2.a = t3.b AND t.a != 0);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
3 DEPENDENT SUBQUERY t3 system NULL NULL NULL NULL 1
3 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where
SELECT * FROM v1 t
WHERE EXISTS (SELECT t3.a FROM t3, t2
WHERE t2.a = t3.b AND t.a != 0);
a
4
EXPLAIN
SELECT * FROM v1 t
WHERE EXISTS (SELECT t3.a FROM t3, t2
WHERE t2.a = t3.b AND t.a != 0);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
2 DEPENDENT SUBQUERY t3 system NULL NULL NULL NULL 1
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where
DROP VIEW v1;
DROP TABLE t1,t2,t3;
#
# LP bug #806097: left join over a view + DISTINCT
#
CREATE TABLE t1 (a int, b int);
INSERT INTO t1 VALUES (252,6), (232,0), (174,232);
CREATE TABLE t2 (a int);
INSERT INTO t2 VALUES (232), (174);
CREATE TABLE t3 (c int);
INSERT INTO t3 VALUES (1), (2);
CREATE VIEW v1 AS SELECT t2.a FROM t3,t2;
SELECT v1.a FROM t1 LEFT JOIN v1 ON t1.b = 0;
a
NULL
232
174
232
174
NULL
SELECT DISTINCT t2.a FROM t1 LEFT JOIN (t3,t2) ON t1.b = 0;
a
NULL
232
174
EXPLAIN
SELECT DISTINCT t2.a FROM t1 LEFT JOIN (t3,t2) ON t1.b = 0;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using temporary
1 SIMPLE t3 ALL NULL NULL NULL NULL 2 Using where
1 SIMPLE t2 ALL NULL NULL NULL NULL 2
SELECT DISTINCT v1.a FROM t1 LEFT JOIN v1 ON t1.b = 0;
a
NULL
232
174
EXPLAIN
SELECT DISTINCT v1.a FROM t1 LEFT JOIN v1 ON t1.b = 0;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using temporary
1 SIMPLE t3 ALL NULL NULL NULL NULL 2 Using where
1 SIMPLE t2 ALL NULL NULL NULL NULL 2
DROP VIEW v1;
DROP TABLE t1,t2,t3;
#
# LP bug #806504: right join over a view/derived table
#
CREATE TABLE t1 (a int, b int) ;
INSERT INTO t1 VALUES (0,0);
CREATE TABLE t2 (a int) ;
INSERT INTO t2 VALUES (0), (0);
CREATE VIEW v1 AS SELECT * FROM t1;
SELECT * FROM t2 RIGHT JOIN (SELECT * FROM t1) AS t ON t.a != 0
WHERE t.a IN (SELECT b FROM t1);
a a b
NULL 0 0
EXPLAIN EXTENDED
SELECT * FROM t2 RIGHT JOIN (SELECT * FROM t1) AS t ON t.a != 0
WHERE t.a IN (SELECT b FROM t1);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 system NULL NULL NULL NULL 1 100.00
1 PRIMARY t1 system NULL NULL NULL NULL 1 100.00
1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00 Using where
Warnings:
Note 1003 select `test`.`t2`.`a` AS `a`,0 AS `a`,0 AS `b` from `test`.`t2` semi join (dual) where 1
SELECT * FROM t2 RIGHT JOIN v1 AS t ON t.a != 0
WHERE t.a IN (SELECT b FROM t1);
a a b
NULL 0 0
EXPLAIN EXTENDED
SELECT * FROM t2 RIGHT JOIN v1 AS t ON t.a != 0
WHERE t.a IN (SELECT b FROM t1);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 system NULL NULL NULL NULL 1 100.00
1 PRIMARY t1 system NULL NULL NULL NULL 1 100.00
1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00 Using where
Warnings:
Note 1003 select `test`.`t2`.`a` AS `a`,0 AS `a`,0 AS `b` from `test`.`t2` semi join (dual) where 1
DROP VIEW v1;
DROP TABLE t1,t2;
#
# LP bug #809206: DISTINCT in derived table / view
#
CREATE TABLE t1 (a int) ;
INSERT INTO t1 VALUES (0);
CREATE TABLE t2 (a varchar(32), b int, KEY (a)) CHARSET=latin1;
INSERT INTO t2 VALUES
('j',28), ('c',29), ('i',26), ('c',29), ('k',27),
('j',28), ('c',29), ('i',25), ('d',26), ('k',27),
('n',28), ('d',29), ('m',26), ('e',29), ('p',27),
('w',28), ('x',29), ('y',25), ('z',26), ('s',27);
CREATE TABLE t3 (a varchar(32)) CHARSET=latin1;
INSERT INTO t3 VALUES ('j'), ('c');
CREATE VIEW v1 AS SELECT DISTINCT t2.b FROM t1,t2,t3 WHERE t3.a = t2.a;
SELECT DISTINCT t2.b FROM t1,t2,t3 WHERE t3.a = t2.a;
b
28
29
EXPLAIN
SELECT DISTINCT t2.b FROM t1,t2,t3 WHERE t3.a = t2.a;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 system NULL NULL NULL NULL 1 Using temporary
1 SIMPLE t3 ALL NULL NULL NULL NULL 2 Using where
1 SIMPLE t2 ref a a 35 test.t3.a 2
SELECT * FROM (SELECT DISTINCT t2.b FROM t1,t2,t3 WHERE t3.a = t2.a) t;
b
28
29
EXPLAIN
SELECT * FROM (SELECT DISTINCT t2.b FROM t1,t2,t3 WHERE t3.a = t2.a) t;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 4
2 DERIVED t1 system NULL NULL NULL NULL 1 Using temporary
2 DERIVED t3 ALL NULL NULL NULL NULL 2 Using where
2 DERIVED t2 ref a a 35 test.t3.a 2
SELECT * FROM v1;
b
28
29
EXPLAIN
SELECT * FROM v1;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 4
2 DERIVED t1 system NULL NULL NULL NULL 1 Using temporary
2 DERIVED t3 ALL NULL NULL NULL NULL 2 Using where
2 DERIVED t2 ref a a 35 test.t3.a 2
DROP VIEW v1;
DROP TABLE t1,t2,t3;
#
# LP bug #809179: right join over a derived table / view
#
CREATE TABLE t1 (a int, b int);
INSERT INTO t1 VALUES (6,5);
CREATE TABLE t2 (a int, b int);
INSERT INTO t2 VALUES (1,0);
CREATE TABLE t3 (a int, b int);
INSERT INTO t3 VALUES (6,5);
CREATE VIEW v1 AS SELECT * FROM t1;
SELECT t.a,t.b FROM t3 RIGHT JOIN (t1 AS t, t2) ON t2.b != 0
WHERE (t.a,t.b) NOT IN (SELECT 7, 5);
a b
6 5
EXPLAIN EXTENDED
SELECT t.a,t.b FROM t3 RIGHT JOIN (t1 AS t, t2) ON t2.b != 0
WHERE (t.a,t.b) NOT IN (SELECT 7, 5);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t system NULL NULL NULL NULL 1 100.00
1 PRIMARY t2 system NULL NULL NULL NULL 1 100.00
1 PRIMARY t3 system NULL NULL NULL NULL 1 100.00
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
Warnings:
Note 1003 /* select#1 */ select 6 AS `a`,5 AS `b` from `test`.`t3` where 1
SELECT t.a,t.b FROM t3 RIGHT JOIN ((SELECT * FROM t1) AS t, t2) ON t2.b != 0
WHERE (t.a,t.b) NOT IN (SELECT 7, 5);
a b
6 5
EXPLAIN EXTENDED
SELECT t.a,t.b FROM t3 RIGHT JOIN ((SELECT * FROM t1) AS t, t2) ON t2.b != 0
WHERE (t.a,t.b) NOT IN (SELECT 7, 5);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 system NULL NULL NULL NULL 1 100.00
1 PRIMARY t2 system NULL NULL NULL NULL 1 100.00
1 PRIMARY t3 system NULL NULL NULL NULL 1 100.00
3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
Warnings:
Note 1003 /* select#1 */ select 6 AS `a`,5 AS `b` from `test`.`t3` where 1
SELECT t.a,t.b FROM t3 RIGHT JOIN (v1 AS t, t2) ON t2.b != 0
WHERE (t.a,t.b) NOT IN (SELECT 7, 5);
a b
6 5
EXPLAIN EXTENDED
SELECT t.a,t.b FROM t3 RIGHT JOIN (v1 AS t, t2) ON t2.b != 0
WHERE (t.a,t.b) NOT IN (SELECT 7, 5);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 system NULL NULL NULL NULL 1 100.00
1 PRIMARY t2 system NULL NULL NULL NULL 1 100.00
1 PRIMARY t3 system NULL NULL NULL NULL 1 100.00
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
Warnings:
Note 1003 /* select#1 */ select 6 AS `a`,5 AS `b` from `test`.`t3` where 1
DROP VIEW v1;
DROP TABLE t1,t2,t3;
#
# LP bug #794901: insert into a multi-table view
#
CREATE TABLE t1 (a int);
CREATE TABLE t2 (a int);
CREATE TABLE t3 (a int);
CREATE VIEW v1 AS SELECT t1.a FROM t1,t2;
CREATE VIEW v2 AS SELECT a FROM t2 GROUP BY a;
CREATE VIEW v3 AS SELECT v1.a FROM v1,v2;
INSERT INTO v3(a) VALUES (1);
ERROR HY000: The target table v3 of the INSERT is not insertable-into
DROP VIEW v1,v2,v3;
DROP TABLE t1,t2,t3;
#
# LP bug #793448: materialized view accessed by two-component key
#
CREATE TABLE t1 (a int, b int);
INSERT INTO t1 VALUES (9,3), (2,5);
CREATE TABLE t2 (a int, b int);
INSERT INTO t2 VALUES (9,3), (3,7), (9,1), (2,5), (2,4), (3,8);
CREATE TABLE t3 (a int, b int);
INSERT INTO t3 VALUES (10,3), (9,7), (9,1), (2,4);
CREATE VIEW v1(a,b) AS SELECT a, MAX(b) FROM t2 GROUP BY a;
CREATE VIEW v2(a,b) AS SELECT a,b FROM t2 UNION SELECT a,b FROM t3;
SELECT * FROM v1;
a b
2 5
3 8
9 3
SELECT a FROM t1 WHERE (a,b) IN (SELECT * FROM v1);
a
9
2
EXPLAIN
SELECT a FROM t1 WHERE (a,b) IN (SELECT * FROM v1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
1 PRIMARY <derived3> ref key0 key0 10 test.t1.a,test.t1.b 1 FirstMatch(t1)
3 DERIVED t2 ALL NULL NULL NULL NULL 6 Using temporary; Using filesort
SELECT * FROM v2;
a b
9 3
3 7
9 1
2 5
2 4
3 8
10 3
9 7
SELECT a FROM t1 WHERE (a,b) IN (SELECT * FROM v2);
a
9
2
EXPLAIN
SELECT a FROM t1 WHERE (a,b) IN (SELECT * FROM v2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
1 PRIMARY <derived3> eq_ref distinct_key distinct_key 10 test.t1.a,test.t1.b 1
3 DERIVED t2 ALL NULL NULL NULL NULL 6
4 UNION t3 ALL NULL NULL NULL NULL 4
NULL UNION RESULT <union3,4> ALL NULL NULL NULL NULL NULL
DROP VIEW v1,v2;
DROP TABLE t1,t2,t3;
#
# LP bug #804686: query over a derived table using a view
# with a degenerated where condition
#
CREATE TABLE t1 (a int, b int);
INSERT INTO t1 VALUES (0,0), (1,0), (0,0), (1,1), (1,0);
CREATE VIEW v1 AS SELECT a,b FROM t1;
CREATE VIEW v2 AS SELECT a, MAX(b) AS b FROM t1 GROUP BY a;
SELECT * FROM (SELECT b FROM v1 WHERE b = 0) t WHERE b<>0;
b
SELECT * FROM (SELECT b FROM v2 WHERE b = 0) t WHERE b<>0;
b
SELECT * FROM (SELECT b FROM v1 WHERE b = 0) t WHERE b;
b
SELECT * FROM (SELECT b FROM v2 WHERE b = 0) t WHERE b;
b
EXPLAIN EXTENDED
SELECT * FROM (SELECT b FROM v1 WHERE b = 0) t WHERE b;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
Warnings:
Note 1003 select `test`.`t1`.`b` AS `b` from `test`.`t1` where 0
EXPLAIN EXTENDED
SELECT * FROM (SELECT b FROM v2 WHERE b = 0) t WHERE b;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
3 DERIVED t1 ALL NULL NULL NULL NULL 5 100.00 Using temporary; Using filesort
Warnings:
Note 1003 /* select#1 */ select `v2`.`b` AS `b` from `test`.`v2` where 0
DROP VIEW v1,v2;
DROP TABLE t1;
#
# LP bug #819716: crash with embedded tableless materialized derived
# with a variable
#
set optimizer_switch='derived_merge=off';
EXPLAIN
SELECT * FROM (SELECT * FROM (SELECT @b) AS t) AS s;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> system NULL NULL NULL NULL 1
2 DERIVED <derived3> system NULL NULL NULL NULL 1
3 DERIVED NULL NULL NULL NULL NULL NULL NULL No tables used
SELECT * FROM (SELECT * FROM (SELECT @b) AS t) AS s;
@b
NULL
set optimizer_switch='derived_merge=on';
#
# LP bug #823826: view over join + IS NULL in WHERE
#
CREATE TABLE t1 (a int) ;
INSERT INTO t1 VALUES (1), (1);
CREATE TABLE t2 (b int) ;
INSERT INTO t2 VALUES (9), (NULL), (7);
CREATE VIEW v1 AS SELECT * FROM t1,t2;
EXPLAIN
SELECT * FROM (SELECT * FROM t1,t2) t WHERE b IS NULL;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 2
1 SIMPLE t2 ALL NULL NULL NULL NULL 3 Using where; Using join buffer (flat, BNL join)
SELECT * FROM (SELECT * FROM t1,t2) t WHERE b IS NULL;
a b
1 NULL
1 NULL
EXPLAIN
SELECT * FROM v1 WHERE b IS NULL;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 2
1 SIMPLE t2 ALL NULL NULL NULL NULL 3 Using where; Using join buffer (flat, BNL join)
SELECT * FROM v1 WHERE b IS NULL;
a b
1 NULL
1 NULL
DROP VIEW v1;
DROP TABLE t1,t2;
#
# LP bug #823835: a duplicate of #823189 with derived table
#
CREATE TABLE t1 (a varchar(32)) ;
INSERT INTO t1 VALUES ('r'), ('p');
CREATE TABLE t2 (a int NOT NULL, b varchar(32)) ;
INSERT INTO t2 VALUES (28,'j');
CREATE TABLE t3 (a int);
INSERT INTO t3 VALUES (0), (0);
EXPLAIN EXTENDED
SELECT * FROM (SELECT * FROM t1) AS t
WHERE EXISTS (SELECT t2.a FROM t3 RIGHT JOIN t2 ON (t3.a = t2.a)
WHERE t2.b < t.a);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 Using where
3 DEPENDENT SUBQUERY t2 system NULL NULL NULL NULL 1 100.00
3 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 2 100.00 Using where
Warnings:
Note 1276 Field or reference 't.a' of SELECT #3 was resolved in SELECT #1
Note 1003 /* select#1 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` where <in_optimizer>(1,<expr_cache><`test`.`t1`.`a`>(exists(/* select#3 */ select 28 from `test`.`t3` where 'j' < `test`.`t1`.`a` limit 1)))
SELECT * FROM (SELECT * FROM t1) AS t
WHERE EXISTS (SELECT t2.a FROM t3 RIGHT JOIN t2 ON (t3.a = t2.a)
WHERE t2.b < t.a);
a
r
p
DROP TABLE t1,t2,t3;
#
# LP bug #824463: nested outer join using a merged view
# as an inner table
#
CREATE TABLE t1 (b int, a int) ;
CREATE TABLE t2 (a int) ;
INSERT INTO t2 VALUES (5), (6);
CREATE TABLE t3 (a int , c int) ;
INSERT INTO t3 VALUES (22,1), (23,-1);
CREATE TABLE t4 (a int);
CREATE TABLE t5 (d int) ;
INSERT INTO t5 VALUES (0), (7), (3), (5);
CREATE VIEW v2 AS SELECT * FROM t2;
CREATE VIEW v3 AS SELECT * FROM t3;
EXPLAIN EXTENDED
SELECT STRAIGHT_JOIN *
FROM ( t2 AS s2
JOIN
( t3 AS s3
LEFT JOIN
( t4 LEFT JOIN t3 ON t4.a != 0 )
ON s3.a != 0)
ON s2.a != 0)
JOIN t5 ON s3.c != 0 AND t5.d = 0;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE s2 ALL NULL NULL NULL NULL 2 100.00 Using where
1 SIMPLE s3 ALL NULL NULL NULL NULL 2 100.00 Using where; Using join buffer (flat, BNL join)
1 SIMPLE t4 ALL NULL NULL NULL NULL 0 0.00 Using where
1 SIMPLE t3 ALL NULL NULL NULL NULL 2 100.00 Using where
1 SIMPLE t5 ALL NULL NULL NULL NULL 4 100.00 Using where; Using join buffer (flat, BNL join)
Warnings:
Note 1003 select straight_join `test`.`s2`.`a` AS `a`,`test`.`s3`.`a` AS `a`,`test`.`s3`.`c` AS `c`,`test`.`t4`.`a` AS `a`,`test`.`t3`.`a` AS `a`,`test`.`t3`.`c` AS `c`,`test`.`t5`.`d` AS `d` from `test`.`t2` `s2` join `test`.`t3` `s3` left join (`test`.`t4` left join `test`.`t3` on(`test`.`t4`.`a` <> 0)) on(`test`.`s3`.`a` <> 0) join `test`.`t5` where `test`.`t5`.`d` = 0 and `test`.`s3`.`c` <> 0 and `test`.`s2`.`a` <> 0
SELECT STRAIGHT_JOIN *
FROM ( t2 AS s2
JOIN
( t3 AS s3
LEFT JOIN
( t4 LEFT JOIN t3 ON t4.a != 0 )
ON s3.a != 0)
ON s2.a != 0)
JOIN t5 ON s3.c != 0 AND t5.d = 0;
a a c a a c d
5 22 1 NULL NULL NULL 0
6 22 1 NULL NULL NULL 0
5 23 -1 NULL NULL NULL 0
6 23 -1 NULL NULL NULL 0
EXPLAIN EXTENDED
SELECT STRAIGHT_JOIN *
FROM t2 AS s2 , t5,
(t3 LEFT JOIN (t4 LEFT JOIN t3 AS s3 ON t4.a != 0) ON t3.a != 0)
WHERE s2.a != 0 AND t3.c != 0 AND t5.d = 0;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE s2 ALL NULL NULL NULL NULL 2 100.00 Using where
1 SIMPLE t5 ALL NULL NULL NULL NULL 4 100.00 Using where; Using join buffer (flat, BNL join)
1 SIMPLE t3 ALL NULL NULL NULL NULL 2 100.00 Using where; Using join buffer (flat, BNL join)
1 SIMPLE t4 ALL NULL NULL NULL NULL 0 0.00 Using where
1 SIMPLE s3 ALL NULL NULL NULL NULL 2 100.00 Using where
Warnings:
Note 1003 select straight_join `test`.`s2`.`a` AS `a`,`test`.`t5`.`d` AS `d`,`test`.`t3`.`a` AS `a`,`test`.`t3`.`c` AS `c`,`test`.`t4`.`a` AS `a`,`test`.`s3`.`a` AS `a`,`test`.`s3`.`c` AS `c` from `test`.`t2` `s2` join `test`.`t5` join `test`.`t3` left join (`test`.`t4` left join `test`.`t3` `s3` on(`test`.`t4`.`a` <> 0)) on(`test`.`t3`.`a` <> 0) where `test`.`t5`.`d` = 0 and `test`.`s2`.`a` <> 0 and `test`.`t3`.`c` <> 0
SELECT STRAIGHT_JOIN *
FROM t2 AS s2 , t5,
(t3 LEFT JOIN (t4 LEFT JOIN t3 AS s3 ON t4.a != 0) ON t3.a != 0)
WHERE s2.a != 0 AND t3.c != 0 AND t5.d = 0;
a d a c a a c
5 0 22 1 NULL NULL NULL
6 0 22 1 NULL NULL NULL
5 0 23 -1 NULL NULL NULL
6 0 23 -1 NULL NULL NULL
EXPLAIN EXTENDED
SELECT STRAIGHT_JOIN *
FROM v2 AS s2 , t5,
(t3 LEFT JOIN (t4 LEFT JOIN v3 AS s3 ON t4.a != 0) ON t3.a != 0)
WHERE s2.a != 0 AND t3.c != 0 AND t5.d = 0;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 2 100.00 Using where
1 SIMPLE t5 ALL NULL NULL NULL NULL 4 100.00 Using where; Using join buffer (flat, BNL join)
1 SIMPLE t3 ALL NULL NULL NULL NULL 2 100.00 Using where; Using join buffer (flat, BNL join)
1 SIMPLE t4 ALL NULL NULL NULL NULL 0 0.00 Using where
1 SIMPLE t3 ALL NULL NULL NULL NULL 2 100.00 Using where
Warnings:
Note 1003 select straight_join `test`.`t2`.`a` AS `a`,`test`.`t5`.`d` AS `d`,`test`.`t3`.`a` AS `a`,`test`.`t3`.`c` AS `c`,`test`.`t4`.`a` AS `a`,`test`.`t3`.`a` AS `a`,`test`.`t3`.`c` AS `c` from `test`.`t2` join `test`.`t5` join `test`.`t3` left join (`test`.`t4` left join (`test`.`t3`) on(`test`.`t4`.`a` <> 0)) on(`test`.`t3`.`a` <> 0) where `test`.`t5`.`d` = 0 and `test`.`t2`.`a` <> 0 and `test`.`t3`.`c` <> 0
SELECT STRAIGHT_JOIN *
FROM v2 AS s2 , t5,
(t3 LEFT JOIN (t4 LEFT JOIN v3 AS s3 ON t4.a != 0) ON t3.a != 0)
WHERE s2.a != 0 AND t3.c != 0 AND t5.d = 0;
a d a c a a c
5 0 22 1 NULL NULL NULL
6 0 22 1 NULL NULL NULL
5 0 23 -1 NULL NULL NULL
6 0 23 -1 NULL NULL NULL
SELECT STRAIGHT_JOIN *
FROM ( ( t2 AS s2
LEFT JOIN
( t3 AS s3
LEFT JOIN
( t4 AS s4 JOIN t3 ON s4.a != 0)
ON s3.a != 0 )
ON s2.a != 0)
LEFT JOIN
t1 AS s1
ON s1.a != 0)
JOIN t5 ON s3.c != 0;
a a c a a c b a d
5 22 1 NULL NULL NULL NULL NULL 0
6 22 1 NULL NULL NULL NULL NULL 0
5 23 -1 NULL NULL NULL NULL NULL 0
6 23 -1 NULL NULL NULL NULL NULL 0
5 22 1 NULL NULL NULL NULL NULL 7
6 22 1 NULL NULL NULL NULL NULL 7
5 23 -1 NULL NULL NULL NULL NULL 7
6 23 -1 NULL NULL NULL NULL NULL 7
5 22 1 NULL NULL NULL NULL NULL 3
6 22 1 NULL NULL NULL NULL NULL 3
5 23 -1 NULL NULL NULL NULL NULL 3
6 23 -1 NULL NULL NULL NULL NULL 3
5 22 1 NULL NULL NULL NULL NULL 5
6 22 1 NULL NULL NULL NULL NULL 5
5 23 -1 NULL NULL NULL NULL NULL 5
6 23 -1 NULL NULL NULL NULL NULL 5
SELECT STRAIGHT_JOIN *
FROM ( ( v2 AS s2
LEFT JOIN
( v3 AS s3
LEFT JOIN
( t4 AS s4 JOIN v3 ON s4.a != 0)
ON s3.a != 0 )
ON s2.a != 0)
LEFT JOIN
t1 AS s1
ON s1.a != 0)
JOIN t5 ON s3.c != 0;
a a c a a c b a d
5 22 1 NULL NULL NULL NULL NULL 0
6 22 1 NULL NULL NULL NULL NULL 0
5 23 -1 NULL NULL NULL NULL NULL 0
6 23 -1 NULL NULL NULL NULL NULL 0
5 22 1 NULL NULL NULL NULL NULL 7
6 22 1 NULL NULL NULL NULL NULL 7
5 23 -1 NULL NULL NULL NULL NULL 7
6 23 -1 NULL NULL NULL NULL NULL 7
5 22 1 NULL NULL NULL NULL NULL 3
6 22 1 NULL NULL NULL NULL NULL 3
5 23 -1 NULL NULL NULL NULL NULL 3
6 23 -1 NULL NULL NULL NULL NULL 3
5 22 1 NULL NULL NULL NULL NULL 5
6 22 1 NULL NULL NULL NULL NULL 5
5 23 -1 NULL NULL NULL NULL NULL 5
6 23 -1 NULL NULL NULL NULL NULL 5
DROP VIEW v2,v3;
DROP TABLE t1,t2,t3,t4,t5;
#
# LP bug #872735: derived used in a NOT IN subquery
#
CREATE TABLE t1 (b int NOT NULL);
INSERT INTO t1 VALUES (9), (7);
CREATE TABLE t2 (a int NOT NULL) ;
INSERT INTO t2 VALUES (1), (2);
CREATE TABLE t3 (
a int NOT NULL , c int NOT NULL, d varchar(1) NOT NULL,
KEY (c,a) , PRIMARY KEY (a)
);
INSERT INTO t3 VALUES
(14,4,'a'), (15,7,'b'), (16,4,'c'), (17,1,'d'), (18,9,'e'),
(19,4,'f'), (20,8,'g');
SET SESSION optimizer_switch='derived_merge=on,subquery_cache=off,materialization=off';
# The following two EXPLAINs must return the same execution plan
EXPLAIN
SELECT * FROM t1 , t2
WHERE (t2.a ,t1.b) NOT IN (SELECT DISTINCT c,a FROM t3 t);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 2
1 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
2 DEPENDENT SUBQUERY t index_subquery PRIMARY,c c 8 func,func 1 Using index; Using where
EXPLAIN
SELECT * FROM t1 , t2
WHERE (t2.a ,t1.b) NOT IN (SELECT DISTINCT c,a FROM (SELECT * FROM t3) t);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 2
1 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
2 DEPENDENT SUBQUERY t3 index_subquery PRIMARY,c c 8 func,func 1 Using index; Using where
SELECT * FROM t1 , t2
WHERE (t2.a ,t1.b) NOT IN (SELECT DISTINCT c,a FROM (SELECT * FROM t3) t);
b a
9 1
7 1
9 2
7 2
DROP TABLE t1,t2,t3;
#
# LP bug #874006: materialized view used in IN subquery
#
CREATE TABLE t3 (a int NOT NULL, b varchar(1), c varchar(1)) CHARSET=latin1;
INSERT INTO t3 VALUES (19,NULL,NULL), (20,'r','r');
CREATE TABLE t1 (a int, b varchar(1) , c varchar(1)) CHARSET=latin1;
INSERT INTO t1 VALUES (1,NULL,NULL), (5,'r','r'), (7,'y','y');
CREATE TABLE t2 (a int NOT NULL , b int, c varchar(1)) CHARSET=latin1;
INSERT INTO t2 VALUES (4,3,'r');
CREATE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t1;
SET @save_optimizer_switch=@@optimizer_switch;
SET optimizer_switch='semijoin_with_cache=off';
SET SESSION optimizer_switch='derived_with_keys=off';
EXPLAIN
SELECT * FROM t3
WHERE t3.b IN (SELECT v1.b FROM v1, t2
WHERE t2.c = v1.c AND t2.c = v1.b AND v1.b = t3.c);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 system NULL NULL NULL NULL 1
1 PRIMARY t3 ALL NULL NULL NULL NULL 2 Using where
1 PRIMARY <derived3> ALL NULL NULL NULL NULL 3 Using where; FirstMatch(t3)
3 DERIVED t1 ALL NULL NULL NULL NULL 3 Using where
SELECT * FROM t3
WHERE t3.b IN (SELECT v1.b FROM v1, t2
WHERE t2.c = v1.c AND t2.c = v1.b AND v1.b = t3.c);
a b c
20 r r
SET SESSION optimizer_switch='derived_with_keys=on';
EXPLAIN
SELECT * FROM t3
WHERE t3.b IN (SELECT v1.b FROM v1, t2
WHERE t2.c = v1.c AND t2.c = v1.b AND v1.b = t3.c);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 system NULL NULL NULL NULL 1
1 PRIMARY t3 ALL NULL NULL NULL NULL 2 Using where
1 PRIMARY <derived3> ref key1 key1 8 const,const 0 FirstMatch(t3)
3 DERIVED t1 ALL NULL NULL NULL NULL 3 Using where
SELECT * FROM t3
WHERE t3.b IN (SELECT v1.b FROM v1, t2
WHERE t2.c = v1.c AND t2.c = v1.b AND v1.b = t3.c);
a b c
20 r r
SET optimizer_switch=@save_optimizer_switch;
DROP VIEW v1;
DROP TABLE t1,t2,t3;
#
# LP bug #873263: materialized view used in correlated IN subquery
#
CREATE TABLE t1 (a int, b int) ;
INSERT INTO t1 VALUES (5,4), (9,8);
CREATE TABLE t2 (a int, b int) ;
INSERT INTO t2 VALUES (4,5), (5,1);
CREATE ALGORITHM=TEMPTABLE VIEW v2 AS SELECT * FROM t2;
SET SESSION optimizer_switch='derived_with_keys=on';
EXPLAIN
SELECT * FROM t1 WHERE t1.b IN (SELECT v2.a FROM v2 WHERE v2.b = t1.a);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
1 PRIMARY <derived3> ref key0 key0 10 test.t1.b,test.t1.a 1 FirstMatch(t1)
3 DERIVED t2 ALL NULL NULL NULL NULL 2
SELECT * FROM t1 WHERE t1.b IN (SELECT v2.a FROM v2 WHERE v2.b = t1.a);
a b
5 4
DROP VIEW v2;
DROP TABLE t1,t2;
#
# LP bug #877316: query over a view with correlated subquery in WHERE
#
CREATE TABLE t1 (a int, b int, PRIMARY KEY (a)) ;
INSERT INTO t1 VALUES (18,2), (19,9);
CREATE TABLE t2 (a int, b int) ;
INSERT INTO t2 VALUES (10,8), (5,10);
CREATE VIEW v1 AS SELECT * FROM t1;
SELECT t1.a FROM t1
WHERE EXISTS (SELECT t2.a FROM t2 WHERE t2.b < t1.b);
a
19
EXPLAIN
SELECT t1.a FROM t1
WHERE EXISTS (SELECT t2.a FROM t2 WHERE t2.b < t1.b);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where
SELECT v1.a FROM v1
WHERE EXISTS (SELECT t2.a FROM t2 WHERE t2.b < v1.b);
a
19
EXPLAIN
SELECT v1.a FROM v1
WHERE EXISTS (SELECT t2.a FROM t2 WHERE t2.b < v1.b);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where
DROP VIEW v1;
DROP TABLE t1,t2;
#
# LP bug #878199: join of two materialized views
#
CREATE TABLE t1 (a int, b varchar(1)) CHARSET=latin1;
INSERT INTO t1 VALUES (7,'c'), (3,'h'), (7,'c');
CREATE TABLE t2 (b varchar(1)) CHARSET=latin1;
INSERT INTO t2 VALUES ('p'), ('c'), ('j'), ('c'), ('p');
CREATE VIEW v1 AS SELECT * FROM t1 GROUP BY a,b;
CREATE VIEW v2 AS SELECT * FROM t2 GROUP BY b;
SET SESSION optimizer_switch = 'derived_with_keys=on';
SELECT v1.a FROM v1,v2 WHERE v2.b = v1.b ORDER BY 1;
a
7
EXPLAIN
SELECT v1.a FROM v1,v2 WHERE v2.b = v1.b ORDER BY 1;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 3 Using where; Using filesort
1 PRIMARY <derived3> ref key0 key0 4 v1.b 1
3 DERIVED t2 ALL NULL NULL NULL NULL 5 Using temporary; Using filesort
2 DERIVED t1 ALL NULL NULL NULL NULL 3 Using temporary; Using filesort
DROP VIEW v1,v2;
DROP TABLE t1,t2;
#
# Bug #743378: join over merged view employing BNL
#
CREATE TABLE t1 ( d varchar(1) NOT NULL) CHARSET=latin1;
INSERT INTO t1 VALUES ('j'),('v'),('c');
CREATE TABLE t2 (h time NOT NULL, d varchar(1) NOT NULL) CHARSET=latin1;
INSERT INTO t2 VALUES ('05:03:03','w'),('02:59:24','d'),('00:01:58','e');
CREATE TABLE t3 (
b int NOT NULL, e varchar(1) NOT NULL, d varchar(1) NOT NULL, KEY (e,b)
) CHARSET=latin1;
INSERT INTO t3 VALUES (4,'x','x'),(9,'w','w'),(4,'d','d'),(8,'e','e');
INSERT INTO t3 VALUES (14,'a','a'),(19,'b','b'),(14,'c','c'),(18,'d','d');
CREATE TABLE t4 (i int NOT NULL, m varchar(1) NOT NULL) CHARSET=latin1;
INSERT INTO t4 VALUES (8,'m'),(9,'d'),(2,'s'),(4,'r'),(8,'m');
CREATE TABLE t5 (
a int NOT NULL, c int NOT NULL, b int NOT NULL, f date NOT NULL,
g date NOT NULL, h time NOT NULL, j time NOT NULL, k datetime NOT NULL
);
INSERT INTO t5 VALUES
(1,4,0,'0000-00-00','0000-00-00','21:22:34','21:22:34','2002-02-13 17:30'),
(2,6,8,'2004-09-18','2004-09-18','10:50:38','10:50:38','2008-09-27 00:34');
CREATE VIEW v3 AS SELECT t3.*, t4.i FROM t3, t4, t5;
SET SESSION join_cache_level = 1;
SET SESSION join_buffer_size = 512;
EXPLAIN
SELECT t2.d FROM t1,t2,v3 WHERE v3.e = t2.d AND v3.i < 3;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t5 ALL NULL NULL NULL NULL 2
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
1 SIMPLE t2 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
1 SIMPLE t3 ref e e 3 test.t2.d 1 Using index
1 SIMPLE t4 ALL NULL NULL NULL NULL 5 Using where; Using join buffer (flat, BNL join)
SELECT t2.d FROM t1,t2,v3 WHERE v3.e = t2.d AND v3.i < 3;
d
w
w
w
w
w
w
d
d
d
d
d
d
d
d
d
d
d
d
e
e
e
e
e
e
SET SESSION join_cache_level = @exit_join_cache_level;
SET SESSION join_buffer_size = @exit_join_buffer_size;
DROP VIEW v3;
DROP TABLE t1,t2,t3,t4,t5;
#
# Bug #879882: right join within mergeable derived table
#
CREATE TABLE t1 (a varchar(1));
INSERT INTO t1 VALUES ('c'), ('a');
CREATE TABLE t2 (a int, b int, c varchar(1));
INSERT INTO t2 VALUES (29,8,'c'), (39,7,'b');
CREATE TABLE t3 (b int);
SET @save_optimizer_switch=@@optimizer_switch;
SET optimizer_switch='outer_join_with_cache=off';
EXPLAIN EXTENDED
SELECT t.b, t.c, t1.a
FROM t1, (SELECT t2.b, t2.c FROM t3 RIGHT JOIN t2 ON t2.a = t3.b) AS t
WHERE t.b AND t.c = t1.a;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t3 system NULL NULL NULL NULL 0 0.00 Const row not found
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00
1 SIMPLE t2 ALL NULL NULL NULL NULL 2 100.00 Using where; Using join buffer (flat, BNL join)
Warnings:
Note 1003 select `test`.`t2`.`b` AS `b`,`test`.`t2`.`c` AS `c`,`test`.`t1`.`a` AS `a` from `test`.`t1` join `test`.`t2` where `test`.`t2`.`c` = `test`.`t1`.`a` and `test`.`t2`.`b` <> 0
SELECT t.b, t.c, t1.a
FROM t1, (SELECT t2.b, t2.c FROM t3 RIGHT JOIN t2 ON t2.a = t3.b) AS t
WHERE t.b AND t.c = t1.a;
b c a
8 c c
EXPLAIN EXTENDED
SELECT t.b, t.c, t1.a
FROM t1, (SELECT t2.b, t2.c FROM t3 RIGHT JOIN t2 ON t2.a = t3.b) AS t
WHERE t.b <> 0 AND t.c = t1.a;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t3 system NULL NULL NULL NULL 0 0.00 Const row not found
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00
1 SIMPLE t2 ALL NULL NULL NULL NULL 2 100.00 Using where; Using join buffer (flat, BNL join)
Warnings:
Note 1003 select `test`.`t2`.`b` AS `b`,`test`.`t2`.`c` AS `c`,`test`.`t1`.`a` AS `a` from `test`.`t1` join `test`.`t2` where `test`.`t2`.`c` = `test`.`t1`.`a` and `test`.`t2`.`b` <> 0
SELECT t.b, t.c, t1.a
FROM t1, (SELECT t2.b, t2.c FROM t3 RIGHT JOIN t2 ON t2.a = t3.b) AS t
WHERE t.b <> 0 AND t.c = t1.a;
b c a
8 c c
INSERT INTO t3 VALUES (100), (200);
EXPLAIN EXTENDED
SELECT t.b, t.c, t1.a
FROM t1, (SELECT t2.b, t2.c FROM t3 RIGHT JOIN t2 ON t2.a = t3.b) AS t
WHERE t.b AND t.c = t1.a;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00
1 SIMPLE t2 ALL NULL NULL NULL NULL 2 100.00 Using where; Using join buffer (flat, BNL join)
1 SIMPLE t3 ALL NULL NULL NULL NULL 2 100.00 Using where
Warnings:
Note 1003 select `test`.`t2`.`b` AS `b`,`test`.`t2`.`c` AS `c`,`test`.`t1`.`a` AS `a` from `test`.`t1` join `test`.`t2` left join `test`.`t3` on(`test`.`t3`.`b` = `test`.`t2`.`a`) where `test`.`t2`.`c` = `test`.`t1`.`a` and `test`.`t2`.`b` <> 0
SELECT t.b, t.c, t1.a
FROM t1, (SELECT t2.b, t2.c FROM t3 RIGHT JOIN t2 ON t2.a = t3.b) AS t
WHERE t.b AND t.c = t1.a;
b c a
8 c c
EXPLAIN EXTENDED
SELECT t.b, t.c, t1.a
FROM t1, (SELECT t2.b, t2.c FROM t3 RIGHT JOIN t2 ON t2.a = t3.b) AS t
WHERE t.b <> 0 AND t.c = t1.a;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00
1 SIMPLE t2 ALL NULL NULL NULL NULL 2 100.00 Using where; Using join buffer (flat, BNL join)
1 SIMPLE t3 ALL NULL NULL NULL NULL 2 100.00 Using where
Warnings:
Note 1003 select `test`.`t2`.`b` AS `b`,`test`.`t2`.`c` AS `c`,`test`.`t1`.`a` AS `a` from `test`.`t1` join `test`.`t2` left join `test`.`t3` on(`test`.`t3`.`b` = `test`.`t2`.`a`) where `test`.`t2`.`c` = `test`.`t1`.`a` and `test`.`t2`.`b` <> 0
SELECT t.b, t.c, t1.a
FROM t1, (SELECT t2.b, t2.c FROM t3 RIGHT JOIN t2 ON t2.a = t3.b) AS t
WHERE t.b <> 0 AND t.c = t1.a;
b c a
8 c c
SET optimizer_switch=@save_optimizer_switch;
DROP TABLE t1,t2,t3;
#
# Bug #880724: materialized const view as inner table of outer join
#
CREATE TABLE t1 (a int, b varchar(1));
INSERT INTO t1 VALUES (9,NULL), (6,'r'), (7,'c');
CREATE TABLE t2 (a int);
INSERT INTO t2 VALUES (6);
CREATE ALGORITHM=TEMPTABLE VIEW v2 AS SELECT * FROM t2;
SET @save_optimizer_switch=@@optimizer_switch;
SET optimizer_switch='outer_join_with_cache=off';
SET SESSION optimizer_switch = 'derived_with_keys=on';
SET SESSION join_cache_level = 4;
EXPLAIN
SELECT t1.b,v2.a FROM t1 LEFT JOIN v2 ON v2.a = t1.a;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 1 Using where
2 DERIVED t2 system NULL NULL NULL NULL 1
SELECT t1.b,v2.a FROM t1 LEFT JOIN v2 ON v2.a = t1.a;
b a
NULL NULL
r 6
c NULL
CREATE TABLE t3 (a int, b varchar(1));
INSERT INTO t3 VALUES (8,'x'), (5,'r'), (9,'y');
EXPLAIN
SELECT * FROM t3
WHERE t3.b <> ANY (SELECT t1.b FROM t1 LEFT JOIN v2 ON v2.a = t1.a);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t3 ALL NULL NULL NULL NULL 3 Using where
2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 3 Using where
2 DEPENDENT SUBQUERY <derived3> ALL NULL NULL NULL NULL 1 Using where
3 DERIVED t2 system NULL NULL NULL NULL 1
SELECT * FROM t3
WHERE t3.b <> ANY (SELECT t1.b FROM t1 LEFT JOIN v2 ON v2.a = t1.a);
a b
8 x
5 r
9 y
SET SESSION join_cache_level = @exit_join_cache_level;
SET optimizer_switch=@save_optimizer_switch;
DROP VIEW v2;
DROP TABLE t1,t2,t3;
#
# Bug #881449: OUTER JOIN usin a merged view within IN subquery
#
CREATE TABLE t1 (a varchar(1)) ;
INSERT INTO t1 VALUES ('y'), ('x');
CREATE TABLE t2 (a int, PRIMARY KEY (a)) ;
INSERT INTO t2 VALUES (1), (2);
CREATE TABLE t3 (a int, b varchar(1)) ;
INSERT INTO t3 VALUES (1,'x');
CREATE VIEW v3 AS SELECT * FROM t3;
SET SESSION optimizer_switch='semijoin=on';
EXPLAIN
SELECT * FROM t1 WHERE a IN (SELECT v3.b FROM t2 RIGHT JOIN v3 ON v3.a = t2.a);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t3 system NULL NULL NULL NULL 1
1 PRIMARY t2 const PRIMARY PRIMARY 4 const 1 Using index
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
SELECT * FROM t1 WHERE a IN (SELECT v3.b FROM t2 RIGHT JOIN v3 ON v3.a = t2.a);
a
x
set optimizer_switch= @save_optimizer_switch;
DROP VIEW v3;
DROP TABLE t1,t2,t3;
#
# Bug #874035: view as an inner table of a materialized derived
#
CREATE TABLE t2 (a int NOT NULL);
INSERT INTO t2 VALUES (7), (4);
CREATE TABLE t1 (b int NOT NULL);
INSERT INTO t1 VALUES (5), (7);
CREATE ALGORITHM=MERGE VIEW v1 AS SELECT * FROM t1;
SET @save_optimizer_switch=@@optimizer_switch;
SET SESSION optimizer_switch='derived_merge=off';
PREPARE st1 FROM
'SELECT * FROM (SELECT * FROM t2 LEFT JOIN v1 ON t2.a = v1.b) AS t';
EXECUTE st1;
a b
7 7
4 NULL
EXECUTE st1;
a b
7 7
4 NULL
DEALLOCATE PREPARE st1;
set SESSION optimizer_switch= @save_optimizer_switch;
DROP VIEW v1;
DROP TABLE t1,t2;
#
# LP bug #879939: assertion in ha_maria::enable_indexes
# with derived_with_keys=on
#
CREATE TABLE t2 (a varchar(3));
INSERT INTO t2 VALUES ('USA'), ('USA'), ('USA'), ('USA'), ('USA');
CREATE TABLE t1 (a varchar(3), b varchar(35));
INSERT INTO t1 VALUES
('USA','Lansing'), ('USA','Laredo'), ('USA','Las Vegas'),
('USA','Lexington-Fayett'), ('USA','Lincoln'), ('USA','Little Rock'),
('USA','Livonia'), ('USA','Long Beach'), ('USA','Los Angeles'),
('USA','Louisville'), ('USA','Lowell'), ('USA','Lubbock'),
('USA','Macon'), ('USA','Madison'), ('USA','Manchester'),
('USA','McAllen'), ('USA','Memphis'), ('USA','Mesa'),
('USA','Mesquite'), ('USA','Metairie'), ('USA','Miami');
CREATE TABLE t3 (a varchar(35));
INSERT INTO t3 VALUES ('Miami');
SET @save_optimizer_switch=@@optimizer_switch;
SET optimizer_switch = 'derived_with_keys=on';
SET @@tmp_table_size=1024*4;
Warnings:
Warning 1292 Truncated incorrect tmp_table_size value: '4096'
explain SELECT * FROM (SELECT t1.* FROM t1, t2) AS t JOIN t3 ON t3.a = t.b;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t3 system NULL NULL NULL NULL 1
1 SIMPLE t2 ALL NULL NULL NULL NULL 5
1 SIMPLE t1 ALL NULL NULL NULL NULL 21 Using where; Using join buffer (flat, BNL join)
SELECT * FROM (SELECT t1.* FROM t1, t2) AS t JOIN t3 ON t3.a = t.b;
a b a
USA Miami Miami
USA Miami Miami
USA Miami Miami
USA Miami Miami
USA Miami Miami
SET @@tmp_table_size=1024*1024*16;
SELECT * FROM (SELECT t1.* FROM t1, t2) AS t JOIN t3 ON t3.a = t.b;
a b a
USA Miami Miami
USA Miami Miami
USA Miami Miami
USA Miami Miami
USA Miami Miami
SET @@tmp_table_size=default;
set SESSION optimizer_switch= @save_optimizer_switch;
drop table t1,t2,t3;
#
# BUG#882994: Crash in QUICK_RANGE_SELECT::reset with derived_with_keys
#
CREATE TABLE t2 (
pk varchar(33),
col_varchar_key varchar(3) NOT NULL,
col_varchar_nokey varchar(52) NOT NULL);
INSERT INTO t2 VALUES ('NICSpanish','NIC','Spanish'),
('NERHausa','NER','Hausa'),('NGAJoruba','NGA','Joruba'),
('NIUNiue','NIU','Niue'),('NFKEnglish','NFK','English'),
('NORNorwegian','NOR','Norwegian'),('CIVAkan','CIV','Akan'),
('OMNArabic','OMN','Arabic'),('PAKPunjabi','PAK','Punjabi'),
('PLWPalau','PLW','Palau'),('PANSpanish','PAN','Spanish'),
('PNGPapuan Langua','PNG','Papuan Languages'), ('PRYSpanish','PRY','Spanish'),
('PERSpanish','PER','Spanish'), ('PCNPitcairnese','PCN','Pitcairnese'),
('MNPPhilippene La','MNP','Philippene Langu'),('PRTPortuguese','PRT','Portuguese'),
('PRISpanish','PRI','Spanish'),('POLPolish','POL','Polish'),('GNQFang','GNQ','Fang');
CREATE TABLE t1 ( col_varchar_nokey varchar(52) NOT NULL ) ;
INSERT INTO t1 VALUES ('Chinese'),('English'),('French'),('German'),
('Italian'),('Japanese'),('Korean'),('Polish'),('Portuguese'),('Spanish'),
('Tagalog'),('Vietnamese');
CREATE TABLE t3 ( col_varchar_key varchar(52)) ;
INSERT INTO t3 VALUES ('United States');
set @tmp_882994= @@max_heap_table_size;
set max_heap_table_size=1;
SELECT *
FROM t3 JOIN
( SELECT t2.* FROM t1, t2 ) AS alias2
ON ( alias2.col_varchar_nokey = t3.col_varchar_key )
ORDER BY CONCAT(alias2.col_varchar_nokey);
col_varchar_key pk col_varchar_key col_varchar_nokey
set max_heap_table_size= @tmp_882994;
drop table t1,t2,t3;
#
# LP bug #917990: Bad estimate of #rows for derived table with LIMIT
#
CREATE TABLE t1 (a int);
INSERT INTO t1 VALUES
(8), (3), (4), (7), (9), (5), (1), (2);
SELECT * FROM (SELECT * FROM t1 LIMIT 3) t;
a
8
3
4
EXPLAIN
SELECT * FROM (SELECT * FROM t1 LIMIT 3) t;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 3
2 DERIVED t1 ALL NULL NULL NULL NULL 8
DROP TABLE t1;
#
# LP BUG#921878 incorrect check of items during columns union types
# aggregation for merged derived tables
#
SET @save_optimizer_switch=@@optimizer_switch;
SET SESSION optimizer_switch='derived_merge=on';
CREATE TABLE t1 ( a ENUM( 'x', 'y' ) );
insert into t1 values ('x');
CREATE TABLE t2 LIKE t1;
insert into t1 values ('y');
CREATE TABLE t3 LIKE t1;
INSERT INTO t3
SELECT * FROM ( SELECT * FROM t1 ) AS A
UNION SELECT * FROM t2;
select * from t3;
a
x
y
drop table t1,t2,t3;
set SESSION optimizer_switch= @save_optimizer_switch;
#
# LP BUG#944782: derived table from an information schema table
#
SET @save_optimizer_switch=@@optimizer_switch;
SET SESSION optimizer_switch='derived_merge=on';
SET SESSION optimizer_switch='derived_with_keys=on';
CREATE TABLE t1 (c1 int PRIMARY KEY, c2 char(5));
EXPLAIN
SELECT COUNT(*) > 0
FROM INFORMATION_SCHEMA.COLUMNS
INNER JOIN
(SELECT TABLE_SCHEMA,
GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX ASC) AS COL_NAMES
FROM INFORMATION_SCHEMA.STATISTICS
GROUP BY TABLE_SCHEMA) AS UNIQUES
ON ( COLUMNS.TABLE_SCHEMA = UNIQUES.TABLE_SCHEMA);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY COLUMNS ALL NULL NULL NULL NULL NULL Open_frm_only; Scanned all databases
1 PRIMARY <derived2> ref key0 key0 194 information_schema.COLUMNS.TABLE_SCHEMA 10
2 DERIVED STATISTICS ALL NULL NULL NULL NULL NULL Open_frm_only; Scanned all databases; Using filesort
SELECT COUNT(*) > 0
FROM INFORMATION_SCHEMA.COLUMNS
INNER JOIN
(SELECT TABLE_SCHEMA,
GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX ASC) AS COL_NAMES
FROM INFORMATION_SCHEMA.STATISTICS
GROUP BY TABLE_SCHEMA) AS UNIQUES
ON ( COLUMNS.TABLE_SCHEMA = UNIQUES.TABLE_SCHEMA);
COUNT(*) > 0
1
Warnings:
Warning 1286 Unknown storage engine 'InnoDB'
Warning 1286 Unknown storage engine 'InnoDB'
Warning 1286 Unknown storage engine 'InnoDB'
Warning 1286 Unknown storage engine 'InnoDB'
Warning 1286 Unknown storage engine 'InnoDB'
Warning 1286 Unknown storage engine 'InnoDB'
DROP TABLE t1;
SET SESSION optimizer_switch= @save_optimizer_switch;
#
# LP BUG#953649: crash when estimating the cost of a look-up
# into a derived table to be materialized
#
CREATE TABLE t1 (a int);
INSERT INTO t1 VALUES (132);
CREATE TABLE t2 (b int, c varchar(256));
INSERT INTO t2 VALUES (132,'test1'), (120,'text2'), (132,'text3');
CREATE VIEW v AS
SELECT b, GROUP_CONCAT(c) AS gc FROM t2 GROUP BY b;
SET @save_optimizer_switch=@@optimizer_switch;
SET SESSION optimizer_switch='derived_merge=off';
SET SESSION optimizer_switch='derived_with_keys=off';
EXPLAIN
SELECT * FROM t1, v WHERE a = b;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 system NULL NULL NULL NULL 1
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 3 Using where
2 DERIVED t2 ALL NULL NULL NULL NULL 3 Using filesort
SELECT * FROM t1, v WHERE a = b;
a b gc
132 132 test1,text3
SET SESSION optimizer_switch='derived_merge=on';
SET SESSION optimizer_switch='derived_with_keys=on';
EXPLAIN
SELECT * FROM t1, v WHERE a = b;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 system NULL NULL NULL NULL 1
1 PRIMARY <derived2> ref key0 key0 5 const 0
2 DERIVED t2 ALL NULL NULL NULL NULL 3 Using filesort
SELECT * FROM t1, v WHERE a = b;
a b gc
132 132 test1,text3
SET SESSION optimizer_switch= @save_optimizer_switch;
DROP VIEW v;
DROP TABLE t1,t2;
#
# LP BUG#968720 crash due to converting to materialized and
# natural join made only once
#
SET @save968720_optimizer_switch=@@optimizer_switch;
SET optimizer_switch = 'derived_merge=on';
CREATE TABLE t1 (a int, INDEX(a));
INSERT INTO t1 VALUES (1);
CREATE TABLE t2 (a int, INDEX(a));
INSERT INTO t2 VALUES (1), (2);
INSERT INTO t1 SELECT a FROM (SELECT a FROM test.t1) AS s1 NATURAL JOIN
t2 AS s2;
INSERT INTO t1 SELECT a FROM (SELECT a FROM test.t1) AS s1 NATURAL JOIN
t2 AS s2;
SELECT * FROM t1;
a
1
1
1
1
DELETE FROM t1;
INSERT INTO t1 VALUES (1);
PREPARE stmt FROM "
INSERT INTO t1 SELECT a FROM (SELECT a FROM test.t1) AS s1 NATURAL JOIN
t2 AS s2;
";
EXECUTE stmt;
SELECT * FROM t1;
a
1
1
EXECUTE stmt;
SELECT * FROM t1;
a
1
1
1
1
drop table t1,t2;
set optimizer_switch=@save968720_optimizer_switch;
#
# LP BUG#978847 Server crashes in Item_ref::real_item on
# INSERT .. SELECT with FROM subquery and derived_merge=ON
SET @save978847_optimizer_switch=@@optimizer_switch;
SET optimizer_switch = 'derived_merge=on';
CREATE TABLE t1 ( a INT, b INT );
INSERT INTO t1 VALUES (2,1),(3,2);
select * from t1;
a b
2 1
3 2
INSERT INTO t1 SELECT * FROM
( SELECT * FROM t1 ) AS alias;
select * from t1;
a b
2 1
3 2
2 1
3 2
prepare stmt1 from 'INSERT INTO t1 SELECT SQL_BIG_RESULT * FROM
( SELECT * FROM t1 ) AS alias';
execute stmt1;
select * from t1;
a b
2 1
3 2
2 1
3 2
2 1
3 2
2 1
3 2
execute stmt1;
select * from t1;
a b
2 1
3 2
2 1
3 2
2 1
3 2
2 1
3 2
2 1
3 2
2 1
3 2
2 1
3 2
2 1
3 2
drop table t1;
set optimizer_switch=@save978847_optimizer_switch;
#
# LP bug998516 Server hangs on INSERT .. SELECT with derived_merge,
# FROM subquery, UNION
#
CREATE TABLE t1 (a INT);
INSERT INTO t1 VALUES (1),(2);
CREATE TABLE t2 (b INT);
INSERT INTO t2 VALUES (3),(4);
INSERT INTO t1 SELECT * FROM ( SELECT * FROM t1 ) AS alias UNION SELECT * FROM t2;
select * from t1;
a
1
2
1
2
3
4
drop table t1,t2;
#
# MDEV-3873: Wrong result (extra rows) with NOT IN and
# a subquery from a MERGE view
#
CREATE TABLE t1 (a INT NOT NULL) ENGINE=MyISAM;
INSERT INTO t1 VALUES (4),(7),(0);
CREATE TABLE t2 (b INT NOT NULL) ENGINE=MyISAM;
INSERT INTO t2 VALUES (1),(2);
CREATE TABLE t3 (c INT NOT NULL) ENGINE=MyISAM;
INSERT INTO t3 VALUES (4),(6),(3);
CREATE TABLE t4 (d INT NOT NULL) ENGINE=MyISAM;
INSERT INTO t4 VALUES (4),(5),(3);
CREATE TABLE tv (e INT NOT NULL) ENGINE=MyISAM;
INSERT INTO tv VALUES (1),(3);
CREATE ALGORITHM=TEMPTABLE VIEW v_temptable AS SELECT * FROM tv;
CREATE ALGORITHM=MERGE VIEW v_merge AS SELECT * FROM tv;
SELECT * FROM t1, t2
WHERE a NOT IN ( SELECT e FROM t3 LEFT JOIN v_temptable ON (c = e) WHERE c <> b ) AND a < b;
a b
SELECT * FROM t1, t2
WHERE a NOT IN ( SELECT e FROM t3 LEFT JOIN v_merge ON (c = e) WHERE c <> b ) AND a < b;
a b
SELECT * FROM t1, t2
WHERE a NOT IN ( SELECT e FROM t3 LEFT JOIN (SELECT * FROM tv) as derived ON (c = e) WHERE c <> b ) AND a < b;
a b
drop view v_temptable, v_merge;
drop table t1,t2,t3,t4,tv;
#
# MDEV-3912: Wrong result (extra rows) with FROM subquery inside
# ALL subquery, LEFT JOIN, derived_merge.
# (duplicate of MDEV-3873 (above))
#
SET @save3912_optimizer_switch=@@optimizer_switch;
SET optimizer_switch = 'derived_merge=on,in_to_exists=on';
CREATE TABLE t1 (a INT) ENGINE=MyISAM;
INSERT INTO t1 VALUES (4),(8);
CREATE TABLE t2 (b INT) ENGINE=MyISAM;
INSERT INTO t2 VALUES (7),(0);
CREATE TABLE t3 (c INT, d INT NOT NULL) ENGINE=MyISAM;
INSERT INTO t3 VALUES (0,4),(8,6);
SELECT * FROM t1
WHERE a >= ALL (
SELECT d FROM t2 LEFT JOIN ( SELECT * FROM t3 ) AS alias ON ( c = b )
WHERE b >= a
);
a
8
set optimizer_switch=@save3912_optimizer_switch;
drop table t1, t2, t3;
#
# MDEV-4209: equi-join on BLOB column from materialized view
# or derived table
#
set @save_optimizer_switch=@@optimizer_switch;
set optimizer_switch='derived_with_keys=on';
CREATE TABLE t1 (c1 text, c2 int);
INSERT INTO t1 VALUES ('a',1), ('c',3), ('g',7), ('d',4), ('c',3);
CREATE TABLE t2 (c1 text, c2 int);
INSERT INTO t2 VALUES ('b',2), ('c',3);
CREATE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t1;
EXPLAIN EXTENDED
SELECT v1.c1, v1.c2 FROM v1, t2 WHERE v1.c1=t2.c1 AND v1.c2=t2.c2;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00 Using where
1 PRIMARY <derived2> ref key0 key0 5 test.t2.c2 1 100.00 Using where
2 DERIVED t1 ALL NULL NULL NULL NULL 5 100.00
Warnings:
Note 1003 /* select#1 */ select `v1`.`c1` AS `c1`,`v1`.`c2` AS `c2` from `test`.`v1` join `test`.`t2` where `v1`.`c1` = `test`.`t2`.`c1` and `v1`.`c2` = `test`.`t2`.`c2`
SELECT v1.c1, v1.c2 FROM v1, t2 WHERE v1.c1=t2.c1 AND v1.c2=t2.c2;
c1 c2
c 3
c 3
EXPLAIN EXTENDED
SELECT t2.c1, t2.c2 FROM (SELECT c1 g, MAX(c2) m FROM t1 GROUP BY c1) t, t2
WHERE t.g=t2.c1 AND t.m=t2.c2;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00 Using where
1 PRIMARY <derived2> ref key0 key0 5 test.t2.c2 1 100.00 Using where
2 DERIVED t1 ALL NULL NULL NULL NULL 5 100.00 Using temporary; Using filesort
Warnings:
Note 1003 /* select#1 */ select `test`.`t2`.`c1` AS `c1`,`test`.`t2`.`c2` AS `c2` from (/* select#2 */ select `test`.`t1`.`c1` AS `g`,max(`test`.`t1`.`c2`) AS `m` from `test`.`t1` group by `test`.`t1`.`c1`) `t` join `test`.`t2` where `t`.`g` = `test`.`t2`.`c1` and `t`.`m` = `test`.`t2`.`c2`
SELECT t2.c1, t2.c2 FROM (SELECT c1 g, MAX(c2) m FROM t1 GROUP BY c1) t, t2
WHERE t.g=t2.c1 AND t.m=t2.c2;
c1 c2
c 3
EXPLAIN EXTENDED
SELECT v1.c1, v1.c2, t2.c1, t2.c2 FROM v1, t2 WHERE v1.c1=t2.c1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 5 100.00 Using where; Using join buffer (flat, BNL join)
2 DERIVED t1 ALL NULL NULL NULL NULL 5 100.00
Warnings:
Note 1003 /* select#1 */ select `v1`.`c1` AS `c1`,`v1`.`c2` AS `c2`,`test`.`t2`.`c1` AS `c1`,`test`.`t2`.`c2` AS `c2` from `test`.`v1` join `test`.`t2` where `v1`.`c1` = `test`.`t2`.`c1`
SELECT v1.c1, v1.c2, t2.c1, t2.c2 FROM v1, t2 WHERE v1.c1=t2.c1;
c1 c2 c1 c2
c 3 c 3
c 3 c 3
DROP VIEW v1;
DROP TABLE t1,t2;
set optimizer_switch=@save_optimizer_switch;
#
# mdev-5078: sum over a view/derived table
#
CREATE TABLE t1 (a int);
INSERT INTO t1 (a) VALUES (1), (2);
CREATE TABLE t2 (b int(11));
INSERT INTO t2 (b) VALUES (1), (2);
CREATE VIEW v AS SELECT b as c FROM t2;
SELECT a, (SELECT SUM(a + c) FROM v) FROM t1;
a (SELECT SUM(a + c) FROM v)
1 5
2 7
SELECT a, (SELECT SUM(a + c) FROM (SELECT b as c FROM t2) AS v1) FROM t1;
a (SELECT SUM(a + c) FROM (SELECT b as c FROM t2) AS v1)
1 5
2 7
DROP VIEW v;
DROP TABLE t1,t2;
#
# mdev-5105: memory overwrite in multi-table update
# using natural join with a view
#
create table t1(a int,b tinyint,c tinyint)engine=myisam;
create table t2(a tinyint,b float,c int, d int, e int, f int, key (b), key(c), key(d), key(e), key(f))engine=myisam;
create table t3(a int,b int,c int, d int, e int, f int, key(a), key(b), key(c), key(d), key(e), key(f))engine=myisam;
create view v1 as select t2.b a, t1.b b, t2.c c, t2.d d, t2.e e, t2.f f from t1,t2 where t1.a=t2.a;
update t3 natural join v1 set a:=1;
drop view v1;
drop table t1,t2,t3;
#
# mdev-5288: assertion failure for query over a view with ORDER BY
#
CREATE TABLE t1 (a int, b int) ENGINE=MyISAM;
INSERT INTO t1 VALUES (4,1);
CREATE ALGORITHM=MERGE VIEW v1 AS SELECT * FROM t1;
EXPLAIN EXTENDED SELECT a FROM v1 WHERE a > 100 ORDER BY b;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
Warnings:
Note 1003 select 4 AS `a` from dual where 0 order by 1
DROP VIEW v1;
DROP TABLE t1;
CREATE TABLE IF NOT EXISTS `galleries` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`year` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `pictures` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`width` float DEFAULT NULL,
`height` float DEFAULT NULL,
`year` int(4) DEFAULT NULL,
`technique` varchar(50) DEFAULT NULL,
`comment` varchar(2000) DEFAULT NULL,
`gallery_id` int(11) NOT NULL,
`type` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `gallery_id` (`gallery_id`)
) DEFAULT CHARSET=utf8 ;
ALTER TABLE `pictures`
ADD CONSTRAINT `pictures_ibfk_1` FOREIGN KEY (`gallery_id`) REFERENCES `galleries` (`id`);
INSERT INTO `galleries` (`id`, `name`, `year`) VALUES
(1, 'Quand le noir et blanc invite le taupe', 2013),
(2, 'Une touche de couleur', 2012),
(3, 'Éclats', 2011),
(4, 'Gris béton', 2010),
(5, 'Expression du spalter', 2010),
(6, 'Zénitude', 2009),
(7, 'La force du rouge', 2008),
(8, 'Sphères', NULL),
(9, 'Centre', 2009),
(10, 'Nébuleuse', NULL);
INSERT INTO `pictures` (`id`, `name`, `width`, `height`, `year`, `technique`, `comment`, `gallery_id`, `type`) VALUES
(1, 'Éclaircie', 72.5, 100, NULL, NULL, NULL, 1, 1),
(2, 'Architecture', 81, 100, NULL, NULL, NULL, 1, 1),
(3, 'Nouveau souffle', 72.5, 100, NULL, NULL, NULL, 1, 1),
(4, 'Échanges (2)', 89, 116, NULL, NULL, NULL, 1, 1),
(5, 'Échanges', 89, 116, NULL, NULL, NULL, 1, 1),
(6, 'Fenêtre de vie', 81, 116, NULL, NULL, NULL, 1, 1),
(7, 'Architecture', 81, 100, NULL, NULL, NULL, 1, 1),
(8, 'Nouveau souffle (2)', 72.5, 100, NULL, NULL, NULL, 1, 1),
(9, 'Fluidité', 89, 116, NULL, NULL, NULL, 1, 1),
(10, 'Nouveau Monde', 89, 125, NULL, NULL, NULL, 1, 1),
(11, 'Mirage', 73, 100, NULL, NULL, NULL, 1, 1),
(12, 'Équilibre', 72.5, 116, NULL, NULL, NULL, 2, 1),
(13, 'Fusion', 72.5, 116, NULL, NULL, NULL, 2, 1),
(14, 'Étincelles', NULL, NULL, NULL, NULL, NULL, 3, 1),
(15, 'Régénérescence', NULL, NULL, NULL, NULL, NULL, 3, 1),
(16, 'Chaleur', 80, 80, NULL, NULL, NULL, 4, 1),
(17, 'Création', 90, 90, NULL, NULL, NULL, 4, 1),
(18, 'Horizon', 92, 73, NULL, NULL, NULL, 4, 1),
(19, 'Labyrinthe', 81, 100, NULL, NULL, NULL, 4, 1),
(20, 'Miroir', 80, 116, NULL, NULL, NULL, 5, 1),
(21, 'Libération', 81, 116, NULL, NULL, NULL, 5, 1),
(22, 'Éclats', 81, 116, NULL, NULL, NULL, 5, 1),
(23, 'Zénitude', 116, 89, NULL, NULL, NULL, 6, 1),
(24, 'Écritures lointaines', 90, 90, NULL, NULL, NULL, 7, 1),
(25, 'Émergence', 80, 80, NULL, NULL, NULL, 7, 1),
(26, 'Liberté', 50, 50, NULL, NULL, NULL, 7, 1),
(27, 'Silhouettes amérindiennes', 701, 70, NULL, NULL, NULL, 7, 1),
(28, 'Puissance', 81, 100, NULL, NULL, NULL, 8, 1),
(29, 'Source', 73, 116, NULL, NULL, NULL, 8, 1),
(30, 'Comme une ville qui prend vie', 50, 100, 2008, NULL, NULL, 9, 1),
(31, 'Suspension azur', 80, 80, NULL, NULL, NULL, 9, 1),
(32, 'Nébuleuse', 70, 70, NULL, NULL, NULL, 10, 1),
(33, 'Œuvre commandée 120 P', 114, 195, NULL, NULL, NULL, 1, 2),
(34, 'Œuvre commandée 120 P', 114, 195, NULL, NULL, NULL, 1, 2),
(35, 'Œuvre commandée 120 P', 114, 195, NULL, NULL, NULL, 1, 2),
(36, 'Œuvre commandée 120 P', 114, 195, NULL, NULL, NULL, 1, 2),
(37, 'Œuvre commandée 120 P', 114, 195, NULL, NULL, NULL, 1, 2),
(38, 'Œuvre commandée 120 P', 114, 195, NULL, NULL, NULL, 1, 2);
explain
SELECT g.id AS gallery_id,
g.name AS gallery_name,
p.id AS picture_id,
p.name AS picture_name,
g.p_random AS r1,
g.p_random AS r2,
g.p_random AS r3
FROM
(
SELECT gal.id,
gal.name,
(
SELECT pi.id
FROM pictures pi
WHERE pi.gallery_id = gal.id
ORDER BY RAND()
LIMIT 1
) AS p_random
FROM galleries gal
) g
LEFT JOIN pictures p
ON p.id = g.p_random
ORDER BY gallery_name ASC
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 10 Using filesort
1 PRIMARY p eq_ref PRIMARY PRIMARY 4 g.p_random 1 Using where
2 DERIVED gal ALL NULL NULL NULL NULL 10
3 DEPENDENT SUBQUERY pi ref gallery_id gallery_id 4 test.gal.id 4 Using temporary; Using filesort
drop table galleries, pictures;
#
# MDEV-5740: Assertion
#`!derived->first_select()->exclude_from_table_unique_test ||
#derived->outer_select()-> exclude_from_table_unique_test'
#failed on 2nd execution of PS with derived_merge
#
set @save_optimizer_switch5740=@@optimizer_switch;
SET optimizer_switch = 'derived_merge=on';
CREATE TABLE t1 (a INT);
INSERT INTO t1 VALUES (1),(2);
CREATE TABLE t2 (b INT);
INSERT INTO t2 VALUES (3),(4);
PREPARE stmt FROM '
INSERT INTO t1 SELECT * FROM t2 UNION SELECT * FROM (SELECT * FROM t1) AS sq
';
EXECUTE stmt;
select * from t1;
a
1
2
3
4
1
2
EXECUTE stmt;
select * from t1;
a
1
2
3
4
1
2
3
4
1
2
deallocate prepare stmt;
drop table t1,t2;
set optimizer_switch=@save_optimizer_switch5740;
#
# Bug mdev-5721: possible long key access to a materialized derived table
# (see also the test case for Bug#13261277 that is actually the same bug)
#
CREATE TABLE t1 (
id varchar(255) NOT NULL DEFAULT '',
familyid int(11) DEFAULT NULL,
withdrawndate date DEFAULT NULL,
KEY index_td_familyid_id (familyid,id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE t2 (
id int(11) NOT NULL AUTO_INCREMENT,
activefromts datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
shortdescription text,
useraccessfamily varchar(512) DEFAULT NULL,
serialized longtext,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
insert into t1 values ('picture/89/1369722032695.pmd',89,NULL);
insert into t1 values ('picture/90/1369832057370.pmd',90,NULL);
insert into t2 values (38,'2013-03-04 07:49:22','desc','CODE','string');
EXPLAIN
SELECT * FROM t2 x,
(SELECT t2.useraccessfamily, t2.serialized AS picturesubuser, COUNT(*)
FROM t2, t1 GROUP BY t2.useraccessfamily, picturesubuser) y
WHERE x.useraccessfamily = y.useraccessfamily;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY x system NULL NULL NULL NULL 1
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 2 Using where
2 DERIVED t2 system NULL NULL NULL NULL 1
2 DERIVED t1 index NULL index_td_familyid_id 772 NULL 2 Using index
SELECT * FROM t2 x,
(SELECT t2.useraccessfamily, t2.serialized AS picturesubuser, COUNT(*)
FROM t2, t1 GROUP BY t2.useraccessfamily, picturesubuser) y
WHERE x.useraccessfamily = y.useraccessfamily;
id activefromts shortdescription useraccessfamily serialized useraccessfamily picturesubuser COUNT(*)
38 2013-03-04 07:49:22 desc CODE string CODE string 2
DROP TABLE t1,t2;
#
# Bug#13261277: Unchecked key length caused missing records.
#
CREATE TABLE t1 (
col_varchar varchar(1024) CHARACTER SET utf8 DEFAULT NULL,
stub1 varchar(1024) CHARACTER SET utf8 DEFAULT NULL,
stub2 varchar(1024) CHARACTER SET utf8 DEFAULT NULL,
stub3 varchar(1024) CHARACTER SET utf8 DEFAULT NULL
);
INSERT INTO t1 VALUES
('d','d','l','ther'),
(NULL,'s','NJBIQ','trzetuchv'),
(-715390976,'coul','MYWFB','cfhtrzetu'),
(1696792576,'f','i\'s','c'),
(1,'i','ltpemcfhtr','gsltpemcf'),
(-663027712,'mgsltpemcf','sa','amgsltpem'),
(-1686700032,'JPRVK','i','vamgsltpe'),
(NULL,'STUNB','UNVJV','u'),
(5,'oka','qyihvamgsl','AXSMD'),
(NULL,'tqwmqyihva','h','yntqwmqyi'),
(3,'EGMJN','e','e');
CREATE TABLE t2 (
col_varchar varchar(10) DEFAULT NULL,
col_int INT DEFAULT NULL
);
INSERT INTO t2 VALUES ('d',9);
set optimizer_switch='derived_merge=off,derived_with_keys=on';
SET @save_heap_size= @@max_heap_table_size;
SET @@max_heap_table_size= 16384;
SELECT t2.col_int
FROM t2
RIGHT JOIN ( SELECT * FROM t1 ) AS dt
ON t2.col_varchar = dt.col_varchar
WHERE t2.col_int IS NOT NULL ;
col_int
9
# Shouldn't use auto_key0 for derived table
EXPLAIN
SELECT t2.col_int
FROM t2
RIGHT JOIN ( SELECT * FROM t1 ) AS dt
ON t2.col_varchar = dt.col_varchar
WHERE t2.col_int IS NOT NULL ;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 system NULL NULL NULL NULL 1
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 11 Using where
2 DERIVED t1 ALL NULL NULL NULL NULL 11
SET @@max_heap_table_size= @save_heap_size;
SET optimizer_switch=@save_optimizer_switch;
DROP TABLE t1,t2;
#
# end of 5.3 tests
#
#
# Bug mdev-11161: The second execution of prepared statement
# does not use generated key for materialized
# derived table / view
# (actually this is a 5.3 bug.)
#
create table t1 (
mat_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
matintnum CHAR(6) NOT NULL,
test MEDIUMINT UNSIGNED NULL
) CHARSET=latin1;
create table t2 (
mat_id MEDIUMINT UNSIGNED NOT NULL,
pla_id MEDIUMINT UNSIGNED NOT NULL
) CHARSET=latin1;
insert into t1 values
(NULL, 'a', 1), (NULL, 'b', 2), (NULL, 'c', 3), (NULL, 'd', 4),
(NULL, 'e', 5), (NULL, 'f', 6), (NULL, 'g', 7), (NULL, 'h', 8),
(NULL, 'i', 9);
insert into t2 values
(1, 100), (1, 101), (1, 102), (2, 100), (2, 103), (2, 104),
(3, 101), (3, 102), (3, 105);
explain
SELECT STRAIGHT_JOIN d.pla_id, m2.mat_id
FROM t1 m2
INNER JOIN
(SELECT mp.pla_id, MIN(m1.matintnum) AS matintnum
FROM t2 mp INNER JOIN t1 m1 ON mp.mat_id=m1.mat_id
GROUP BY mp.pla_id) d
ON d.matintnum=m2.matintnum;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY m2 ALL NULL NULL NULL NULL 9
1 PRIMARY <derived2> ref key0 key0 7 test.m2.matintnum 1
2 DERIVED mp ALL NULL NULL NULL NULL 9 Using temporary; Using filesort
2 DERIVED m1 eq_ref PRIMARY PRIMARY 3 test.mp.mat_id 1
prepare stmt1 from
"SELECT STRAIGHT_JOIN d.pla_id, m2.mat_id
FROM t1 m2
INNER JOIN
(SELECT mp.pla_id, MIN(m1.matintnum) AS matintnum
FROM t2 mp INNER JOIN t1 m1 ON mp.mat_id=m1.mat_id
GROUP BY mp.pla_id) d
ON d.matintnum=m2.matintnum";
flush status;
execute stmt1;
pla_id mat_id
102 1
101 1
100 1
104 2
103 2
105 3
show status like '%Handler_read%';
Variable_name Value
Handler_read_first 0
Handler_read_key 21
Handler_read_last 0
Handler_read_next 6
Handler_read_prev 0
Handler_read_retry 0
Handler_read_rnd 6
Handler_read_rnd_deleted 0
Handler_read_rnd_next 27
flush status;
execute stmt1;
pla_id mat_id
102 1
101 1
100 1
104 2
103 2
105 3
show status like '%Handler_read%';
Variable_name Value
Handler_read_first 0
Handler_read_key 21
Handler_read_last 0
Handler_read_next 6
Handler_read_prev 0
Handler_read_retry 0
Handler_read_rnd 6
Handler_read_rnd_deleted 0
Handler_read_rnd_next 27
deallocate prepare stmt1;
drop table t1,t2;
#
# Bug mdev-12670: mergeable derived / view with subqueries
# subject to semi-join optimizations
# (actually this is a 5.3 bug.)
#
create table t1 (a int) engine=myisam;
insert into t1 values (5),(3),(2),(7),(2),(5),(1);
create table t2 (b int, index idx(b)) engine=myisam;
insert into t2 values (2),(3),(2),(1),(3),(4);
insert into t2 select b+10 from t2;
insert into t2 select b+10 from t2;
insert into t2 select b+10 from t2;
insert into t2 select b+10 from t2;
insert into t2 select b+10 from t2;
insert into t2 select b+10 from t2;
insert into t2 select b+10 from t2;
insert into t2 select b+10 from t2;
insert into t2 select b+10 from t2;
insert into t2 select b+10 from t2;
analyze table t1,t2;
Table Op Msg_type Msg_text
test.t1 analyze status Engine-independent statistics collected
test.t1 analyze status OK
test.t2 analyze status Engine-independent statistics collected
test.t2 analyze status OK
explain select a from t1 where a in (select b from t2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 7 Using where
1 PRIMARY t2 ref idx idx 5 test.t1.a 139 Using index; FirstMatch(t1)
explain select * from (select a from t1 where a in (select b from t2)) t;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 7 Using where
1 PRIMARY t2 ref idx idx 5 test.t1.a 139 Using index; FirstMatch(t1)
create view v1 as select a from t1 where a in (select b from t2);
explain select * from v1;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 7 Using where
1 PRIMARY t2 ref idx idx 5 test.t1.a 139 Using index; FirstMatch(t1)
drop view v1;
drop table t1,t2;
#
# Bug mdev-12812: mergeable derived / view with subqueries
# NOT subject to semi-join optimizations
#
CREATE TABLE t1 (c1 varchar(3)) ENGINE=MyISAM;
INSERT INTO t1 VALUES ('foo'),('foo');
CREATE TABLE t2 (c2 varchar(3)) ENGINE=MyISAM;
INSERT INTO t2 VALUES ('bar'),('qux'),('foo');
SELECT STRAIGHT_JOIN *
FROM ( SELECT * FROM t1 WHERE c1 IN ( SELECT c2 FROM t2 ) ) AS sq;
c1
foo
foo
EXPLAIN EXTENDED SELECT STRAIGHT_JOIN *
FROM ( SELECT * FROM t1 WHERE c1 IN ( SELECT c2 FROM t2 ) ) AS sq;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 Using where
3 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 3 100.00 Using where
Warnings:
Note 1003 /* select#1 */ select straight_join `test`.`t1`.`c1` AS `c1` from `test`.`t1` where <in_optimizer>(`test`.`t1`.`c1`,<exists>(/* select#3 */ select `test`.`t2`.`c2` from `test`.`t2` where <cache>(`test`.`t1`.`c1`) = `test`.`t2`.`c2`))
DROP TABLE t1, t2;
#
# Bug mdev-16420: materialized view that renames columns
# in inner part of outer join
#
CREATE TABLE t1 (id int, PRIMARY KEY (id));
INSERT INTO t1 VALUES (2), (3), (7), (1);
CREATE VIEW v1 AS SELECT * FROM t1;
CREATE VIEW v2 AS SELECT v1.id AS order_pk FROM v1 GROUP BY v1.id;
CREATE VIEW v3 AS
SELECT t.id AS order_pk FROM (SELECT * FROM t1) AS t GROUP BY t.id;
SELECT * FROM t1 LEFT JOIN v2 ON t1.id=v2.order_pk;
id order_pk
1 1
2 2
3 3
7 7
EXPLAIN EXTENDED
SELECT * FROM t1 LEFT JOIN v2 ON t1.id=v2.order_pk;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 index NULL PRIMARY 4 NULL 4 100.00 Using index
1 PRIMARY <derived2> ref key0 key0 5 test.t1.id 1 100.00
2 LATERAL DERIVED t1 eq_ref PRIMARY PRIMARY 4 test.t1.id 1 100.00 Using where; Using index
Warnings:
Note 1003 /* select#1 */ select `test`.`t1`.`id` AS `id`,`v2`.`order_pk` AS `order_pk` from `test`.`t1` left join `test`.`v2` on(`v2`.`order_pk` = `test`.`t1`.`id`) where 1
SELECT * FROM t1 LEFT JOIN v3 ON t1.id=v3.order_pk;
id order_pk
1 1
2 2
3 3
7 7
EXPLAIN EXTENDED
SELECT * FROM t1 LEFT JOIN v3 ON t1.id=v3.order_pk;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 index NULL PRIMARY 4 NULL 4 100.00 Using index
1 PRIMARY <derived2> ref key0 key0 5 test.t1.id 1 100.00
2 LATERAL DERIVED t1 eq_ref PRIMARY PRIMARY 4 test.t1.id 1 100.00 Using where; Using index
Warnings:
Note 1003 /* select#1 */ select `test`.`t1`.`id` AS `id`,`v3`.`order_pk` AS `order_pk` from `test`.`t1` left join `test`.`v3` on(`v3`.`order_pk` = `test`.`t1`.`id`) where 1
DROP VIEW v1,v2,v3;
DROP TABLE t1;
set optimizer_switch=@exit_optimizer_switch;
set join_cache_level=@exit_join_cache_level;
#
# Bug mdev-18479: EXPLAIN for query with many expensive derived
#
CREATE TABLE t1
(id int auto_increment primary key,
uid int NOT NULL,
gp_id int NOT NULL,
r int NOT NULL
);
INSERT INTO t1(uid,gp_id,r) VALUES
(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),
(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),
(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),
(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),
(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),(1,1,1),
(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),
(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),
(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),
(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),
(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),(1,2,1),
(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),
(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),
(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),
(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),
(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),(1,3,1),
(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),
(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),
(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),
(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),
(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),(1,4,1),
(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),
(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),
(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),
(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),
(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),(1,5,1),
(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),
(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),
(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),
(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),
(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),(1,6,1),
(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),
(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),
(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),
(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),
(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),(1,7,1),
(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),
(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),
(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),
(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),
(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),(1,8,1),
(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),
(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),
(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),
(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),
(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),(1,9,1),
(1,10,1),(1,10,1),(1,10,1),(1,10,1),(1,10,1),(1,10,1),(1,10,1),(1,10,1),(1,10,1),
(1,10,1),(1,10,1),(1,10,1),(1,10,1),(1,10,1),(1,10,1),(1,10,1),(1,10,1),(1,10,1),
(1,10,1),(1,10,1),(1,10,1),(1,10,1),(1,10,1),(1,10,1),(1,10,1),(1,10,1),(1,10,1),
(1,10,1),(1,10,1),(1,10,1),(1,10,1),(1,10,1),(1,10,1),(1,10,1),(1,10,1),(1,10,1),
(1,10,1),(1,10,1),(1,10,1),(1,10,1),(1,10,1),(1,10,1),(1,10,1),(1,10,1),(1,10,1),
(1,10,1),(1,10,1),(1,10,1),(1,10,1),(1,10,1),(1,11,1),(1,11,1),(1,11,1),(1,11,1),
(1,11,1),(1,11,1),(1,11,1),(1,11,1),(1,11,1),(1,11,1),(1,11,1),(1,11,1),(1,11,1),
(1,11,1),(1,11,1),(1,11,1),(1,11,1),(1,11,1),(1,11,1),(1,11,1),(1,11,1),(1,11,1),
(1,11,1),(1,11,1),(1,11,1),(1,11,1),(1,11,1),(1,11,1),(1,11,1),(1,11,1),(1,11,1),
(1,11,1),(1,11,1),(1,11,1),(1,11,1),(1,11,1),(1,11,1),(1,11,1),(1,11,1),(1,11,1),
(1,11,1),(1,11,1),(1,11,1),(1,11,1),(1,11,1),(1,11,1),(1,11,1),(1,11,1),(1,11,1),
(1,11,1);
CREATE TABLE t2 (id int) ;
INSERT INTO t2 VALUES (1);
explain SELECT 1 FROM t2 JOIN
(SELECT t2.id
FROM t2
JOIN t1 p4 ON p4.r=4 AND p4.uid=t2.id
JOIN t1 p5 ON p5.r=5 AND p5.uid=t2.id
JOIN t1 p6 ON p6.r=6 AND p6.uid=t2.id
JOIN t1 p7 ON p7.r=7 AND p7.uid=t2.id
JOIN t1 p8 ON p8.r=8 AND p8.uid=t2.id
JOIN t1 p9 ON p9.r=9 AND p9.uid=t2.id
JOIN t1 p10 ON p10.r=10 AND p10.uid=t2.id
) gp_1 ON gp_1.id=t2.id
JOIN
(SELECT t2.id
FROM t2
JOIN t1 p4 ON p4.r=4 AND p4.uid=t2.id
JOIN t1 p5 ON p5.r=5 AND p5.uid=t2.id
JOIN t1 p6 ON p6.r=6 AND p6.uid=t2.id
JOIN t1 p7 ON p7.r=7 AND p7.uid=t2.id
JOIN t1 p8 ON p8.r=8 AND p8.uid=t2.id
JOIN t1 p9 ON p9.r=9 AND p9.uid=t2.id
JOIN t1 p10 ON p10.r=10 AND p10.uid=t2.id
) gp_2 ON gp_2.id=t2.id
JOIN
(SELECT t2.id
FROM t2
JOIN t1 p1 ON p1.r=1 AND p1.uid=t2.id
JOIN t1 p3 ON p3.r=3 AND p3.uid=t2.id
JOIN t1 p4 ON p4.r=4 AND p4.uid=t2.id
JOIN t1 p5 ON p5.r=5 AND p5.uid=t2.id
JOIN t1 p6 ON p6.r=6 AND p6.uid=t2.id
JOIN t1 p7 ON p7.r=7 AND p7.uid=t2.id
JOIN t1 p8 ON p8.r=8 AND p8.uid=t2.id
JOIN t1 p9 ON p9.r=9 AND p9.uid=t2.id
JOIN t1 p10 ON p10.r=10 AND p10.uid=t2.id
) gp_3 ON gp_3.id=t2.id
JOIN
(SELECT t2.id
FROM t2
JOIN t1 p1 ON p1.r=1 AND p1.uid=t2.id
JOIN t1 p3 ON p3.r=3 AND p3.uid=t2.id
JOIN t1 p4 ON p4.r=4 AND p4.uid=t2.id
JOIN t1 p5 ON p5.r=5 AND p5.uid=t2.id
JOIN t1 p6 ON p6.r=6 AND p6.uid=t2.id
JOIN t1 p7 ON p7.r=7 AND p7.uid=t2.id
JOIN t1 p8 ON p8.r=8 AND p8.uid=t2.id
JOIN t1 p9 ON p9.r=9 AND p9.uid=t2.id
JOIN t1 p10 ON p10.r=10 AND p10.uid=t2.id
) gp_4 ON gp_4.id=t2.id
JOIN
(SELECT t2.id
FROM t2
JOIN t1 p1 ON p1.r=1 AND p1.uid=t2.id
JOIN t1 p3 ON p3.r=3 AND p3.uid=t2.id
JOIN t1 p4 ON p4.r=4 AND p4.uid=t2.id
JOIN t1 p5 ON p5.r=5 AND p5.uid=t2.id
JOIN t1 p6 ON p6.r=6 AND p6.uid=t2.id
JOIN t1 p7 ON p7.r=7 AND p7.uid=t2.id
JOIN t1 p8 ON p8.r=8 AND p8.uid=t2.id
JOIN t1 p9 ON p9.r=9 AND p9.uid=t2.id
JOIN t1 p10 ON p10.r=10 AND p10.uid=t2.id
) gp_5 ON gp_5.id=t2.id
JOIN
(SELECT t2.id
FROM t2
JOIN t1 p1 ON p1.r=1 AND p1.uid=t2.id
JOIN t1 p3 ON p3.r=3 AND p3.uid=t2.id
JOIN t1 p4 ON p4.r=4 AND p4.uid=t2.id
JOIN t1 p5 ON p5.r=5 AND p5.uid=t2.id
JOIN t1 p6 ON p6.r=6 AND p6.uid=t2.id
JOIN t1 p7 ON p7.r=7 AND p7.uid=t2.id
JOIN t1 p8 ON p8.r=8 AND p8.uid=t2.id
JOIN t1 p9 ON p9.r=9 AND p9.uid=t2.id
JOIN t1 p10 ON p10.r=10 AND p10.uid=t2.id
) gp_6 ON gp_6.id=t2.id
JOIN
(SELECT t2.id
FROM t2
JOIN t1 p1 ON p1.r=1 AND p1.uid=t2.id
JOIN t1 p3 ON p3.r=3 AND p3.uid=t2.id
JOIN t1 p4 ON p4.r=4 AND p4.uid=t2.id
JOIN t1 p5 ON p5.r=5 AND p5.uid=t2.id
JOIN t1 p6 ON p6.r=6 AND p6.uid=t2.id
JOIN t1 p7 ON p7.r=7 AND p7.uid=t2.id
JOIN t1 p8 ON p8.r=8 AND p8.uid=t2.id
JOIN t1 p9 ON p9.r=9 AND p9.uid=t2.id
JOIN t1 p10 ON p10.r=10 AND p10.uid=t2.id
WHERE p1.gp_id=7) gp_7 ON gp_7.id=t2.id
JOIN
(SELECT t2.id
FROM t2
JOIN t1 p4 ON p4.r=4 AND p4.uid=t2.id
JOIN t1 p5 ON p5.r=5 AND p5.uid=t2.id
JOIN t1 p6 ON p6.r=6 AND p6.uid=t2.id
JOIN t1 p7 ON p7.r=7 AND p7.uid=t2.id
JOIN t1 p8 ON p8.r=8 AND p8.uid=t2.id
JOIN t1 p9 ON p9.r=9 AND p9.uid=t2.id
JOIN t1 p10 ON p10.r=10 AND p10.uid=t2.id
) gp_8 ON gp_8.id=t2.id
JOIN
(SELECT t2.id
FROM t2
JOIN t1 p4 ON p4.r=4 AND p4.uid=t2.id
JOIN t1 p5 ON p5.r=5 AND p5.uid=t2.id
JOIN t1 p6 ON p6.r=6 AND p6.uid=t2.id
JOIN t1 p7 ON p7.r=7 AND p7.uid=t2.id
JOIN t1 p8 ON p8.r=8 AND p8.uid=t2.id
JOIN t1 p9 ON p9.r=9 AND p9.uid=t2.id
JOIN t1 p10 ON p10.r=10 AND p10.uid=t2.id
) gp_9 ON gp_9.id=t2.id
JOIN
(SELECT t2.id
FROM t2
JOIN t1 p4 ON p4.r=4 AND p4.uid=t2.id
JOIN t1 p5 ON p5.r=5 AND p5.uid=t2.id
JOIN t1 p6 ON p6.r=6 AND p6.uid=t2.id
JOIN t1 p7 ON p7.r=7 AND p7.uid=t2.id
JOIN t1 p8 ON p8.r=8 AND p8.uid=t2.id
JOIN t1 p9 ON p9.r=9 AND p9.uid=t2.id
JOIN t1 p10 ON p10.r=10 AND p10.uid=t2.id
) gp_14 ON gp_14.id=t2.id
JOIN
(SELECT t2.id
FROM t2
JOIN t1 p4 ON p4.r=4 AND p4.uid=t2.id
JOIN t1 p5 ON p5.r=5 AND p5.uid=t2.id
JOIN t1 p6 ON p6.r=6 AND p6.uid=t2.id
JOIN t1 p7 ON p7.r=7 AND p7.uid=t2.id
JOIN t1 p8 ON p8.r=8 AND p8.uid=t2.id
JOIN t1 p9 ON p9.r=9 AND p9.uid=t2.id
JOIN t1 p10 ON p10.r=10 AND p10.uid=t2.id
) gp_15 ON gp_15.id=t2.id
JOIN
(SELECT t2.id
FROM t2
JOIN t1 p4 ON p4.r=4 AND p4.uid=t2.id
JOIN t1 p5 ON p5.r=5 AND p5.uid=t2.id
JOIN t1 p6 ON p6.r=6 AND p6.uid=t2.id
JOIN t1 p7 ON p7.r=7 AND p7.uid=t2.id
JOIN t1 p8 ON p8.r=8 AND p8.uid=t2.id
JOIN t1 p9 ON p9.r=9 AND p9.uid=t2.id
JOIN t1 p10 ON p10.r=10 AND p10.uid=t2.id
) gp_16 ON gp_16.id=t2.id
JOIN
(SELECT t2.id
FROM t2
JOIN t1 p4 ON p4.r=4 AND p4.uid=t2.id
JOIN t1 p5 ON p5.r=5 AND p5.uid=t2.id
JOIN t1 p7 ON p7.r=7 AND p7.uid=t2.id
JOIN t1 p8 ON p8.r=8 AND p8.uid=t2.id
JOIN t1 p9 ON p9.r=9 AND p9.uid=t2.id
JOIN t1 p10 ON p10.r=10 AND p10.uid=t2.id
)gp_17 ON gp_17.id=t2.id
JOIN
(SELECT t2.id
FROM t2
JOIN t1 p4 ON p4.r=4 AND p4.uid=t2.id
JOIN t1 p5 ON p5.r=5 AND p5.uid=t2.id
JOIN t1 p6 ON p6.r=6 AND p6.uid=t2.id
JOIN t1 p7 ON p7.r=7 AND p7.uid=t2.id
JOIN t1 p8 ON p8.r=8 AND p8.uid=t2.id
JOIN t1 p9 ON p9.r=9 AND p9.uid=t2.id
JOIN t1 p10 ON p10.r=10 AND p10.uid=t2.id
)gp_18 ON gp_18.id=t2.id
JOIN
(SELECT t2.id
FROM t2
JOIN t1 p4 ON p4.r=4 AND p4.uid=t2.id
JOIN t1 p5 ON p5.r=5 AND p5.uid=t2.id
JOIN t1 p6 ON p6.r=6 AND p6.uid=t2.id
JOIN t1 p7 ON p7.r=7 AND p7.uid=t2.id
JOIN t1 p8 ON p8.r=8 AND p8.uid=t2.id
JOIN t1 p9 ON p9.r=9 AND p9.uid=t2.id
JOIN t1 p10 ON p10.r=10 AND p10.uid=t2.id
)gp_19 ON gp_19.id=t2.id
JOIN
(SELECT t2.id
FROM t2
JOIN t1 p4 ON p4.r=4 AND p4.uid=t2.id
JOIN t1 p5 ON p5.r=5 AND p5.uid=t2.id
JOIN t1 p6 ON p6.r=6 AND p6.uid=t2.id
JOIN t1 p7 ON p7.r=7 AND p7.uid=t2.id
JOIN t1 p8 ON p8.r=8 AND p8.uid=t2.id
) gp_20 ON gp_20.id=t2.id ;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 system NULL NULL NULL NULL 1
1 PRIMARY t2 system NULL NULL NULL NULL 1
1 PRIMARY t2 system NULL NULL NULL NULL 1
1 PRIMARY t2 system NULL NULL NULL NULL 1
1 PRIMARY t2 system NULL NULL NULL NULL 1
1 PRIMARY t2 system NULL NULL NULL NULL 1
1 PRIMARY p4 ALL NULL NULL NULL NULL 550 Using where
1 PRIMARY p5 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (flat, BNL join)
1 PRIMARY p6 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p7 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p8 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p9 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p10 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p4 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p5 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p6 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p7 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p8 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p9 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p10 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p1 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p3 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p4 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p5 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p6 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p7 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p8 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p9 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p10 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p1 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p3 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p4 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p5 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p6 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p7 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p8 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p9 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p10 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p1 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p3 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p4 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p5 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p6 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p7 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p8 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p9 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY p10 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY <derived17> ALL NULL NULL NULL NULL 50328437500000 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY <derived14> ALL NULL NULL NULL NULL 27680640625000000 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY <derived9> ALL NULL NULL NULL NULL 15224352343750000640 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY <derived10> ALL NULL NULL NULL NULL 15224352343750000640 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY <derived11> ALL NULL NULL NULL NULL 15224352343750000640 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY <derived12> ALL NULL NULL NULL NULL 15224352343750000640 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY <derived13> ALL NULL NULL NULL NULL 15224352343750000640 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY <derived15> ALL NULL NULL NULL NULL 15224352343750000640 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY <derived16> ALL NULL NULL NULL NULL 15224352343750000640 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY <derived7> ALL NULL NULL NULL NULL 18446744073709551615 Using where; Using join buffer (incremental, BNL join)
1 PRIMARY <derived8> ALL NULL NULL NULL NULL 18446744073709551615 Using where; Using join buffer (incremental, BNL join)
17 DERIVED t2 system NULL NULL NULL NULL 1
17 DERIVED p4 ALL NULL NULL NULL NULL 550 Using where
17 DERIVED p5 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (flat, BNL join)
17 DERIVED p6 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
17 DERIVED p7 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
17 DERIVED p8 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
16 DERIVED t2 system NULL NULL NULL NULL 1
16 DERIVED p4 ALL NULL NULL NULL NULL 550 Using where
16 DERIVED p5 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (flat, BNL join)
16 DERIVED p6 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
16 DERIVED p7 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
16 DERIVED p8 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
16 DERIVED p9 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
16 DERIVED p10 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
15 DERIVED t2 system NULL NULL NULL NULL 1
15 DERIVED p4 ALL NULL NULL NULL NULL 550 Using where
15 DERIVED p5 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (flat, BNL join)
15 DERIVED p6 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
15 DERIVED p7 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
15 DERIVED p8 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
15 DERIVED p9 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
15 DERIVED p10 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
14 DERIVED t2 system NULL NULL NULL NULL 1
14 DERIVED p4 ALL NULL NULL NULL NULL 550 Using where
14 DERIVED p5 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (flat, BNL join)
14 DERIVED p7 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
14 DERIVED p8 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
14 DERIVED p9 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
14 DERIVED p10 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
13 DERIVED t2 system NULL NULL NULL NULL 1
13 DERIVED p4 ALL NULL NULL NULL NULL 550 Using where
13 DERIVED p5 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (flat, BNL join)
13 DERIVED p6 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
13 DERIVED p7 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
13 DERIVED p8 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
13 DERIVED p9 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
13 DERIVED p10 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
12 DERIVED t2 system NULL NULL NULL NULL 1
12 DERIVED p4 ALL NULL NULL NULL NULL 550 Using where
12 DERIVED p5 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (flat, BNL join)
12 DERIVED p6 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
12 DERIVED p7 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
12 DERIVED p8 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
12 DERIVED p9 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
12 DERIVED p10 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
11 DERIVED t2 system NULL NULL NULL NULL 1
11 DERIVED p4 ALL NULL NULL NULL NULL 550 Using where
11 DERIVED p5 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (flat, BNL join)
11 DERIVED p6 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
11 DERIVED p7 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
11 DERIVED p8 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
11 DERIVED p9 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
11 DERIVED p10 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
10 DERIVED t2 system NULL NULL NULL NULL 1
10 DERIVED p4 ALL NULL NULL NULL NULL 550 Using where
10 DERIVED p5 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (flat, BNL join)
10 DERIVED p6 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
10 DERIVED p7 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
10 DERIVED p8 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
10 DERIVED p9 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
10 DERIVED p10 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
9 DERIVED t2 system NULL NULL NULL NULL 1
9 DERIVED p4 ALL NULL NULL NULL NULL 550 Using where
9 DERIVED p5 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (flat, BNL join)
9 DERIVED p6 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
9 DERIVED p7 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
9 DERIVED p8 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
9 DERIVED p9 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
9 DERIVED p10 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
8 DERIVED t2 system NULL NULL NULL NULL 1
8 DERIVED p1 ALL NULL NULL NULL NULL 550 Using where
8 DERIVED p3 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (flat, BNL join)
8 DERIVED p4 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
8 DERIVED p5 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
8 DERIVED p6 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
8 DERIVED p7 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
8 DERIVED p8 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
8 DERIVED p9 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
8 DERIVED p10 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
7 DERIVED t2 system NULL NULL NULL NULL 1
7 DERIVED p1 ALL NULL NULL NULL NULL 550 Using where
7 DERIVED p3 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (flat, BNL join)
7 DERIVED p4 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
7 DERIVED p5 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
7 DERIVED p6 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
7 DERIVED p7 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
7 DERIVED p8 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
7 DERIVED p9 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
7 DERIVED p10 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
DROP TABLE t1, t2;
#
# MDEV-19778: equality condition for mergeable view returning constants
# in its columns and used as inner table of outer join
#
create table t1 (pk int, a int);
insert into t1 values (1,7), (2,3), (3,2), (4,3);
create table t2 (b int);
insert into t2 values (5), (1), (NULL), (3);
create table t3 (c int);
insert into t3 values (1), (8);
create view v1 as
select 3 as d, t2.b from t2;
select * from t1 left join v1 on t1.pk <= 2 where t1.a=v1.d;
pk a d b
2 3 3 5
2 3 3 1
2 3 3 NULL
2 3 3 3
explain extended select * from t1 left join v1 on t1.pk <= 2 where t1.a=v1.d;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 4 100.00 Using where
1 SIMPLE t2 ALL NULL NULL NULL NULL 4 100.00 Using join buffer (flat, BNL join)
Warnings:
Note 1003 select `test`.`t1`.`pk` AS `pk`,`test`.`t1`.`a` AS `a`,3 AS `d`,`test`.`t2`.`b` AS `b` from `test`.`t1` join `test`.`t2` where `test`.`t1`.`a` = 3 and `test`.`t1`.`pk` <= 2
select * from t1 left join (select 3 as d, t2.b from t2) dt on t1.pk <= 2
where t1.a=dt.d;
pk a d b
2 3 3 5
2 3 3 1
2 3 3 NULL
2 3 3 3
explain extended select * from t1 left join (select 3 as d, t2.b from t2) dt on t1.pk <= 2
where t1.a=dt.d;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 4 100.00 Using where
1 SIMPLE t2 ALL NULL NULL NULL NULL 4 100.00 Using join buffer (flat, BNL join)
Warnings:
Note 1003 select `test`.`t1`.`pk` AS `pk`,`test`.`t1`.`a` AS `a`,3 AS `d`,`test`.`t2`.`b` AS `b` from `test`.`t1` join `test`.`t2` where `test`.`t1`.`a` = 3 and `test`.`t1`.`pk` <= 2
select * from t1 left join (v1,t3) on t1.pk <= 2 where t1.a=v1.d;
pk a d b c
2 3 3 5 1
2 3 3 5 8
2 3 3 1 1
2 3 3 1 8
2 3 3 NULL 1
2 3 3 NULL 8
2 3 3 3 1
2 3 3 3 8
explain extended select * from t1 left join (v1,t3) on t1.pk <= 2 where t1.a=v1.d;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t3 ALL NULL NULL NULL NULL 2 100.00
1 SIMPLE t1 ALL NULL NULL NULL NULL 4 100.00 Using where; Using join buffer (flat, BNL join)
1 SIMPLE t2 ALL NULL NULL NULL NULL 4 100.00 Using join buffer (incremental, BNL join)
Warnings:
Note 1003 select `test`.`t1`.`pk` AS `pk`,`test`.`t1`.`a` AS `a`,3 AS `d`,`test`.`t2`.`b` AS `b`,`test`.`t3`.`c` AS `c` from `test`.`t1` join `test`.`t2` join `test`.`t3` where `test`.`t1`.`a` = 3 and `test`.`t1`.`pk` <= 2
drop view v1;
drop table t1,t2,t3;
#
# MDEV-25679: view / derived table defined as ordered select with LIMIT
#
create table t1 (a int);
insert into t1 values (3), (7), (1);
create view v1 as (select a from t1 limit 2) order by a desc;
(select a from t1 limit 2) order by a desc;
a
7
3
select * from v1;
a
7
3
select * from ((select a from t1 limit 2) order by a desc) dt;
a
3
7
drop view v1;
drop table t1;
#
# MDEV-24454 Second execution of SELECT containing set function
# MDEV-25086: whose only argument is an outer reference to a column
# of mergeable view/derived/table/CTE
#
create table t1 (a int);
create table t2 (b int);
insert into t1 values (3), (1), (3);
insert into t2 values (70), (30), (70);
create view v1 as select * from t2;
prepare stmt from "
select (select sum(b) from t1 where a=1) as r from v1;
";
execute stmt;
r
170
execute stmt;
r
170
deallocate prepare stmt;
prepare stmt from "
select (select sum(b) from t1 where a=1) as r from (select * from t2) dt;
";
execute stmt;
r
170
execute stmt;
r
170
deallocate prepare stmt;
prepare stmt from "
with cte as (select * from t2)
select (select sum(b) from t1 where a=1) as r from cte;
";
execute stmt;
r
170
execute stmt;
r
170
deallocate prepare stmt;
prepare stmt from "
select (select sum(b) from t1 where a=1) as r
from (select * from v1 where b > 50) dt;
";
execute stmt;
r
140
execute stmt;
r
140
deallocate prepare stmt;
prepare stmt from "
select (select sum(b) from t1 where a=1) as r
from (select * from (select * from t2) dt1 where b > 50) dt;
";
execute stmt;
r
140
execute stmt;
r
140
deallocate prepare stmt;
prepare stmt from "
with cte as (select * from (select * from t2) dt1 where b > 50)
select (select sum(b) from t1 where a=1) as r from cte;
";
execute stmt;
r
140
execute stmt;
r
140
deallocate prepare stmt;
create procedure sp1()
begin
select (select sum(b) from t1 where a=1) as r from v1;
end |
call sp1();
r
170
call sp1();
r
170
drop procedure sp1;
create procedure sp1()
begin
select (select sum(b) from t1 where a=1) as r from (select * from t2) dt;
end |
call sp1();
r
170
call sp1();
r
170
drop procedure sp1;
create procedure sp1()
begin
with cte as (select * from t2)
select (select sum(b) from t1 where a=1) as r from cte;
end |
call sp1();
r
170
call sp1();
r
170
drop procedure sp1;
drop view v1;
drop table t1,t2;
CREATE TABLE t1(f0 INT);
INSERT INTO t1 VALUES (3);
CREATE VIEW v1 AS SELECT f0 AS f1 FROM t1;
CREATE VIEW v2 AS
SELECT
(SELECT GROUP_CONCAT(v1.f1 SEPARATOR ', ') FROM v1 n) AS f2,
GROUP_CONCAT('aa' SEPARATOR ', ') AS f3
FROM v1;
CREATE VIEW v3 AS SELECT * FROM v2;
CREATE PROCEDURE p1()
SELECT * FROM v3;
CALL p1();
f2 f3
3 aa
CALL p1();
f2 f3
3 aa
DROP PROCEDURE p1;
DROP VIEW v1,v2,v3;
DROP TABLE t1;
#
# MDEV-27212: 2-nd execution of PS for select with embedded derived tables
# and correlated subquery in select list of outer derived
#
create table t1 ( id int, id2 int ) engine=myisam;
create table t2 ( x3 int , x1 int , x2 int, a1 int) engine=myisam;
insert into t1 values (3, 2), (4, 2), (3, 4);
insert into t2 values (1, 2, 2, 1), (1, 3, 3, 2), (2, 3, 3, 1);
prepare stmt from "select id from t1
join
( select dt2.x1,
( select sum(a1) from t2 where t2.x1 = dt2.x1 ) m
from ( select x1 from t2 u where x3 = 1 ) dt2
) dt
on t1.id = dt.x1
where t1.id2 < dt.m";
execute stmt;
id
3
execute stmt;
id
3
deallocate prepare stmt;
create procedure sp1() select id from t1
join
( select dt2.x1,
( select sum(a1) from t2 where t2.x1 = dt2.x1 ) m
from ( select x1 from t2 u where x3 = 1 ) dt2
) dt
on t1.id = dt.x1
where t1.id2 < dt.m;
call sp1();
id
3
call sp1();
id
3
create view v2 as select x1 from t2 u where x3 = 1;
create view v as
select v2.x1,
( select sum(a1) from t2 where t2.x1 = v2.x1 ) m from v2;
prepare stmt from "select id from t1 join v on t1.id = v.x1 where t1.id2 < v.m";
execute stmt;
id
3
execute stmt;
id
3
deallocate prepare stmt;
create procedure sp2() select id from t1 join v on t1.id = v.x1 where t1.id2 < v.m;
call sp2();
id
3
call sp2();
id
3
drop procedure sp1;
drop procedure sp2;
drop view v, v2;
drop table t1,t2;
# End of 10.2 tests
#
# MDEV-30706: view defined as select with implicit grouping and
# a set function used in a subquery
#
CREATE TABLE t1 (a INT PRIMARY KEY, b INT);
INSERT INTO t1 VALUES (1,1), (2,2);
CREATE TABLE t2 (a INT PRIMARY KEY, b INT);
INSERT INTO t2 VALUES (1,1), (3,3);
CREATE TABLE t3 (a INT PRIMARY KEY, b INT);
INSERT INTO t3 VALUES (2,2), (4,4), (7,7);
CREATE TABLE t4 (a INT PRIMARY KEY, b INT);
INSERT INTO t4 VALUES (2,2), (5,5), (7,7);
CREATE VIEW v AS SELECT
(SELECT SUM(t4.b) FROM t1, t2 WHERE t1.a = t2.b GROUP BY t1.a) AS m
FROM t3, t4
WHERE t3.a = t4.b;
SELECT
(SELECT SUM(t4.b) FROM t1, t2 WHERE t1.a = t2.b GROUP BY t1.a) AS m
FROM t3, t4
WHERE t3.a = t4.b;
m
9
SELECT * FROM v;
m
9
WITH cte AS ( SELECT
(SELECT SUM(t4.b) FROM t1, t2 WHERE t1.a = t2.b GROUP BY t1.a) AS m
FROM t3, t4
WHERE t3.a = t4.b ) SELECT * FROM cte;
m
9
EXPLAIN SELECT
(SELECT SUM(t4.b) FROM t1, t2 WHERE t1.a = t2.b GROUP BY t1.a) AS m
FROM t3, t4
WHERE t3.a = t4.b;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t4 ALL NULL NULL NULL NULL 3 Using where
1 PRIMARY t3 eq_ref PRIMARY PRIMARY 4 test.t4.b 1 Using index
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using temporary; Using filesort
2 DEPENDENT SUBQUERY t1 eq_ref PRIMARY PRIMARY 4 test.t2.b 1 Using index
EXPLAIN SELECT * FROM v;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 3
2 SUBQUERY t4 ALL NULL NULL NULL NULL 3 Using where
2 SUBQUERY t3 eq_ref PRIMARY PRIMARY 4 test.t4.b 1 Using index
3 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using temporary; Using filesort
3 DEPENDENT SUBQUERY t1 eq_ref PRIMARY PRIMARY 4 test.t2.b 1 Using index
EXPLAIN WITH cte AS ( SELECT
(SELECT SUM(t4.b) FROM t1, t2 WHERE t1.a = t2.b GROUP BY t1.a) AS m
FROM t3, t4
WHERE t3.a = t4.b ) SELECT * FROM cte;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 3
2 DERIVED t4 ALL NULL NULL NULL NULL 3 Using where
2 DERIVED t3 eq_ref PRIMARY PRIMARY 4 test.t4.b 1 Using index
3 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using temporary; Using filesort
3 DEPENDENT SUBQUERY t1 eq_ref PRIMARY PRIMARY 4 test.t2.b 1 Using index
PREPARE stmt FROM "SELECT
(SELECT SUM(t4.b) FROM t1, t2 WHERE t1.a = t2.b GROUP BY t1.a) AS m
FROM t3, t4
WHERE t3.a = t4.b";
execute stmt;
m
9
execute stmt;
m
9
DEALLOCATE PREPARE stmt;
PREPARE stmt FROM "SELECT * FROM v";
execute stmt;
m
9
execute stmt;
m
9
DEALLOCATE PREPARE stmt;
PREPARE stmt FROM "WITH cte AS ( SELECT
(SELECT SUM(t4.b) FROM t1, t2 WHERE t1.a = t2.b GROUP BY t1.a) AS m
FROM t3, t4
WHERE t3.a = t4.b ) SELECT * FROM cte";
execute stmt;
m
9
execute stmt;
m
9
DEALLOCATE PREPARE stmt;
DROP VIEW v;
DROP TABLE t1,t2,t3,t4;
#
# MDEV-29224: view defined as select with implicit grouping and
# a set function used in a subquery
#
CREATE TABLE t1 (f1 INT);
INSERT INTO t1 VALUES (1),(2);
CREATE TABLE t2 (f2 int);
INSERT INTO t2 VALUES (3);
CREATE VIEW v AS SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1;
SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1;
( SELECT MAX(f1) FROM t2 )
2
SELECT * FROM v;
( SELECT MAX(f1) FROM t2 )
2
WITH cte AS ( SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1 ) SELECT * FROM cte;
( SELECT MAX(f1) FROM t2 )
2
EXPLAIN SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 2
2 DEPENDENT SUBQUERY t2 system NULL NULL NULL NULL 1
EXPLAIN SELECT * FROM v;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 2
2 SUBQUERY t1 ALL NULL NULL NULL NULL 2
3 DEPENDENT SUBQUERY t2 system NULL NULL NULL NULL 1
EXPLAIN WITH cte AS ( SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1 ) SELECT * FROM cte;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 2
2 DERIVED t1 ALL NULL NULL NULL NULL 2
3 DEPENDENT SUBQUERY t2 system NULL NULL NULL NULL 1
PREPARE stmt FROM "SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1";
execute stmt;
( SELECT MAX(f1) FROM t2 )
2
execute stmt;
( SELECT MAX(f1) FROM t2 )
2
DEALLOCATE PREPARE stmt;
PREPARE stmt FROM "SELECT * FROM v";
execute stmt;
( SELECT MAX(f1) FROM t2 )
2
execute stmt;
( SELECT MAX(f1) FROM t2 )
2
DEALLOCATE PREPARE stmt;
PREPARE stmt FROM "WITH cte AS ( SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1 ) SELECT * FROM cte";
execute stmt;
( SELECT MAX(f1) FROM t2 )
2
execute stmt;
( SELECT MAX(f1) FROM t2 )
2
DEALLOCATE PREPARE stmt;
DROP VIEW v;
DROP TABLE t1,t2;
#
# MDEV-28573: view defined as select with implicit grouping and
# a set function used in a subquery
#
CREATE TABLE t1 (a INTEGER, b INTEGER);
CREATE TABLE t2 (c INTEGER);
INSERT INTO t1 VALUES (1,11), (2,22), (2,22);
INSERT INTO t2 VALUES (1), (2);
CREATE VIEW v1 AS SELECT (SELECT COUNT(b) FROM t2) FROM t1;
CREATE VIEW v2 AS SELECT (SELECT COUNT(b) FROM t2 WHERE c > 1) FROM t1;
SELECT (SELECT COUNT(b) FROM t2) FROM t1;
ERROR 21000: Subquery returns more than 1 row
SELECT * FROM v1;
ERROR 21000: Subquery returns more than 1 row
WITH cte AS ( SELECT (SELECT COUNT(b) FROM t2) FROM t1 ) SELECT * FROM cte;
ERROR 21000: Subquery returns more than 1 row
SELECT (SELECT COUNT(b) FROM t2 WHERE c > 1) FROM t1;
(SELECT COUNT(b) FROM t2 WHERE c > 1)
3
SELECT * FROM v2;
(SELECT COUNT(b) FROM t2 WHERE c > 1)
3
WITH cte AS ( SELECT (SELECT COUNT(b) FROM t2 WHERE c > 1) FROM t1 ) SELECT * FROM cte;
(SELECT COUNT(b) FROM t2 WHERE c > 1)
3
EXPLAIN SELECT (SELECT COUNT(b) FROM t2) FROM t1;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2
EXPLAIN SELECT * FROM v1;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 3
2 SUBQUERY t1 ALL NULL NULL NULL NULL 3
3 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2
EXPLAIN WITH cte AS ( SELECT (SELECT COUNT(b) FROM t2) FROM t1 ) SELECT * FROM cte;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 3
2 DERIVED t1 ALL NULL NULL NULL NULL 3
3 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2
PREPARE stmt FROM "SELECT (SELECT COUNT(b) FROM t2) FROM t1";
execute stmt;
ERROR 21000: Subquery returns more than 1 row
execute stmt;
ERROR 21000: Subquery returns more than 1 row
DEALLOCATE PREPARE stmt;
PREPARE stmt FROM "SELECT * FROM v1";
execute stmt;
ERROR 21000: Subquery returns more than 1 row
execute stmt;
ERROR 21000: Subquery returns more than 1 row
DEALLOCATE PREPARE stmt;
PREPARE stmt FROM "WITH cte AS ( SELECT (SELECT COUNT(b) FROM t2) FROM t1 ) SELECT * FROM cte";
execute stmt;
ERROR 21000: Subquery returns more than 1 row
execute stmt;
ERROR 21000: Subquery returns more than 1 row
DEALLOCATE PREPARE stmt;
PREPARE stmt FROM "SELECT (SELECT COUNT(b) FROM t2 WHERE c > 1) FROM t1";
execute stmt;
(SELECT COUNT(b) FROM t2 WHERE c > 1)
3
execute stmt;
(SELECT COUNT(b) FROM t2 WHERE c > 1)
3
DEALLOCATE PREPARE stmt;
PREPARE stmt FROM "SELECT * FROM v2";
execute stmt;
(SELECT COUNT(b) FROM t2 WHERE c > 1)
3
execute stmt;
(SELECT COUNT(b) FROM t2 WHERE c > 1)
3
DEALLOCATE PREPARE stmt;
PREPARE stmt FROM "WITH cte AS ( SELECT (SELECT COUNT(b) FROM t2 WHERE c > 1) FROM t1 ) SELECT * FROM cte";
execute stmt;
(SELECT COUNT(b) FROM t2 WHERE c > 1)
3
execute stmt;
(SELECT COUNT(b) FROM t2 WHERE c > 1)
3
DEALLOCATE PREPARE stmt;
DROP VIEW v1,v2;
DROP TABLE t1,t2;
#
# MDEV-28570: VIEW with WHERE containing subquery
# with set function aggregated in query
#
CREATE TABLE t1 (a int, b int);
CREATE TABLE t2 (c int, d int);
INSERT INTO t1 VALUES
(1,10), (2,10), (1,20), (2,20), (3,20), (2,30), (4,40);
INSERT INTO t2 VALUES
(2,10), (2,20), (4,10), (5,10), (3,20), (2,40);
CREATE VIEW v AS SELECT a FROM t1 GROUP BY a
HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20);
SELECT a FROM t1 GROUP BY a
HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20);
a
2
4
SELECT * FROM v;
a
2
4
WITH cte AS ( SELECT a FROM t1 GROUP BY a
HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20) ) SELECT * FROM cte;
a
2
4
EXPLAIN SELECT a FROM t1 GROUP BY a
HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 7 Using temporary; Using filesort
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 6
EXPLAIN SELECT * FROM v;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 7
2 DERIVED t1 ALL NULL NULL NULL NULL 7 Using temporary; Using filesort
3 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 6
EXPLAIN WITH cte AS ( SELECT a FROM t1 GROUP BY a
HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20) ) SELECT * FROM cte;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 7
2 DERIVED t1 ALL NULL NULL NULL NULL 7 Using temporary; Using filesort
3 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 6
PREPARE stmt FROM "SELECT a FROM t1 GROUP BY a
HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20)";
execute stmt;
a
2
4
execute stmt;
a
2
4
DEALLOCATE PREPARE stmt;
PREPARE stmt FROM "SELECT * FROM v";
execute stmt;
a
2
4
execute stmt;
a
2
4
DEALLOCATE PREPARE stmt;
PREPARE stmt FROM "WITH cte AS ( SELECT a FROM t1 GROUP BY a
HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20) ) SELECT * FROM cte";
execute stmt;
a
2
4
execute stmt;
a
2
4
DEALLOCATE PREPARE stmt;
DROP VIEW v;
DROP TABLE t1,t2;
#
# MDEV-28571: VIEW with select list containing subquery
# with set function aggregated in query
#
CREATE TABLE t1 (a int, b int);
CREATE TABLE t2 (m int, n int);
INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44);
CREATE VIEW v AS SELECT (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a) AS c
FROM t1
GROUP BY a;
SELECT (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a) AS c
FROM t1
GROUP BY a;
c
2
3
1,1
SELECT * FROM v;
c
2
3
1,1
WITH cte AS ( SELECT (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a) AS c
FROM t1
GROUP BY a ) SELECT * FROM cte;
c
2
3
1,1
EXPLAIN SELECT (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a) AS c
FROM t1
GROUP BY a;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 6 Using temporary; Using filesort
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
EXPLAIN SELECT * FROM v;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 6
2 DERIVED t1 ALL NULL NULL NULL NULL 6 Using temporary; Using filesort
3 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
EXPLAIN WITH cte AS ( SELECT (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a) AS c
FROM t1
GROUP BY a ) SELECT * FROM cte;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 6
2 DERIVED t1 ALL NULL NULL NULL NULL 6 Using temporary; Using filesort
3 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
PREPARE stmt FROM "SELECT (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a) AS c
FROM t1
GROUP BY a";
execute stmt;
c
2
3
1,1
execute stmt;
c
2
3
1,1
DEALLOCATE PREPARE stmt;
PREPARE stmt FROM "SELECT * FROM v";
execute stmt;
c
2
3
1,1
execute stmt;
c
2
3
1,1
DEALLOCATE PREPARE stmt;
PREPARE stmt FROM "WITH cte AS ( SELECT (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a) AS c
FROM t1
GROUP BY a ) SELECT * FROM cte";
execute stmt;
c
2
3
1,1
execute stmt;
c
2
3
1,1
DEALLOCATE PREPARE stmt;
DROP VIEW v;
DROP TABLE t1,t2;
#
# MDEV-30668: VIEW with WHERE containing nested subquery
# with set function aggregated in outer subquery
#
create table t1 (a int);
insert into t1 values (3), (7), (1);
create table t2 (b int);
insert into t2 values (2), (1), (4), (7);
create table t3 (a int, b int);
insert into t3 values (2,10), (7,30), (2,30), (1,10), (7,40);
create view v as select * from t1
where t1.a in (select t3.a from t3 group by t3.a
having t3.a > any (select t2.b from t2
where t2.b*10 < sum(t3.b)));
select * from t1
where t1.a in (select t3.a from t3 group by t3.a
having t3.a > any (select t2.b from t2
where t2.b*10 < sum(t3.b)));
a
7
select * from v;
a
7
with cte as ( select * from t1
where t1.a in (select t3.a from t3 group by t3.a
having t3.a > any (select t2.b from t2
where t2.b*10 < sum(t3.b))) ) select * from cte;
a
7
explain select * from t1
where t1.a in (select t3.a from t3 group by t3.a
having t3.a > any (select t2.b from t2
where t2.b*10 < sum(t3.b)));
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 test.t1.a 1
2 MATERIALIZED t3 ALL NULL NULL NULL NULL 5 Using temporary
3 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 4 Using where
explain select * from v;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 test.t1.a 1
3 MATERIALIZED t3 ALL NULL NULL NULL NULL 5 Using temporary
4 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 4 Using where
explain with cte as ( select * from t1
where t1.a in (select t3.a from t3 group by t3.a
having t3.a > any (select t2.b from t2
where t2.b*10 < sum(t3.b))) ) select * from cte;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 test.t1.a 1
3 MATERIALIZED t3 ALL NULL NULL NULL NULL 5 Using temporary
4 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 4 Using where
prepare stmt from "select * from t1
where t1.a in (select t3.a from t3 group by t3.a
having t3.a > any (select t2.b from t2
where t2.b*10 < sum(t3.b)))";
execute stmt;
a
7
execute stmt;
a
7
deallocate prepare stmt;
prepare stmt from "select * from v";
execute stmt;
a
7
execute stmt;
a
7
deallocate prepare stmt;
prepare stmt from "with cte as ( select * from t1
where t1.a in (select t3.a from t3 group by t3.a
having t3.a > any (select t2.b from t2
where t2.b*10 < sum(t3.b))) ) select * from cte";
execute stmt;
a
7
execute stmt;
a
7
deallocate prepare stmt;
drop view v;
drop table t1,t2,t3;
#
# MDEV-32829 Crash when executing PS for query with eliminated subquery
# using view
#
create view v1 as select 1 as a;
prepare stmt from
'SELECT EXISTS (SELECT 1 FROM v1 GROUP BY a IN (SELECT a FROM v1))';
execute stmt;
EXISTS (SELECT 1 FROM v1 GROUP BY a IN (SELECT a FROM v1))
1
drop view v1;
create table t1 (a int, b int);
insert into t1 values (1,2),(3,4),(5,6);
create view v1 as select * from t1;
create table t2 select * from t1;
prepare stmt from "select t2.a from t2 where exists
(
select * from t1 where t2.b = t1.b and t1.b != 6
group by a in (select a from v1 where v1.a = t2.a)
)";
execute stmt;
a
1
3
execute stmt;
a
1
3
deallocate prepare stmt;
create procedure aproc() select t2.a from t2 where exists
(
select * from t1 where t2.b = t1.b and t1.b != 6
group by a in (select a from v1 where v1.a = t2.a)
);
call aproc();
a
1
3
call aproc();
a
1
3
drop table t1, t2;
drop view v1;
drop procedure aproc;
#
# MDEV-31305: Aggregation over materialized derived table
#
CREATE VIEW v AS
SELECT seq1.seq AS dim1, seq2.seq AS dim2, seq3.seq AS dim3,
FLOOR(RAND(13) * 5) AS p
FROM seq_100_to_105 seq1
JOIN seq_10_to_15 seq2
JOIN seq_1_to_5 seq3;
SELECT v.*, SUM(p) from v;
dim1 dim2 dim3 p SUM(p)
100 10 1 2 371
SELECT d.*, SUM(p)
FROM (
SELECT seq1.seq AS dim1, seq2.seq AS dim2, seq3.seq AS dim3,
FLOOR(RAND(13) * 5) AS p
FROM seq_100_to_105 seq1
JOIN seq_10_to_15 seq2
JOIN seq_1_to_5 seq3
) d;
dim1 dim2 dim3 p SUM(p)
100 10 1 2 371
WITH demo AS
(
SELECT seq1.seq AS dim1, seq2.seq AS dim2, seq3.seq AS dim3,
FLOOR(RAND(13) * 5) AS p
FROM seq_100_to_105 seq1
JOIN seq_10_to_15 seq2
JOIN seq_1_to_5 seq3
)
SELECT d.*, SUM(p) FROM demo d;
dim1 dim2 dim3 p SUM(p)
100 10 1 2 371
DROP VIEW v;
# End of 10.4 tests
#
# MDEV-31277: 2-nd execution of PS to select from materialized view
# specified as left join whose inner table is mergeable
# derived containing a constant column
#
create table t1 (
Election int(10) unsigned NOT NULL
) engine=MyISAM;
insert into t1 (Election) values (1), (4);
create table t2 (
VoteID int(10),
ElectionID int(10),
UserID int(10)
);
insert into t2 (ElectionID, UserID) values (2, 30), (3, 30);
create view v1 as select * from t1
left join ( select 'Y' AS Voted, ElectionID from t2 ) AS T
on T.ElectionID = t1.Election
limit 9;
prepare stmt1 from "select * from v1";
execute stmt1;
Election Voted ElectionID
1 NULL NULL
4 NULL NULL
execute stmt1;
Election Voted ElectionID
1 NULL NULL
4 NULL NULL
deallocate prepare stmt1;
drop view v1;
drop table t1, t2;
# End of 10.5 tests
#
# MDEV-31143: view with ORDER BY used in query with rownum() in WHERE
#
create table t1 (id int primary key);
insert into t1 values (3), (7), (1);
create table t2 (a int);
insert into t2 values (2), (4);
create view v as select a from t2 order by a;
set big_tables= 1;
Warnings:
Warning 1287 '@@big_tables' is deprecated and will be removed in a future release
select t1.id from v, t1 where rownum() = 1 group by t1.id;
id
1
set big_tables=default;
Warnings:
Warning 1287 '@@big_tables' is deprecated and will be removed in a future release
drop view v;
drop table t1, t2;
#
# MDEV-31162: multi-table mergeable view with ORDER BY used
# in query with rownum() in WHERE
#
create table t1 (a INT) engine=MyISAM;
insert into t1 values (1),(2);
create table t2 (b INT) engine=MyISAM;
insert into t2 values (3),(4);
create view v1 AS select * from t1 join t2 order by b;
explain select * from v1 where rownum() <= 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 Using temporary; Using filesort
1 SIMPLE t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
select * from v1 where rownum() <= 2;
a b
1 3
2 3
prepare stmt from "select * from v1 where rownum() <= 2";
execute stmt;
a b
1 3
2 3
execute stmt;
a b
1 3
2 3
deallocate prepare stmt;
create view v2 AS select * from t1 join t2 order by b/a;
explain select * from v2 where rownum() <= 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 Using temporary; Using filesort
1 SIMPLE t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
select * from v2 where rownum() <= 2;
a b
2 3
1 3
prepare stmt from "select * from v2 where rownum() <= 2";
execute stmt;
a b
2 3
1 3
execute stmt;
a b
2 3
1 3
deallocate prepare stmt;
drop view v1,v2;
drop table t1,t2;
# End of 10.6 tests
|