I'm wondering what the most effective way to do this MySQL statement that looks like the following
SELECT id FROM something WHERE x IN
(58, 285, 287, 1928, 175, 1928, 2, 481, 2851, 2875, 21, 498, 582, 198, 18)
AND y IN (57, 28, 29, 85, 101, 587, 201, 598, 982, 105, 78, 92, 100, 200, 909,
1120, 592, 109, 581, 2752, 581, 201, 510 700);
There is a composite index on [x, y]
Seems like there's a more effective way to do this, but I'm not sure.
Using IN() with 100 integers in each list isn't too much to ask of a query particularly if x & y are appropriately indexed. Any advantage in persisting those lists to tables may not be worth the effort unless they were stable or infrequently changed (and that different users don't need lists unique to each causing concurrency issues).
One thing to be aware of that those 2 independent lists are not evaluated as tuples as the following attempts to demonstrate:
MySQL 5.6 Schema Setup :
CREATE TABLE something
(`x` int, `y` int)
;
INSERT INTO something
(`x`, `y`)
VALUES
(58, 57),
(58, 28),
(58, 29),
(58, 30),
(58, 31)
;
Query 1 :
SELECT *
FROM `something`
WHERE x IN (58)
AND y IN (57, 28, 29)
Results :
| x | y |
|----|----|
| 58 | 57 |
| 58 | 28 |
| 58 | 29 |
Query 2 :
SELECT *
FROM `something`
WHERE (x,y) IN ((58,28),(58,29))
Results :
| x | y |
|----|----|
| 58 | 28 |
| 58 | 29 |
Perhaps not the best of examples but hopefully useful.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.