DROP TABLE IF EXISTS attributes; CREATE TABLE attributes ( entity_id INT UNSIGNED NOT NULL, name_id INT UNSIGNED NOT NULL, value_hash INT UNSIGNED NOT NULL, id BIGINT UNSIGNED NOT NULL, value_prefix VARBINARY(256) NOT NULL DEFAULT '', PRIMARY KEY (entity_id,name_id,value_hash,id) ) ENGINE = InnoDB; CREATE INDEX query_index ON attributes (name_id,value_prefix,value_hash); insert attributes(entity_id, name_id, value_hash, id, value_prefix) values (10, 1, 857172247, 46, 'Acura'), (9, 1, 1620526073, 42, 'Acura'), (8, 1, 2011977658, 37, 'Acura'), (5, 1, 2258874605, 21, 'Acura'), (4, 1, 2446894254, 16, 'Acura'), (7, 1, 2824181867, 31, 'Acura'), (6, 1, 3207498792, 26, 'Acura'), (1, 1, 3679445473, 2, 'Acura'), (2, 1, 3804421412, 7, 'Acura'), (3, 1, 4122593639, 12, 'Acura'), (27, 1, 118379965, 131, 'Ford'), (20, 1, 231431332, 96, 'Ford'), (29, 1, 310658959, 142, 'Ford'), (30, 1, 447760615, 146, 'Ford'), (19, 1, 722041674, 91, 'Ford'), (17, 1, 1048799608, 81, 'Ford'), (25, 1, 1160467136, 122, 'Ford'), (22, 1, 1341021145, 106, 'Ford'), (12, 1, 1986225948, 57, 'Ford'), (15, 1, 2091281925, 72, 'Ford'), (23, 1, 2202295111, 112, 'Ford'), (24, 1, 2306953822, 116, 'Ford'), (14, 1, 2953604763, 67, 'Ford'), (13, 1, 3133761410, 62, 'Ford'), (21, 1, 3244383290, 102, 'Ford'), (26, 1, 3416544547, 126, 'Ford'), (28, 1, 3727574801, 136, 'Ford'), (18, 1, 3886250964, 87, 'Ford'), (16, 1, 4062800358, 77, 'Ford'), (11, 1, 4176240895, 52, 'Ford'), (48, 1, 634070212, 209, 'Honda'), (49, 1, 850407559, 213, 'Honda'), (50, 1, 1909583330, 217, 'Honda'), (41, 1, 2305331871, 181, 'Honda'), (43, 1, 2812197401, 189, 'Honda'), (42, 1, 2967842394, 185, 'Honda'), (44, 1, 3288324048, 193, 'Honda'), (45, 1, 3565470611, 197, 'Honda'), (46, 1, 3976855382, 201, 'Honda'), (47, 1, 4201835285, 205, 'Honda'), (8, 1, 265388250, 36, 'Mazda'), (9, 1, 413832345, 41, 'Mazda'), (10, 1, 1261092471, 47, 'Mazda'), (3, 1, 2374245895, 11, 'Mazda'), (2, 1, 2600405572, 6, 'Mazda'), (1, 1, 2742171265, 1, 'Mazda'), (6, 1, 3339934536, 27, 'Mazda'), (7, 1, 3496496907, 32, 'Mazda'), (4, 1, 3924128718, 17, 'Mazda'), (5, 1, 4271794061, 22, 'Mazda'), (29, 1, 126175092, 141, 'Subaru'), (23, 1, 279435197, 111, 'Subaru'), (21, 1, 340920192, 101, 'Subaru'), (27, 1, 424441799, 132, 'Subaru'), (25, 1, 498706426, 121, 'Subaru'), (30, 1, 975090558, 147, 'Subaru'), (28, 1, 3896978506, 137, 'Subaru'), (24, 1, 4068199620, 117, 'Subaru'), (26, 1, 4136508665, 127, 'Subaru'), (22, 1, 4284857475, 107, 'Subaru'), (13, 1, 45076740, 61, 'Volvo'), (12, 1, 366265671, 56, 'Volvo'), (11, 1, 744094082, 51, 'Volvo'), (16, 1, 1211667531, 76, 'Volvo'), (17, 1, 1598259208, 82, 'Volvo'), (14, 1, 1724824781, 66, 'Volvo'), (15, 1, 1907730574, 71, 'Volvo'), (18, 1, 2163888089, 86, 'Volvo'), (19, 1, 2541837210, 92, 'Volvo'), (20, 1, 3491059394, 97, 'Volvo'), (10, 2, 319194899, 48, 'Black'), (34, 2, 1115791016, 160, 'Black'), (3, 2, 1137006735, 13, 'Black'), (36, 2, 1182027413, 166, 'Black'), (1, 2, 1194363058, 3, 'Black'), (7, 2, 1244592373, 33, 'Black'), (5, 2, 1322985672, 23, 'Black'), (32, 2, 1335881455, 154, 'Black'), (9, 2, 1424476230, 43, 'Black'), (38, 2, 1488813606, 172, 'Black'), (40, 2, 1649486673, 178, 'Black'), (33, 2, 2690483665, 157, 'Black'), (4, 2, 2702787574, 18, 'Black'), (31, 2, 2762495468, 151, 'Black'), (6, 2, 2783744971, 28, 'Black'), (37, 2, 2847286699, 169, 'Black'), (2, 2, 2886153137, 8, 'Black'), (35, 2, 2906912150, 163, 'Black'), (39, 2, 3078559000, 175, 'Black'), (8, 2, 3139806072, 38, 'Black'), (26, 2, 1667677993, 128, 'Brown'), (24, 2, 1737715476, 118, 'Brown'), (22, 2, 1787633491, 108, 'Brown'), (28, 2, 2108657562, 138, 'Brown'), (21, 2, 2176492624, 103, 'Brown'), (23, 2, 2236576877, 113, 'Brown'), (25, 2, 2287010858, 123, 'Brown'), (27, 2, 2359612439, 133, 'Brown'), (29, 2, 2456675492, 143, 'Brown'), (30, 2, 2952138926, 148, 'Brown'), (43, 2, 45711586, 190, 'Grey'), (42, 2, 365073569, 186, 'Grey'), (41, 2, 743417956, 182, 'Grey'), (20, 2, 908774496, 98, 'Grey'), (46, 2, 1211024813, 202, 'Grey'), (47, 2, 1599459822, 206, 'Grey'), (18, 2, 1724241275, 88, 'Grey'), (44, 2, 1725492523, 194, 'Grey'), (45, 2, 1906571624, 198, 'Grey'), (19, 2, 1908326712, 93, 'Grey'), (48, 2, 2162957887, 210, 'Grey'), (14, 2, 2163287663, 68, 'Grey'), (15, 2, 2542416428, 73, 'Grey'), (49, 2, 2543258236, 214, 'Grey'), (16, 2, 2919698153, 78, 'Grey'), (17, 2, 3111918250, 83, 'Grey'), (11, 2, 3395707680, 53, 'Grey'), (50, 2, 3572946713, 218, 'Grey'), (13, 2, 3834661798, 63, 'Grey'), (12, 2, 4092288997, 58, 'Grey'), (48, 3, 385512906, 211, 33), (46, 3, 753931450, 203, 33), (41, 3, 838149122, 183, 33), (50, 3, 1558117395, 219, 33), (44, 3, 1631106481, 195, 33), (43, 3, 2084406537, 191, 33), (45, 3, 2858706452, 199, 33), (42, 3, 3076609708, 187, 33), (49, 3, 3718686319, 215, 33), (47, 3, 3886860063, 207, 33), (27, 3, 437806982, 134, 34), (29, 3, 538052342, 144, 34), (22, 3, 1255536181, 109, 34), (25, 3, 1473300109, 124, 34), (23, 3, 2173248912, 114, 34), (24, 3, 2626426152, 119, 34), (30, 3, 2708102282, 149, 34), (21, 3, 3426845851, 104, 34), (26, 3, 3510940707, 129, 34), (28, 3, 3947812179, 139, 34), (5, 3, 621925818, 24, 35), (2, 3, 940893442, 9, 35), (10, 3, 1106410805, 49, 35), (9, 3, 1389565377, 44, 35), (7, 3, 1759081649, 34, 35), (8, 3, 2576336484, 39, 35), (6, 3, 2743445268, 29, 35), (1, 3, 3196098476, 4, 35), (4, 3, 3998025247, 19, 35), (3, 3, 4081596071, 14, 35), (15, 3, 773099214, 74, 40), (12, 3, 856800886, 59, 40), (20, 3, 1328612832, 99, 40), (19, 3, 1507217077, 94, 40), (17, 3, 1675374533, 84, 40), (18, 3, 2458557712, 89, 40), (16, 3, 2827025504, 79, 40), (11, 3, 3045444824, 54, 40), (14, 3, 3846724971, 69, 40), (13, 3, 4165823955, 64, 40), (37, 3, 71019160, 170, 56), (39, 3, 1043450856, 176, 56), (35, 3, 1240664979, 164, 56), (32, 3, 1425423147, 155, 56), (34, 3, 2192562230, 161, 56), (33, 3, 2678777998, 158, 56), (36, 3, 3479664957, 167, 56), (31, 3, 3529656709, 152, 56), (40, 3, 3701056686, 179, 56), (38, 3, 4117567565, 173, 56), (23, 4, 590438055, 115, 'Ale'), (24, 4, 703879102, 120, 'Ale'), (21, 4, 1628723674, 105, 'Ale'), (26, 4, 1808879811, 130, 'Ale'), (28, 4, 2119910129, 140, 'Ale'), (27, 4, 2809876573, 135, 'Ale'), (29, 4, 3002155631, 145, 'Ale'), (30, 4, 3133492487, 150, 'Ale'), (25, 4, 3848165152, 125, 'Ale'), (22, 4, 4019936825, 110, 'Ale'), (14, 4, 580920839, 70, 'Claudio'), (18, 4, 665480070, 90, 'Claudio'), (17, 4, 1430189303, 85, 'Claudio'), (13, 4, 1446563720, 65, 'Claudio'), (20, 4, 2624352694, 100, 'Claudio'), (15, 4, 3104139880, 75, 'Claudio'), (11, 4, 3120779543, 55, 'Claudio'), (19, 4, 3155109865, 95, 'Claudio'), (12, 4, 3449652199, 60, 'Claudio'), (16, 4, 3466311832, 80, 'Claudio'), (42, 4, 226769654, 188, 'Luca'), (43, 4, 452931253, 192, 'Luca'), (41, 4, 873006643, 184, 'Luca'), (47, 4, 1192460217, 208, 'Luca'), (46, 4, 1349020666, 204, 'Luca'), (45, 4, 1776637759, 200, 'Luca'), (44, 4, 2124301180, 196, 'Luca'), (49, 4, 2412880939, 216, 'Luca'), (48, 4, 2561323112, 212, 'Luca'), (50, 4, 3434117454, 220, 'Luca'), (38, 4, 1151522168, 174, 'Paola'), (32, 4, 1400915377, 156, 'Paola'), (36, 4, 1517020619, 168, 'Paola'), (34, 4, 1587426806, 162, 'Paola'), (40, 4, 2119073807, 180, 'Paola'), (39, 4, 2875244102, 177, 'Paola'), (35, 4, 2975620808, 165, 'Paola'), (37, 4, 3047788277, 171, 'Paola'), (31, 4, 3099066034, 153, 'Paola'), (33, 4, 3158453903, 159, 'Paola'), (6, 4, 73410141, 30, 'Stefano'), (2, 4, 124132642, 10, 'Stefano'), (10, 4, 1465584605, 50, 'Stefano'), (1, 4, 1895354322, 5, 'Stefano'), (5, 4, 1946088621, 25, 'Stefano'), (9, 4, 1995749676, 45, 'Stefano'), (3, 4, 2630046029, 15, 'Stefano'), (7, 4, 2680515122, 35, 'Stefano'), (4, 4, 3898325186, 20, 'Stefano'), (8, 4, 3981571395, 40, 'Stefano'); select at0.value_prefix, at0.value_hash from attributes at0 force index (query_index), attributes at1 force index (query_index) where at0.entity_id = at1.entity_id AND ((at0.name_id = 1 AND ((at0.value_prefix = 'Ford') OR (at0.value_prefix = 'Mazda'))) OR (at0.name_id = 2 AND (at0.value_prefix = 'Grey'))) AND (at1.name_id = 3 AND (at1.value_prefix < '40')) AND ((at0.value_prefix = 'Ford' AND at0.value_hash > 3727574801) OR (at0.value_prefix > 'Ford' and at0.value_hash >= 0)) ORDER BY at0.value_prefix,at0.value_hash LIMIT 20; // >= 33 is ok