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
|
CREATE TABLE wp(
FTS_DOC_ID BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
title VARCHAR(255) NOT NULL DEFAULT '',
text MEDIUMTEXT NOT NULL,
dummy INTEGER,
PRIMARY KEY (FTS_DOC_ID),
UNIQUE KEY FTS_DOC_ID_INDEX (FTS_DOC_ID),
FULLTEXT KEY idx (title,text)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO wp (title, text) VALUES
('MySQL Tutorial','DBMS stands for MySQL DataBase ...'),
('How To Use MySQL Well','After you went through a ...'),
('Optimizing MySQL','In this tutorial we will show ...'),
('1001 MySQL Tricks','1. Never run mysqld as root. 2. ...'),
('MySQL vs. YourSQL','In the following database to database comparison ...'),
('MySQL Security','When configured properly, MySQL ...');
CREATE TABLE t1 (i INTEGER);
INSERT INTO t1 SELECT FTS_DOC_ID FROM wp;
SET STATEMENT use_stat_tables=never FOR
ANALYZE TABLE t1;
Table Op Msg_type Msg_text
test.t1 analyze status OK
SET STATEMENT use_stat_tables=never FOR
ANALYZE TABLE wp;
Table Op Msg_type Msg_text
test.wp analyze status OK
SELECT FTS_DOC_ID, title, MATCH(title, text) AGAINST ('database') AS score1,
MATCH(title, text) AGAINST ('mysql') AS score2
FROM wp;
FTS_DOC_ID title score1 score2
1 MySQL Tutorial 0.22764469683170319 0.000000003771856604828372
2 How To Use MySQL Well 0 0.000000001885928302414186
3 Optimizing MySQL 0 0.000000001885928302414186
4 1001 MySQL Tricks 0 0.000000001885928302414186
5 MySQL vs. YourSQL 0.45528939366340637 0.000000001885928302414186
6 MySQL Security 0 0.000000003771856604828372
No sorting for this query
FLUSH STATUS;
SELECT title, MATCH(title, text) AGAINST ('database') AS score
FROM wp
WHERE MATCH(title, text) AGAINST ('database')
ORDER BY score DESC;
title score
MySQL vs. YourSQL 0.45528939366340637
MySQL Tutorial 0.22764469683170319
SHOW SESSION STATUS LIKE 'Sort%';
Variable_name Value
Sort_merge_passes 0
Sort_priority_queue_sorts 0
Sort_range 0
Sort_rows 2
Sort_scan 1
No sorting for this query even if MATCH is part of an expression
FLUSH STATUS;
SELECT title, MATCH(title, text) AGAINST ('database') AS score
FROM wp
WHERE MATCH(title, text) AGAINST ('database') > 0.1
ORDER BY score DESC;
title score
MySQL vs. YourSQL 0.45528939366340637
MySQL Tutorial 0.22764469683170319
SHOW SESSION STATUS LIKE 'Sort%';
Variable_name Value
Sort_merge_passes 0
Sort_priority_queue_sorts 0
Sort_range 0
Sort_rows 2
Sort_scan 1
No sorting even if there are several MATCH expressions as long as the
right one is used in ORDER BY
FLUSH STATUS;
SELECT title, MATCH(title, text) AGAINST ('database') AS score1,
MATCH(title, text) AGAINST ('mysql') AS score2
FROM wp
WHERE MATCH(title, text) AGAINST ('database')
ORDER BY score1 DESC;
title score1 score2
MySQL vs. YourSQL 0.45528939366340637 0.000000001885928302414186
MySQL Tutorial 0.22764469683170319 0.000000003771856604828372
SHOW SESSION STATUS LIKE 'Sort%';
Variable_name Value
Sort_merge_passes 0
Sort_priority_queue_sorts 0
Sort_range 0
Sort_rows 2
Sort_scan 1
No Sorting since FT table is first table in query
FLUSH STATUS;
SELECT title, MATCH(title, text) AGAINST ('database') AS score
FROM wp, t1
WHERE MATCH(title, text) AGAINST ('database') AND FTS_DOC_ID = t1.i
ORDER BY score DESC;
title score
MySQL vs. YourSQL 0.45528939366340637
MySQL Tutorial 0.22764469683170319
SHOW SESSION STATUS LIKE 'Sort_rows%';
Variable_name Value
Sort_rows 2
Sorting since there is no WHERE clause
FLUSH STATUS;
SELECT MATCH(title, text) AGAINST ('database'), title AS score
FROM wp
ORDER BY score DESC;
MATCH(title, text) AGAINST ('database') score
0 1001 MySQL Tricks
0 How To Use MySQL Well
0 MySQL Security
0 Optimizing MySQL
0.22764469683170319 MySQL Tutorial
0.45528939366340637 MySQL vs. YourSQL
SHOW SESSION STATUS LIKE 'Sort_rows%';
Variable_name Value
Sort_rows 6
Sorting since ordering on multiple columns
FLUSH STATUS;
SELECT title, MATCH(title, text) AGAINST ('database') AS score
FROM wp
WHERE MATCH(title, text) AGAINST ('database')
ORDER BY score DESC, FTS_DOC_ID;
title score
MySQL vs. YourSQL 0.45528939366340637
MySQL Tutorial 0.22764469683170319
SHOW SESSION STATUS LIKE 'Sort_rows%';
Variable_name Value
Sort_rows 2
Sorting since ordering is not descending
FLUSH STATUS;
SELECT title, MATCH(title, text) AGAINST ('database') AS score
FROM wp
WHERE MATCH(title, text) AGAINST ('database')
ORDER BY score ASC;
title score
MySQL Tutorial 0.22764469683170319
MySQL vs. YourSQL 0.45528939366340637
SHOW SESSION STATUS LIKE 'Sort_rows%';
Variable_name Value
Sort_rows 2
Sorting because one is ordering on a different MATCH expression
FLUSH STATUS;
SELECT title, MATCH(title, text) AGAINST ('mysql') AS score
FROM wp
WHERE MATCH(title, text) AGAINST ('database')
ORDER BY score DESC;
title score
MySQL Tutorial 0.000000003771856604828372
MySQL vs. YourSQL 0.000000001885928302414186
SHOW SESSION STATUS LIKE 'Sort_rows%';
Variable_name Value
Sort_rows 2
No sorting for this query
FLUSH STATUS;
SELECT title, MATCH(title, text) AGAINST ('database') AS score
FROM wp
ORDER BY score DESC LIMIT 2;
title score
MySQL vs. YourSQL 0.45528939366340637
MySQL Tutorial 0.22764469683170319
SHOW SESSION STATUS LIKE 'Sort%';
Variable_name Value
Sort_merge_passes 0
Sort_priority_queue_sorts 1
Sort_range 0
Sort_rows 2
Sort_scan 1
Revert to table scan and sorting for this query since not
enough matching rows to satisfy LIMIT clause
FLUSH STATUS;
SELECT title, MATCH(title, text) AGAINST ('database') AS score
FROM wp
ORDER BY score DESC LIMIT 2;
title score
MySQL vs. YourSQL 0.45528939366340637
MySQL Tutorial 0.22764469683170319
SHOW SESSION 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 2
Handler_read_rnd_deleted 0
Handler_read_rnd_next 14
SHOW SESSION STATUS LIKE 'Sort_rows%';
Variable_name Value
Sort_rows 2
Sorting since no LIMIT clause
FLUSH STATUS;
SELECT MATCH(title, text) AGAINST ('database') AS score, title
FROM wp
ORDER BY score DESC;
score title
0 1001 MySQL Tricks
0 How To Use MySQL Well
0 MySQL Security
0 Optimizing MySQL
0.22764469683170319 MySQL Tutorial
0.45528939366340637 MySQL vs. YourSQL
SHOW SESSION STATUS LIKE 'Sort_rows%';
Variable_name Value
Sort_rows 6
Sorting since there is a WHERE clause
FLUSH STATUS;
SELECT title, MATCH(title, text) AGAINST ('database') AS score
FROM wp
WHERE dummy IS NULL
ORDER BY score DESC LIMIT 2;
title score
MySQL vs. YourSQL 0.45528939366340637
MySQL Tutorial 0.22764469683170319
SHOW SESSION STATUS LIKE 'Sort_rows%';
Variable_name Value
Sort_rows 2
Sorting since ordering is not on a simple MATCH expressions
FLUSH STATUS;
SELECT title, (MATCH(title, text) AGAINST ('database')) * 100 AS score
FROM wp
ORDER BY score DESC LIMIT 2;
title score
MySQL vs. YourSQL 45.52893936634064
MySQL Tutorial 22.76446968317032
SHOW SESSION STATUS LIKE 'Sort_rows%';
Variable_name Value
Sort_rows 2
No ordinary handler accesses when only accessing FTS_DOC_ID and MATCH
FLUSH STATUS;
SELECT FTS_DOC_ID docid, MATCH(title, text) AGAINST ('database') AS score
FROM wp
WHERE MATCH(title, text) AGAINST ('database');
docid score
5 0.45528939366340637
1 0.22764469683170319
SHOW SESSION 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
Still no handler accesses when adding FTS_DOC_ID to WHERE clause
FLUSH STATUS;
SELECT FTS_DOC_ID docid, MATCH(title, text) AGAINST ('database') AS score
FROM wp
WHERE MATCH(title, text) AGAINST ('database') AND FTS_DOC_ID > 2;
docid score
5 0.45528939366340637
SHOW SESSION 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
Still no handler accesses when ordering by MATCH expression
FLUSH STATUS;
SELECT FTS_DOC_ID docid, MATCH(title, text) AGAINST ('database') AS score
FROM wp
WHERE MATCH(title, text) AGAINST ('database')
ORDER BY score;
docid score
1 0.22764469683170319
5 0.45528939366340637
SHOW SESSION 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 2
Handler_read_rnd_deleted 0
Handler_read_rnd_next 3
Optimization is disabled when ordering on FTS_DOC_ID
FLUSH STATUS;
SELECT FTS_DOC_ID docid, MATCH(title, text) AGAINST ('database') AS score
FROM wp
WHERE MATCH(title, text) AGAINST ('database')
ORDER BY 1 DESC;
docid score
5 0.45528939366340637
1 0.22764469683170319
SHOW SESSION 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 2
Handler_read_rnd_deleted 0
Handler_read_rnd_next 0
Optimization also work with several MATCH expressions
FLUSH STATUS;
SELECT FTS_DOC_ID docid, MATCH(title, text) AGAINST ('database') AS score1,
MATCH(title, text) AGAINST ('mysql') AS score2
FROM wp
WHERE MATCH(title, text) AGAINST ('database');
docid score1 score2
5 0.45528939366340637 0.000000001885928302414186
1 0.22764469683170319 0.000000003771856604828372
SHOW SESSION 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
Optimization does not apply if sorting on a different MATCH expressions
from the one used to access the
FLUSH STATUS;
SELECT FTS_DOC_ID docid, MATCH(title, text) AGAINST ('database') AS score1,
MATCH(title, text) AGAINST ('mysql') AS score2
FROM wp
WHERE MATCH(title, text) AGAINST ('database')
ORDER BY score2 DESC;
docid score1 score2
1 0.22764469683170319 0.000000003771856604828372
5 0.45528939366340637 0.000000001885928302414186
SHOW SESSION 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 2
Handler_read_rnd_deleted 0
Handler_read_rnd_next 3
FLUSH STATUS;
Optimization does not apply for GROUP BY
SET @save_mode = @@sql_mode;
SET sql_mode = (select replace(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
SELECT FTS_DOC_ID, MATCH(title, text) AGAINST ('database') AS score
FROM wp
WHERE MATCH(title, text) AGAINST ('database')
GROUP BY score;
FTS_DOC_ID score
1 0.22764469683170319
5 0.45528939366340637
SHOW SESSION 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 2
Handler_read_rnd_deleted 0
Handler_read_rnd_next 3
SET sql_mode = @save_mode;
No sorting and no table access with LIMIT clause and only information
from FTS result
FLUSH STATUS;
SELECT FTS_DOC_ID docid, MATCH(title, text) AGAINST ('database') AS score
FROM wp
ORDER BY score DESC LIMIT 2;
docid score
5 0.45528939366340637
1 0.22764469683170319
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 2
Handler_read_rnd_deleted 0
Handler_read_rnd_next 14
SHOW SESSION STATUS LIKE 'Sort%';
Variable_name Value
Sort_merge_passes 0
Sort_priority_queue_sorts 1
Sort_range 0
Sort_rows 2
Sort_scan 1
If count optimization applies, EXPLAIN shows
"Select tables optimized away."
EXPLAIN SELECT COUNT(*)
FROM wp
WHERE MATCH(title,text) AGAINST ('database' IN NATURAL LANGUAGE MODE);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE wp fulltext idx idx 0 1 Using where
FLUSH STATUS;
SELECT COUNT(*)
FROM wp
WHERE MATCH(title,text) AGAINST ('database' IN NATURAL LANGUAGE MODE);
COUNT(*)
2
Verify that there was no table access
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
Optimization applies also to COUNT(expr) as long as expr is not nullable
EXPLAIN SELECT COUNT(title)
FROM wp
WHERE MATCH(title,text) AGAINST ('database' IN NATURAL LANGUAGE MODE);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE wp fulltext idx idx 0 1 Using where
SELECT COUNT(title)
FROM wp
WHERE MATCH(title,text) AGAINST ('database' IN NATURAL LANGUAGE MODE);
COUNT(title)
2
Optimization does not apply if not a single table query.
EXPLAIN SELECT count(*)
FROM wp, t1
WHERE MATCH(title, text) AGAINST ('database');
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE wp fulltext idx idx 0 1 Using where
1 SIMPLE t1 ALL NULL NULL NULL NULL 6
SELECT count(*)
FROM wp, t1
WHERE MATCH(title, text) AGAINST ('database');
count(*)
12
Optimization does not apply if MATCH is part of an expression
EXPLAIN SELECT COUNT(title)
FROM wp
WHERE MATCH(title,text) AGAINST ('database' IN NATURAL LANGUAGE MODE) > 0;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE wp fulltext idx idx 0 1 Using where
SELECT COUNT(title)
FROM wp
WHERE MATCH(title,text) AGAINST ('database' IN NATURAL LANGUAGE MODE) > 0;
COUNT(title)
2
Optimization does not apply if MATCH is part of an expression
EXPLAIN SELECT COUNT(title)
FROM wp
WHERE MATCH(title,text) AGAINST ('database' IN NATURAL LANGUAGE MODE) > 0;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE wp fulltext idx idx 0 1 Using where
SELECT COUNT(title)
FROM wp
WHERE MATCH(title,text) AGAINST ('database' IN NATURAL LANGUAGE MODE) > 0;
COUNT(title)
2
Optimization does not apply if COUNT expression is nullable
EXPLAIN SELECT COUNT(dummy)
FROM wp
WHERE MATCH(title,text) AGAINST ('database' IN NATURAL LANGUAGE MODE);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE wp fulltext idx idx 0 1 Using where
SELECT COUNT(dummy)
FROM wp
WHERE MATCH(title,text) AGAINST ('database' IN NATURAL LANGUAGE MODE);
COUNT(dummy)
0
FLUSH STATUS;
SELECT MATCH(title, text) AGAINST ('database' WITH QUERY EXPANSION) AS score,
title
FROM wp
WHERE MATCH(title, text) AGAINST ('database' WITH QUERY EXPANSION)
ORDER BY score DESC;
score title
0.000000001885928302414186 1001 MySQL Tricks
0.000000001885928302414186 How To Use MySQL Well
0.000000003771856604828372 MySQL Security
0.22764469683170319 Optimizing MySQL
1.6663280725479126 MySQL Tutorial
2.2718474864959717 MySQL vs. YourSQL
SHOW SESSION STATUS LIKE 'Sort%';
Variable_name Value
Sort_merge_passes 0
Sort_priority_queue_sorts 0
Sort_range 0
Sort_rows 6
Sort_scan 1
FLUSH STATUS;
SELECT title,
MATCH(title, text) AGAINST ('database' WITH QUERY EXPANSION) AS score
FROM wp
ORDER BY score DESC LIMIT 2;
title score
MySQL vs. YourSQL 2.2718474864959717
MySQL Tutorial 1.6663280725479126
SHOW SESSION STATUS LIKE 'Sort%';
Variable_name Value
Sort_merge_passes 0
Sort_priority_queue_sorts 1
Sort_range 0
Sort_rows 2
Sort_scan 1
FLUSH STATUS;
SELECT FTS_DOC_ID docid,
MATCH(title, text) AGAINST ('database' WITH QUERY EXPANSION) AS score
FROM wp
WHERE MATCH(title, text) AGAINST ('database');
docid score
5 2.2718474864959717
1 1.6663280725479126
SHOW SESSION 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 FTS_DOC_ID docid,
MATCH(title, text) AGAINST ('database' WITH QUERY EXPANSION) AS score
FROM wp
ORDER BY score DESC LIMIT 2;
docid score
5 2.2718474864959717
1 1.6663280725479126
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 2
Handler_read_rnd_deleted 0
Handler_read_rnd_next 14
SHOW SESSION STATUS LIKE 'Sort%';
Variable_name Value
Sort_merge_passes 0
Sort_priority_queue_sorts 1
Sort_range 0
Sort_rows 2
Sort_scan 1
EXPLAIN SELECT COUNT(*)
FROM wp
WHERE MATCH(title,text) AGAINST ('database' WITH QUERY EXPANSION);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE wp fulltext idx idx 0 1 Using where
FLUSH STATUS;
SELECT COUNT(*)
FROM wp
WHERE MATCH(title,text) AGAINST ('database' WITH QUERY EXPANSION);
COUNT(*)
6
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 MATCH(title, text) AGAINST ('+MySQL -database' IN BOOLEAN MODE) score,
title
FROM wp
WHERE MATCH(title, text) AGAINST ('+MySQL -database' IN BOOLEAN MODE)
ORDER BY score DESC;
score title
0.000000001885928302414186 1001 MySQL Tricks
0.000000001885928302414186 How To Use MySQL Well
0.000000001885928302414186 Optimizing MySQL
0.000000003771856604828372 MySQL Security
SHOW SESSION STATUS LIKE 'Sort%';
Variable_name Value
Sort_merge_passes 0
Sort_priority_queue_sorts 0
Sort_range 0
Sort_rows 4
Sort_scan 1
FLUSH STATUS;
SELECT MATCH(title, text) AGAINST ('+MySQL -database' IN BOOLEAN MODE) score,
title
FROM wp
ORDER BY score DESC;
score title
0 MySQL Tutorial
0 MySQL vs. YourSQL
0.000000001885928302414186 1001 MySQL Tricks
0.000000001885928302414186 How To Use MySQL Well
0.000000001885928302414186 Optimizing MySQL
0.000000003771856604828372 MySQL Security
SHOW SESSION STATUS LIKE 'Sort%';
Variable_name Value
Sort_merge_passes 0
Sort_priority_queue_sorts 0
Sort_range 0
Sort_rows 6
Sort_scan 1
FLUSH STATUS;
SELECT FTS_DOC_ID docid,
MATCH(title, text) AGAINST ('+MySQL -database' IN BOOLEAN MODE) AS score
FROM wp
WHERE MATCH(title, text) AGAINST ('+MySQL -database');
docid score
5 0
1 0
6 0.000000003771856604828372
2 0.000000001885928302414186
3 0.000000001885928302414186
4 0.000000001885928302414186
SHOW SESSION 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 FTS_DOC_ID docid,
MATCH(title, text) AGAINST ('+MySQL -database' IN BOOLEAN MODE) AS score
FROM wp
ORDER BY score DESC LIMIT 1;
docid score
6 0.000000003771856604828372
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 1
Handler_read_rnd_deleted 0
Handler_read_rnd_next 14
SHOW SESSION STATUS LIKE 'Sort%';
Variable_name Value
Sort_merge_passes 0
Sort_priority_queue_sorts 1
Sort_range 0
Sort_rows 1
Sort_scan 1
EXPLAIN SELECT COUNT(*)
FROM wp
WHERE MATCH(title,text) AGAINST ('+MySQL -database' IN BOOLEAN MODE);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE wp fulltext idx idx 0 1 Using where
FLUSH STATUS;
SELECT COUNT(*)
FROM wp
WHERE MATCH(title,text) AGAINST ('+MySQL -database' IN BOOLEAN MODE);
COUNT(*)
4
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 title,
MATCH(title, text) AGAINST ('"MySQL database"@5' IN BOOLEAN MODE) AS score
FROM wp
WHERE MATCH(title, text) AGAINST ('"MySQL database"@5' IN BOOLEAN MODE)
ORDER BY score DESC;
title score
MySQL Tutorial 0.22764469683170319
SHOW SESSION STATUS LIKE 'Sort%';
Variable_name Value
Sort_merge_passes 0
Sort_priority_queue_sorts 0
Sort_range 0
Sort_rows 1
Sort_scan 1
FLUSH STATUS;
SELECT title,
MATCH(title, text) AGAINST ('"MySQL database"@5' IN BOOLEAN MODE) AS score
FROM wp
ORDER BY score DESC LIMIT 1;
title score
MySQL Tutorial 0.22764469683170319
SHOW SESSION STATUS LIKE 'Sort%';
Variable_name Value
Sort_merge_passes 0
Sort_priority_queue_sorts 1
Sort_range 0
Sort_rows 1
Sort_scan 1
FLUSH STATUS;
SELECT FTS_DOC_ID docid,
MATCH(title, text) AGAINST ('"MySQL database"@5' IN BOOLEAN MODE) AS score
FROM wp
WHERE MATCH(title, text) AGAINST ('"MySQL database"@5');
docid score
1 0.22764469683170319
SHOW SESSION 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 FTS_DOC_ID docid,
MATCH(title, text) AGAINST ('"MySQL database"@5' IN BOOLEAN MODE) AS score
FROM wp
ORDER BY score DESC LIMIT 1;
docid score
1 0.22764469683170319
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 1
Handler_read_rnd_deleted 0
Handler_read_rnd_next 14
SHOW SESSION STATUS LIKE 'Sort%';
Variable_name Value
Sort_merge_passes 0
Sort_priority_queue_sorts 1
Sort_range 0
Sort_rows 1
Sort_scan 1
EXPLAIN SELECT COUNT(*)
FROM wp
WHERE MATCH(title,text) AGAINST ('"MySQL database"@5' IN BOOLEAN MODE);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE wp fulltext idx idx 0 1 Using where
FLUSH STATUS;
SELECT COUNT(*)
FROM wp
WHERE MATCH(title,text) AGAINST ('"MySQL database"@5' IN BOOLEAN MODE);
COUNT(*)
1
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
SELECT title,
MATCH(title, text) AGAINST ('database') AS score
FROM wp
WHERE MATCH(title, text) AGAINST ('database' WITH QUERY EXPANSION)
ORDER BY score DESC, title ASC;
title score
MySQL vs. YourSQL 0.45528939366340637
MySQL Tutorial 0.22764469683170319
1001 MySQL Tricks 0
How To Use MySQL Well 0
MySQL Security 0
Optimizing MySQL 0
SELECT title,
MATCH(title, text) AGAINST ('+MySQL -database' IN BOOLEAN MODE) AS score
FROM wp
WHERE MATCH(title, text) AGAINST ('MySQL database' WITH QUERY EXPANSION)
ORDER BY score DESC, title ASC;
title score
MySQL Security 0.000000003771856604828372
1001 MySQL Tricks 0.000000001885928302414186
How To Use MySQL Well 0.000000001885928302414186
Optimizing MySQL 0.000000001885928302414186
MySQL Tutorial 0
MySQL vs. YourSQL 0
SELECT title,
MATCH(title, text) AGAINST ('+MySQL -database' IN BOOLEAN MODE) AS score
FROM wp
WHERE MATCH(title, text) AGAINST ('"MySQL database"@5' IN BOOLEAN MODE)
ORDER BY score DESC, title ASC;
title score
MySQL Tutorial 0
ALTER TABLE wp ENGINE=myisam;
FLUSH STATUS;
SELECT title, MATCH(title, text) AGAINST ('database') AS score
FROM wp
WHERE MATCH(title, text) AGAINST ('database')
ORDER BY score DESC;
title score
MySQL vs. YourSQL 0.9562782645225525
MySQL Tutorial 0.5756555199623108
SHOW SESSION STATUS LIKE 'Sort%';
Variable_name Value
Sort_merge_passes 0
Sort_priority_queue_sorts 0
Sort_range 0
Sort_rows 2
Sort_scan 1
FLUSH STATUS;
SELECT title, MATCH(title, text) AGAINST ('database') AS score
FROM wp
ORDER BY score DESC LIMIT 2;
title score
MySQL vs. YourSQL 0.9562782645225525
MySQL Tutorial 0.5756555199623108
SHOW SESSION STATUS LIKE 'Sort%';
Variable_name Value
Sort_merge_passes 0
Sort_priority_queue_sorts 1
Sort_range 0
Sort_rows 2
Sort_scan 1
FLUSH STATUS;
SELECT FTS_DOC_ID docid, MATCH(title, text) AGAINST ('database') AS score
FROM wp
WHERE MATCH(title, text) AGAINST ('database');
docid score
5 0.9562782645225525
1 0.5756555199623108
SHOW SESSION STATUS LIKE 'Handler_read%';
Variable_name Value
Handler_read_first 0
Handler_read_key 0
Handler_read_last 0
Handler_read_next 3
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 FTS_DOC_ID docid, MATCH(title, text) AGAINST ('database') AS score
FROM wp
ORDER BY score DESC LIMIT 2;
docid score
5 0.9562782645225525
1 0.5756555199623108
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 2
Handler_read_rnd_deleted 0
Handler_read_rnd_next 14
SHOW SESSION STATUS LIKE 'Sort%';
Variable_name Value
Sort_merge_passes 0
Sort_priority_queue_sorts 1
Sort_range 0
Sort_rows 2
Sort_scan 1
EXPLAIN SELECT COUNT(*)
FROM wp
WHERE MATCH(title,text) AGAINST ('database' IN NATURAL LANGUAGE MODE);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE wp fulltext idx idx 0 1 Using where
FLUSH STATUS;
SELECT COUNT(*)
FROM wp
WHERE MATCH(title,text) AGAINST ('database' IN NATURAL LANGUAGE MODE);
COUNT(*)
2
SHOW STATUS LIKE 'Handler_read%';
Variable_name Value
Handler_read_first 0
Handler_read_key 0
Handler_read_last 0
Handler_read_next 3
Handler_read_prev 0
Handler_read_retry 0
Handler_read_rnd 0
Handler_read_rnd_deleted 0
Handler_read_rnd_next 0
DROP TABLE wp, t1;
CREATE TABLE t1
(
FTS_DOC_ID BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
title VARCHAR(255) DEFAULT '',
text MEDIUMTEXT ,
PRIMARY KEY (FTS_DOC_ID),
UNIQUE KEY FTS_DOC_ID_INDEX (FTS_DOC_ID),
FULLTEXT KEY ft_idx (title,text)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO t1 (title, text) VALUES
('MySQL Tutorial','DBMS stands for MySQL DataBase ...'),
('How To Use MySQL Well','After you went through a ...'),
('Optimizing MySQL','In this tutorial we will show ...'),
('1001 MySQL Tricks','1. Never run mysqld as root. 2. ...'),
('MySQL vs. YourSQL database','In the following database to database comparison ...'),
('MySQL Security','When configured properly, MySQL ...'),
('InnoDB', 'InnoDB is a transaction-safe (ACID compliant) storage engine'),
('MySQL is a database management system', 'A database is a structured collection of data...'),
('MySQL databases are relational', 'A relational database stores data in separate tables rather than putting all the data in one big storeroom...'),
('MySQL software is Open Source', 'Open Source means that it is possible for anyone to use and modify the software...'),
('The MySQL Database Server is very fast, reliable, scalable, and easy to use', 'MySQL Server can run comfortably on a desktop or laptop...'),
('MySQL Server works in client/server or embedded systems', 'The MySQL Database Software is a client/server system...'),
('MyISAM', 'MyISAM is based on the older (and no longer available) ISAM storage engine but has many useful extensions'),
('A large amount of contributed MySQL software is available', 'MySQL Server has a practical set of features developed in close cooperation with our users'),
(NULL,NULL);
ANALYZE TABLE t1;
# No ranking
EXPLAIN
SELECT count(*) FROM t1 WHERE MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext ft_idx ft_idx 0 1 Using where
SELECT count(*) FROM t1 WHERE MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE);
count(*)
6
EXPLAIN
SELECT count(*) FROM t1 WHERE MATCH (title, text) AGAINST ('data*' IN BOOLEAN MODE) ORDER BY title LIMIT 3;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext ft_idx ft_idx 0 1 Using where
SELECT count(*) FROM t1 WHERE MATCH (title, text) AGAINST ('data*' IN BOOLEAN MODE) ORDER BY title LIMIT 3;
count(*)
6
EXPLAIN
SELECT FTS_DOC_ID, title FROM t1 WHERE MATCH(title, text) AGAINST ('+fast +database' IN BOOLEAN MODE);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext ft_idx ft_idx 0 1 Using where
SELECT FTS_DOC_ID, title FROM t1 WHERE MATCH(title, text) AGAINST ('+fast +database' IN BOOLEAN MODE);
FTS_DOC_ID title
11 The MySQL Database Server is very fast, reliable, scalable, and easy to use
EXPLAIN
SELECT FTS_DOC_ID, title FROM t1 WHERE MATCH(title, text) AGAINST ('+very +fast' WITH QUERY EXPANSION);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext ft_idx ft_idx 0 1 Using where
SELECT FTS_DOC_ID, title FROM t1 WHERE MATCH(title, text) AGAINST ('+very +fast' WITH QUERY EXPANSION);
FTS_DOC_ID title
11 The MySQL Database Server is very fast, reliable, scalable, and easy to use
12 MySQL Server works in client/server or embedded systems
10 MySQL software is Open Source
4 1001 MySQL Tricks
14 A large amount of contributed MySQL software is available
2 How To Use MySQL Well
13 MyISAM
5 MySQL vs. YourSQL database
8 MySQL is a database management system
1 MySQL Tutorial
9 MySQL databases are relational
6 MySQL Security
3 Optimizing MySQL
EXPLAIN
SELECT FTS_DOC_ID, title FROM t1 WHERE MATCH(title, text) AGAINST ('"very fast"@3' IN BOOLEAN MODE);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext ft_idx ft_idx 0 1 Using where
SELECT FTS_DOC_ID, title FROM t1 WHERE MATCH(title, text) AGAINST ('"very fast"@3' IN BOOLEAN MODE);
FTS_DOC_ID title
11 The MySQL Database Server is very fast, reliable, scalable, and easy to use
EXPLAIN SELECT FTS_DOC_ID FROM t1
WHERE MATCH(title, text) AGAINST ('+for' IN BOOLEAN MODE);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext ft_idx ft_idx 0 1 Using where
SELECT FTS_DOC_ID FROM t1
WHERE MATCH(title, text) AGAINST ('+for' IN BOOLEAN MODE);
FTS_DOC_ID
# No sorting by rank
EXPLAIN SELECT FTS_DOC_ID, TITLE FROM t1
WHERE MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE)
ORDER BY title;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext ft_idx ft_idx 0 1 Using where; Using filesort
SELECT FTS_DOC_ID, TITLE FROM t1
WHERE MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE)
ORDER BY title;
FTS_DOC_ID TITLE
9 MySQL databases are relational
8 MySQL is a database management system
12 MySQL Server works in client/server or embedded systems
1 MySQL Tutorial
5 MySQL vs. YourSQL database
11 The MySQL Database Server is very fast, reliable, scalable, and easy to use
EXPLAIN SELECT FTS_DOC_ID FROM t1
WHERE MATCH(title, text) AGAINST ('+fast +database' IN BOOLEAN MODE);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext ft_idx ft_idx 0 1 Using where
SELECT FTS_DOC_ID FROM t1
WHERE MATCH(title, text) AGAINST ('+fast +database' IN BOOLEAN MODE);
FTS_DOC_ID
11
EXPLAIN
SELECT FTS_DOC_ID, title FROM t1 WHERE MATCH(title, text) AGAINST ('+very +fast' WITH QUERY EXPANSION) ORDER BY title;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext ft_idx ft_idx 0 1 Using where; Using filesort
SELECT FTS_DOC_ID, title FROM t1 WHERE MATCH(title, text) AGAINST ('+very +fast' WITH QUERY EXPANSION) ORDER BY title;
FTS_DOC_ID title
4 1001 MySQL Tricks
14 A large amount of contributed MySQL software is available
2 How To Use MySQL Well
13 MyISAM
9 MySQL databases are relational
8 MySQL is a database management system
6 MySQL Security
12 MySQL Server works in client/server or embedded systems
10 MySQL software is Open Source
1 MySQL Tutorial
5 MySQL vs. YourSQL database
3 Optimizing MySQL
11 The MySQL Database Server is very fast, reliable, scalable, and easy to use
EXPLAIN
SELECT FTS_DOC_ID, title FROM t1 WHERE MATCH(title, text) AGAINST ('"very fast"@3' IN BOOLEAN MODE) ORDER BY title;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext ft_idx ft_idx 0 1 Using where; Using filesort
SELECT FTS_DOC_ID, title FROM t1 WHERE MATCH(title, text) AGAINST ('"very fast"@3' IN BOOLEAN MODE) ORDER BY title;
FTS_DOC_ID title
11 The MySQL Database Server is very fast, reliable, scalable, and easy to use
# LIMIT optimization
EXPLAIN SELECT FTS_DOC_ID, TITLE FROM t1
WHERE MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE)
LIMIT 3;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext ft_idx ft_idx 0 1 Using where
SELECT FTS_DOC_ID, TITLE FROM t1
WHERE MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE)
LIMIT 3;
FTS_DOC_ID TITLE
11 The MySQL Database Server is very fast, reliable, scalable, and easy to use
5 MySQL vs. YourSQL database
8 MySQL is a database management system
EXPLAIN SELECT FTS_DOC_ID FROM t1
WHERE MATCH(title, text) AGAINST ('+fast +database' IN BOOLEAN MODE)
LIMIT 3;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext ft_idx ft_idx 0 1 Using where
SELECT FTS_DOC_ID FROM t1
WHERE MATCH(title, text) AGAINST ('+fast +database' IN BOOLEAN MODE)
LIMIT 3;
FTS_DOC_ID
11
EXPLAIN SELECT FTS_DOC_ID FROM t1
WHERE MATCH(title, text) AGAINST ('+fast +database' IN BOOLEAN MODE)
ORDER BY title
LIMIT 3;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext ft_idx ft_idx 0 1 Using where; Using filesort
SELECT FTS_DOC_ID FROM t1
WHERE MATCH(title, text) AGAINST ('+fast +database' IN BOOLEAN MODE)
ORDER BY title
LIMIT 3;
FTS_DOC_ID
11
EXPLAIN
SELECT FTS_DOC_ID FROM t1 WHERE MATCH(title, text) AGAINST ('+very +fast' WITH QUERY EXPANSION) ORDER BY title LIMIT 1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext ft_idx ft_idx 0 1 Using where; Using filesort
SELECT FTS_DOC_ID FROM t1 WHERE MATCH(title, text) AGAINST ('+very +fast' WITH QUERY EXPANSION) ORDER BY title LIMIT 1;
FTS_DOC_ID
4
EXPLAIN
SELECT FTS_DOC_ID FROM t1 WHERE MATCH(title, text) AGAINST ('"very fast"@3' IN BOOLEAN MODE) ORDER BY title LIMIT 1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext ft_idx ft_idx 0 1 Using where; Using filesort
SELECT FTS_DOC_ID FROM t1 WHERE MATCH(title, text) AGAINST ('"very fast"@3' IN BOOLEAN MODE) ORDER BY title LIMIT 1;
FTS_DOC_ID
11
EXPLAIN
SELECT FTS_DOC_ID, MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE) as rank
FROM t1 WHERE MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE)
ORDER BY rank, FTS_DOC_ID
LIMIT 3;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext ft_idx ft_idx 0 1 Using where; Using temporary; Using filesort
SELECT FTS_DOC_ID, MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE) as rank
FROM t1 WHERE MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE)
ORDER BY rank, FTS_DOC_ID
LIMIT 3;
FTS_DOC_ID rank
1 0.15835624933242798
9 0.15835624933242798
12 0.15835624933242798
EXPLAIN
SELECT FTS_DOC_ID, MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE) as rank
FROM t1 WHERE MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE)
ORDER BY rank DESC, FTS_DOC_ID ASC
LIMIT 3;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext ft_idx ft_idx 0 1 Using where; Using temporary; Using filesort
SELECT FTS_DOC_ID, MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE) as rank
FROM t1 WHERE MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE)
ORDER BY rank DESC, FTS_DOC_ID ASC
LIMIT 3;
FTS_DOC_ID rank
11 1.5415468215942383
5 0.47506874799728394
8 0.31671249866485596
EXPLAIN SELECT FTS_DOC_ID, MATCH(title, text) AGAINST ('+database' IN BOOLEAN MODE) as rank
FROM t1
ORDER BY rank DESC
LIMIT 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 15 Using temporary; Using filesort
SELECT FTS_DOC_ID, MATCH(title, text) AGAINST ('+database' IN BOOLEAN MODE) as rank
FROM t1
ORDER BY rank DESC
LIMIT 2;
FTS_DOC_ID rank
5 0.47506874799728394
8 0.31671249866485596
EXPLAIN SELECT FTS_DOC_ID, MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE) as rank
FROM t1
ORDER BY rank DESC
LIMIT 3;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 15 Using temporary; Using filesort
SELECT FTS_DOC_ID, MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE) as rank
FROM t1
ORDER BY rank DESC
LIMIT 3;
FTS_DOC_ID rank
11 1.5415468215942383
5 0.47506874799728394
8 0.31671249866485596
EXPLAIN SELECT FTS_DOC_ID, MATCH(title, text) AGAINST ('+database' IN BOOLEAN MODE) as rank
FROM t1 WHERE MATCH(title, text) AGAINST ('+database' IN BOOLEAN MODE)
ORDER BY MATCH(title, text) AGAINST ('+database' IN BOOLEAN MODE) DESC,
FTS_DOC_ID ASC;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext ft_idx ft_idx 0 1 Using where; Using temporary; Using filesort
SELECT FTS_DOC_ID, MATCH(title, text) AGAINST ('+database' IN BOOLEAN MODE) as rank
FROM t1 WHERE MATCH(title, text) AGAINST ('+database' IN BOOLEAN MODE)
ORDER BY MATCH(title, text) AGAINST ('+database' IN BOOLEAN MODE) DESC,
FTS_DOC_ID ASC;
FTS_DOC_ID rank
5 0.47506874799728394
8 0.31671249866485596
1 0.15835624933242798
9 0.15835624933242798
11 0.15835624933242798
12 0.15835624933242798
EXPLAIN SELECT FTS_DOC_ID, MATCH(title, text) AGAINST ('+database' IN BOOLEAN MODE) as rank
FROM t1 WHERE MATCH(title, text) AGAINST ('+database' IN BOOLEAN MODE) and FTS_DOC_ID > 1
ORDER BY MATCH(title, text) AGAINST ('+database' IN BOOLEAN MODE) DESC
LIMIT 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext PRIMARY,FTS_DOC_ID_INDEX,ft_idx ft_idx 0 1 Using where; Using temporary; Using filesort
SELECT FTS_DOC_ID, MATCH(title, text) AGAINST ('+database' IN BOOLEAN MODE) as rank
FROM t1 WHERE MATCH(title, text) AGAINST ('+database' IN BOOLEAN MODE) and FTS_DOC_ID > 1
ORDER BY MATCH(title, text) AGAINST ('+database' IN BOOLEAN MODE) DESC
LIMIT 2;
FTS_DOC_ID rank
5 0.47506874799728394
8 0.31671249866485596
EXPLAIN
SELECT FTS_DOC_ID,MATCH(title, text) AGAINST ('+very +fast' WITH QUERY EXPANSION) as rank
FROM t1 WHERE MATCH(title, text) AGAINST ('+very +fast' WITH QUERY EXPANSION)
ORDER BY rank
LIMIT 1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext ft_idx ft_idx 0 1 Using where; Using temporary; Using filesort
SELECT FTS_DOC_ID,MATCH(title, text) AGAINST ('+very +fast' WITH QUERY EXPANSION) as rank
FROM t1 WHERE MATCH(title, text) AGAINST ('+very +fast' WITH QUERY EXPANSION)
ORDER BY rank
LIMIT 1;
FTS_DOC_ID rank
3 0.009391550906002522
EXPLAIN
SELECT FTS_DOC_ID,MATCH(title, text) AGAINST ('+very +fast' WITH QUERY EXPANSION) as rank
FROM t1 WHERE MATCH(title, text) AGAINST ('+very +fast' WITH QUERY EXPANSION)
ORDER BY rank DESC
LIMIT 1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext ft_idx ft_idx 0 1 Using where; Using temporary; Using filesort
SELECT FTS_DOC_ID,MATCH(title, text) AGAINST ('+very +fast' WITH QUERY EXPANSION) as rank
FROM t1 WHERE MATCH(title, text) AGAINST ('+very +fast' WITH QUERY EXPANSION)
ORDER BY rank DESC
LIMIT 1;
FTS_DOC_ID rank
11 15.345823287963867
EXPLAIN
SELECT FTS_DOC_ID,MATCH(title, text) AGAINST ('+very +fast' WITH QUERY EXPANSION) as rank
FROM t1 WHERE MATCH(title, text) AGAINST ('+very +fast' WITH QUERY EXPANSION)
ORDER BY MATCH(title, text) AGAINST ('+very +fast' WITH QUERY EXPANSION)
LIMIT 1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext ft_idx ft_idx 0 1 Using where; Using temporary; Using filesort
SELECT FTS_DOC_ID,MATCH(title, text) AGAINST ('+very +fast' WITH QUERY EXPANSION) as rank
FROM t1 WHERE MATCH(title, text) AGAINST ('+very +fast' WITH QUERY EXPANSION)
ORDER BY MATCH(title, text) AGAINST ('+very +fast' WITH QUERY EXPANSION) DESC
LIMIT 1;
FTS_DOC_ID rank
11 15.345823287963867
# WHERE optimization on MATCH > 'some_rank'
EXPLAIN SELECT FTS_DOC_ID FROM t1
WHERE MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE) > 0.1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext ft_idx ft_idx 0 1 Using where
SELECT FTS_DOC_ID FROM t1
WHERE MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE) > 0.1;
FTS_DOC_ID
11
5
8
1
9
12
# additional test for correct behaviour
EXPLAIN SELECT * FROM t1 ORDER BY MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE) DESC LIMIT 10;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 15 Using temporary; Using filesort
SELECT FTS_DOC_ID FROM t1
WHERE MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE) AND
MATCH (title, text) AGAINST ('mysql' IN NATURAL LANGUAGE MODE)
LIMIT 6;
FTS_DOC_ID
11
5
8
1
9
12
# test OR condition
SELECT FTS_DOC_ID
FROM t1
WHERE MATCH(title, text) AGAINST ('database')
OR MATCH(title, text) AGAINST ('mysql')
ORDER BY MATCH(title, text) AGAINST ('database') DESC, FTS_DOC_ID ASC;
FTS_DOC_ID
5
8
1
9
11
12
2
3
4
6
10
14
EXPLAIN SELECT FTS_DOC_ID
FROM t1
WHERE MATCH(title, text) AGAINST ('database')
OR MATCH(title, text) AGAINST ('mysql')
ORDER BY MATCH(title, text) AGAINST ('database') DESC, FTS_DOC_ID ASC;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 15 Using where; Using temporary; Using filesort
# MATCH and GROUP BY, DISTINCT
SET sql_mode = (select replace(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
EXPLAIN SELECT FTS_DOC_ID FROM t1
WHERE MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE)
GROUP BY FTS_DOC_ID
ORDER BY MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE) DESC
LIMIT 3;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext ft_idx ft_idx 0 1 Using where; Using temporary; Using filesort
SELECT FTS_DOC_ID FROM t1
WHERE MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE)
GROUP BY FTS_DOC_ID
ORDER BY MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE) DESC
LIMIT 3;
FTS_DOC_ID
11
5
8
EXPLAIN SELECT FTS_DOC_ID FROM t1
WHERE MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE)
GROUP BY title
ORDER BY MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE) DESC
LIMIT 3;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext ft_idx ft_idx 0 1 Using where; Using temporary; Using filesort
SELECT FTS_DOC_ID FROM t1
WHERE MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE)
GROUP BY title
ORDER BY MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE) DESC
LIMIT 3;
FTS_DOC_ID
11
5
8
EXPLAIN SELECT MAX(FTS_DOC_ID) FROM t1
WHERE MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE)
ORDER BY MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE) DESC
LIMIT 3;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext ft_idx ft_idx 0 1 Using where
SELECT MAX(FTS_DOC_ID) FROM t1
WHERE MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE)
ORDER BY MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE) DESC
LIMIT 3;
MAX(FTS_DOC_ID)
12
EXPLAIN SELECT DISTINCT(title) FROM t1
WHERE MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE)
ORDER BY MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE) DESC
LIMIT 3;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext ft_idx ft_idx 0 1 Using where; Using temporary; Using filesort
SELECT DISTINCT(title) FROM t1
WHERE MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE)
ORDER BY MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE) DESC
LIMIT 3;
title
The MySQL Database Server is very fast, reliable, scalable, and easy to use
MySQL vs. YourSQL database
MySQL is a database management system
EXPLAIN SELECT DISTINCT(FTS_DOC_ID) FROM t1
WHERE MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE)
ORDER BY MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE) DESC
LIMIT 3;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext ft_idx ft_idx 0 1 Using where; Using temporary; Using filesort
SELECT DISTINCT(FTS_DOC_ID) FROM t1
WHERE MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE)
ORDER BY MATCH (title, text) AGAINST ('fast database' IN NATURAL LANGUAGE MODE) DESC
LIMIT 3;
FTS_DOC_ID
11
5
8
SET sql_mode = @save_mode;
# FTS index access
SELECT FTS_DOC_ID, MATCH(title, text) AGAINST ('+database' IN BOOLEAN MODE) as rank
FROM t1
ORDER BY rank DESC
LIMIT 2;
FTS_DOC_ID rank
5 0.47506874799728394
8 0.31671249866485596
EXPLAIN SELECT FTS_DOC_ID, MATCH(title, text) AGAINST ('+database' IN BOOLEAN MODE) as rank
FROM t1
ORDER BY rank DESC
LIMIT 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 15 Using temporary; Using filesort
SELECT a.FTS_DOC_ID, b.FTS_DOC_ID
FROM t1 a, t1 b
WHERE MATCH(a.title, a.text) AGAINST ('+database' IN BOOLEAN MODE) and
MATCH(b.title, b.text) AGAINST ('+mysql' IN BOOLEAN MODE) and
a.FTS_DOC_ID = b.FTS_DOC_ID;
FTS_DOC_ID FTS_DOC_ID
5 5
8 8
1 1
9 9
11 11
12 12
EXPLAIN SELECT a.FTS_DOC_ID, b.FTS_DOC_ID
FROM t1 a, t1 b
WHERE MATCH(a.title, a.text) AGAINST ('+database' IN BOOLEAN MODE) and
MATCH(b.title, b.text) AGAINST ('+mysql' IN BOOLEAN MODE) and
a.FTS_DOC_ID = b.FTS_DOC_ID;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE a fulltext PRIMARY,FTS_DOC_ID_INDEX,ft_idx ft_idx 0 1 Using where
1 SIMPLE b fulltext PRIMARY,FTS_DOC_ID_INDEX,ft_idx ft_idx 0 1 Using where
SELECT a.FTS_DOC_ID, MATCH(a.title, a.text) AGAINST ('+database' IN BOOLEAN MODE),
b.FTS_DOC_ID, MATCH(b.title, b.text) AGAINST ('+database' IN BOOLEAN MODE)
FROM t1 a, t1 b
WHERE MATCH(a.title, a.text) AGAINST ('+database' IN BOOLEAN MODE) and
MATCH(b.title, b.text) AGAINST ('+database' IN BOOLEAN MODE);
FTS_DOC_ID MATCH(a.title, a.text) AGAINST ('+database' IN BOOLEAN MODE) FTS_DOC_ID MATCH(b.title, b.text) AGAINST ('+database' IN BOOLEAN MODE)
5 0.47506874799728394 5 0.47506874799728394
5 0.47506874799728394 8 0.31671249866485596
5 0.47506874799728394 1 0.15835624933242798
5 0.47506874799728394 9 0.15835624933242798
5 0.47506874799728394 11 0.15835624933242798
5 0.47506874799728394 12 0.15835624933242798
8 0.31671249866485596 5 0.47506874799728394
8 0.31671249866485596 8 0.31671249866485596
8 0.31671249866485596 1 0.15835624933242798
8 0.31671249866485596 9 0.15835624933242798
8 0.31671249866485596 11 0.15835624933242798
8 0.31671249866485596 12 0.15835624933242798
1 0.15835624933242798 5 0.47506874799728394
1 0.15835624933242798 8 0.31671249866485596
1 0.15835624933242798 1 0.15835624933242798
1 0.15835624933242798 9 0.15835624933242798
1 0.15835624933242798 11 0.15835624933242798
1 0.15835624933242798 12 0.15835624933242798
9 0.15835624933242798 5 0.47506874799728394
9 0.15835624933242798 8 0.31671249866485596
9 0.15835624933242798 1 0.15835624933242798
9 0.15835624933242798 9 0.15835624933242798
9 0.15835624933242798 11 0.15835624933242798
9 0.15835624933242798 12 0.15835624933242798
11 0.15835624933242798 5 0.47506874799728394
11 0.15835624933242798 8 0.31671249866485596
11 0.15835624933242798 1 0.15835624933242798
11 0.15835624933242798 9 0.15835624933242798
11 0.15835624933242798 11 0.15835624933242798
11 0.15835624933242798 12 0.15835624933242798
12 0.15835624933242798 5 0.47506874799728394
12 0.15835624933242798 8 0.31671249866485596
12 0.15835624933242798 1 0.15835624933242798
12 0.15835624933242798 9 0.15835624933242798
12 0.15835624933242798 11 0.15835624933242798
12 0.15835624933242798 12 0.15835624933242798
EXPLAIN SELECT a.FTS_DOC_ID, MATCH(a.title, a.text) AGAINST ('+database' IN BOOLEAN MODE),
b.FTS_DOC_ID, MATCH(b.title, b.text) AGAINST ('+database' IN BOOLEAN MODE)
FROM t1 a, t1 b
WHERE MATCH(a.title, a.text) AGAINST ('+database' IN BOOLEAN MODE) and
MATCH(b.title, b.text) AGAINST ('+database' IN BOOLEAN MODE);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE a fulltext ft_idx ft_idx 0 1 Using where
1 SIMPLE b fulltext ft_idx ft_idx 0 1 Using where
EXPLAIN SELECT FTS_DOC_ID FROM t1 WHERE MATCH(title, text) AGAINST ("data*" IN BOOLEAN MODE) * 100;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 15 Using where
SELECT FTS_DOC_ID, MATCH(title, text) AGAINST ("data*" IN BOOLEAN MODE) * 100
FROM t1 WHERE MATCH(title, text) AGAINST ("data*" IN BOOLEAN MODE) * 100;
FTS_DOC_ID MATCH(title, text) AGAINST ("data*" IN BOOLEAN MODE) * 100
1 4.92168664932251
5 14.76505994796753
8 9.84337329864502
9 4.92168664932251
11 4.92168664932251
12 4.92168664932251
SELECT * FROM t1 WHERE title IS NULL AND text IS NULL;
FTS_DOC_ID title text
15 NULL NULL
CREATE TABLE t2 SELECT FTS_DOC_ID as doc_id, title, text FROM t1;
ALTER TABLE t2 ADD PRIMARY KEY (doc_id);
ALTER TABLE t2 ADD FULLTEXT KEY ft_idx (title,text);
ANALYZE TABLE t2;
EXPLAIN SELECT DOC_ID FROM t2 WHERE MATCH(title, text) AGAINST ('+database' IN BOOLEAN MODE) * 100;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 15 Using where
SELECT DOC_ID FROM t2 WHERE MATCH(title, text) AGAINST ('+database' IN BOOLEAN MODE) * 100;
DOC_ID
1
5
8
9
11
12
EXPLAIN SELECT FTS_DOC_ID FROM t1 WHERE MATCH(title, text) AGAINST ('+database' IN BOOLEAN MODE) * 100;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 15 Using where
SELECT FTS_DOC_ID FROM t1 WHERE MATCH(title, text) AGAINST ('+database' IN BOOLEAN MODE) * 100;
FTS_DOC_ID
1
5
8
9
11
12
DROP TABLE t1, t2;
"Check hints with uft8 charset for 2 cases"
set names utf8;
CREATE TABLE t1 (
FTS_DOC_ID BIGINT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
title VARCHAR(200),
text TEXT
) CHARACTER SET = utf8, ENGINE=InnoDB;
INSERT INTO t1 (title, text) VALUES
('Я могу есть стекло', 'оно мне не вредит'),
('Мога да ям стъкло', 'то не ми вреди'),
('Μπορῶ νὰ φάω σπασμένα' ,'γυαλιὰ χωρὶς νὰ πάθω τίποτα'),
('Příliš žluťoučký kůň', 'úpěl ďábelské kódy'),
('Sævör grét', 'áðan því úlpan var ónýt'),
('うゐのおくやま','けふこえて'),
('いろはにほへど ちりぬる','あさきゆめみじ ゑひもせず');
CREATE FULLTEXT INDEX idx on t1 (title, text);
# No ranking
EXPLAIN
SELECT count(*) FROM t1 WHERE MATCH (title, text) AGAINST ('вредит' IN NATURAL LANGUAGE MODE);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext idx idx 0 1 Using where
SELECT count(*) FROM t1 WHERE MATCH (title, text) AGAINST ('вредит' IN NATURAL LANGUAGE MODE);
count(*)
1
EXPLAIN
SELECT * FROM t1 WHERE MATCH(title, text) AGAINST ("оно" WITH QUERY EXPANSION);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext idx idx 0 1 Using where
SELECT * FROM t1 WHERE MATCH(title, text) AGAINST ("оно" WITH QUERY EXPANSION);
FTS_DOC_ID title text
1 Я могу есть стекло оно мне не вредит
# No sorting by rank
EXPLAIN SELECT FTS_DOC_ID FROM t1
WHERE MATCH(title, text) AGAINST ('+(Мога τίποτα)' IN BOOLEAN MODE);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext idx idx 0 1 Using where
SELECT FTS_DOC_ID FROM t1
WHERE MATCH(title, text) AGAINST ('+(Мога τίποτα)' IN BOOLEAN MODE);
FTS_DOC_ID
2
3
DROP TABLE t1;
#
# Bug #18924341 CRASH IN TEST_IF_SKIP_SORT_ORDER, GROUP BY MATCH AGAINST DESC
#
CREATE TABLE t1 (f1 CHAR(1), FULLTEXT KEY (f1));
SELECT 1 FROM t1 NATURAL JOIN t1 a GROUP BY MATCH(t1.f1) AGAINST ("1") DESC;
1
DROP TABLE t1;
#
# Bug#20261601 ASSERTION FAILED: !FIRST_QEP_TAB->TABLE()->NO_KEYREAD
#
CREATE TABLE t1(a INT PRIMARY KEY);
INSERT INTO t1 VALUES(1),(2);
SELECT (SELECT MATCH(`a`)AGAINST('1') FROM t1) FROM t1;
ERROR HY000: Can't find FULLTEXT index matching the column list
SELECT 1, a IN (SELECT a FROM t1) FROM t1;
1 a IN (SELECT a FROM t1)
1 1
1 1
DROP TABLE t1;
#
# Bug#20442572 ASSERTION `!FIRST_QEP_TAB->TABLE()->NO_KEYREAD' FAILED.
# Bug#75688 Assertion `!first_qep_tab->table()->no_keyread' failed.
#
CREATE TABLE t1(a INT,b POINT NOT NULL,KEY(a));
HANDLER t1 OPEN;
select * from t1 where MATCH a,b AGAINST('"Now sUPPort"' IN BOOLEAN MODE);
a b
prepare stmt1 from "truncate t1";
SELECT a IN(SELECT a FROM t1)FROM t1;
a IN(SELECT a FROM t1)
deallocate prepare stmt1;
DROP TABLE t1;
#
# Bug #20685427 INVALID WRITE OF FREED MEMORY IN ITEM_FUNC_MATCH::CLEANUP
#
CREATE TABLE t1(a TEXT CHARSET LATIN1, FULLTEXT KEY(a)) ENGINE=INNODB;
SELECT MATCH(a) AGAINST ('') FROM (SELECT a FROM t1 LIMIT 1) q;
ERROR HY000: Can't find FULLTEXT index matching the column list
DROP TABLE t1;
#
# Bug#21140067 EXPLAIN .. MATCH AGAINST: ASSERTION FAILED: TO <= END
#
CREATE TABLE t1(f1 CHAR(1) CHARSET latin1, FULLTEXT(f1)) ENGINE=INNODB;
EXPLAIN SELECT 1 FROM t1 WHERE 1.238585e+308 <= MATCH(f1) AGAINST ('1' IN BOOLEAN MODE);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 fulltext f1 f1 0 1 Using where
EXPLAIN FORMAT = JSON SELECT 1 FROM t1 WHERE 1.238585e+308 <= MATCH(f1) AGAINST ('1' IN BOOLEAN MODE);
EXPLAIN
{
"query_block": {
"select_id": 1,
"cost": 0.00009287,
"nested_loop": [
{
"table": {
"table_name": "t1",
"access_type": "fulltext",
"possible_keys": ["f1"],
"key": "f1",
"key_length": "0",
"used_key_parts": ["f1"],
"loops": 1,
"rows": 1,
"cost": 0.00009287,
"filtered": 100,
"attached_condition": "1.238585e+308 <= (match t1.f1 against ('1' in boolean mode))"
}
}
]
}
}
DROP TABLE t1;
#
# Bug#21140088 MATCH AGAINST: ASSERTION FAILED: !TABLE || (!TABLE->READ_SET || BITMAP_IS_SET
#
SET sql_mode='';
CREATE TABLE t1(a INT) ENGINE=INNODB;
CREATE TABLE t2(b TEXT CHARSET LATIN1, FULLTEXT(b), PRIMARY KEY(b(10))) ENGINE=INNODB;
INSERT INTO t2 VALUES ('a'),('b');
SELECT NOT EXISTS (SELECT MATCH(b) AGAINST ('1') FROM t1) FROM t2 GROUP BY "a";
ERROR HY000: Incorrect arguments to MATCH
DROP TABLE t1, t2;
CREATE TABLE t1(a INT) ENGINE=MyISAM;
CREATE TABLE t2(b TEXT CHARSET LATIN1, FULLTEXT(b), PRIMARY KEY(b(10))) ENGINE=MyISAM;
INSERT INTO t2 VALUES ('a'),('b');
SELECT NOT EXISTS (SELECT MATCH(b) AGAINST ('1' in BOOLEAN MODE) FROM t1) FROM t2 GROUP BY "a";
NOT EXISTS (SELECT MATCH(b) AGAINST ('1' in BOOLEAN MODE) FROM t1)
1
DROP TABLE t1, t2;
SET sql_mode=default;
#
# Bug#21140039 ASSERTION FAILED: !FIRST_QEP_TAB->TABLE()->NO_KEYREAD MATCH AGAINST.....
#
CREATE TABLE t1
(
a INT,
b INT,
c CHAR(1) CHARSET latin1,
PRIMARY KEY (b,a),
FULLTEXT KEY (c)
) ENGINE=INNODB;
SELECT "a" NOT IN(SELECT b FROM t1 WHERE MATCH(c) AGAINST ('a' IN BOOLEAN MODE));
"a" NOT IN(SELECT b FROM t1 WHERE MATCH(c) AGAINST ('a' IN BOOLEAN MODE))
1
DROP TABLE t1;
#
# Bug#21300774 ASSERT `!INIT_FTFUNCS(THD, SELECT_LEX)` IN JOIN::RESET AT SQL/SQL_SELECT.CC:874
#
CREATE TABLE t1 (f1 INT);
INSERT INTO t1 VALUES (1);
INSERT INTO t1 VALUES (2);
CREATE TABLE t2 (ft TEXT, FULLTEXT KEY ft(ft));
INSERT INTO t2 VALUES ('abc');
INSERT INTO t2 VALUES ('def');
UPDATE t1 SET f1 =
(SELECT t1.f1 FROM t2 WHERE NOT TRUE AND
MATCH (ft) AGAINST ((SELECT 'xyz' FROM t2)));
ERROR 21000: Subquery returns more than 1 row
DROP TABLE t1, t2;
#
# Bug#22679209: FULL-TEXT QUERIES WITH ADDITIONAL SECONDARY INDEX
# GIVES NULL OR ZERO ROWS
#
CREATE TABLE t1 (
f1 INTEGER,
title varchar(255),
body mediumtext,
KEY f1 (f1),
FULLTEXT KEY title (title),
FULLTEXT KEY body (body)
) ENGINE=InnoDB;
INSERT INTO t1 VALUES
(1, 'Insert into table', 'insert into table select from'),
(1, 'Delete from table', 'insert into table select from'),
(1, 'Update', 'perform update'),
(2, 'Insert into table', 'insert into table select from'),
( 2, 'Delete from table', 'some body text here'),
( 2, 'Update', 'perform update'),
( 3, 'Insert into table', 'insert into table select from'),
( 3, 'Delete from table', 'some body text here');
SELECT f1 FROM t1 WHERE f1=1 AND
(MATCH (title) AGAINST ('table' IN BOOLEAN MODE) OR
MATCH (body) AGAINST ('table' IN BOOLEAN MODE));
f1
1
1
DROP TABLE t1;
# End of test for Bug#22679209
|