简体   繁体   English

如何使以下与mySQL的交互更加有效?

[英]How do I make the following interaction with mySQL more efficient?

I've got an array that contains combinations of unique MySql IDs: 我有一个包含唯一MySql ID组合的数组:

For example: 例如:

[
 [1,10,11],
 [2,10],
 [3,10,12],
 [3,12,13,20],
 [4,12]
]

In total there are a couple hundred different combinations of IDs. 总共有数百种ID的不同组合。

Some of these combinations are "valid" and some are not. 这些组合中的某些是“有效的”,而某些则不是。 For example, [1,10,11] may be a valid combination, whereas [3,10,12] may be invalid. 例如,[1,10,11]可能是有效组合,而[3,10,12]可能无效。

Combinations are valid or invalid depending on how the data is arranged in the database. 组合是否有效取决于数据库中数据的排列方式。

Currently I am using a SELECT statement to determine whether or not a specific combination of IDs is valid. 目前,我正在使用SELECT语句来确定ID的特定组合是否有效。 It looks something like this: 看起来像这样:

SELECT id1 
FROM table
WHERE id2 IN ($combination)
GROUP BY id1
HAVING COUNT(distinct id2) = $number

...where $combination is one possible combination of IDs (eg 1,10,11) and $number is the number of IDs in that combination (in this case, 3). ...其中$ combination是ID的一种可能组合(例如1,10,11),而$ number是该组合中ID的数目(在这种情况下为3)。 An invalid combination will return 0 rows. 无效的组合将返回0行。 A valid combination will return 1 or more rows. 有效组合将返回1或更多行。

However, to solve the entire set of possible combinations means looping a couple hundred SELECT statements, which I would rather not be doing. 但是,要解决整个可能的组合,意味着要循环数百条SELECT语句,而我宁愿不这样做。

I am wondering: Are there any tricks for making this more efficient? 我想知道:是否有任何技巧可以提高效率? Is it possible to submit the entire dataset to mySQL in one go, and have mySQL iterate through it? 是否可以一次性将整个数据集提交给mySQL,并使其遍历mySQL? Any suggestions would be much appreciated. 任何建议将不胜感激。

Thanks in advance! 提前致谢!

If I'm following your exaplantaion correctly, then.... 如果我正确地追踪了您的遗体,那么...

Using the data you described earlier: 使用您之前描述的数据:

CREATE TABLE valid_combinations (
   combination_id INT NOT NULL,
   record_id INT NOT NULL
   PRIMARY KEY (record_id, combination_id),
   KEY (record_id)
)

Then... 然后...

INSERT INTO valid_combinations (combination_id, record_id)
VALUES (1, 1);
INSERT INTO valid_combinations (combination_id, record_id)
VALUES (1, 10);
INSERT INTO valid_combinations (combination_id, record_id)
VALUES (1, 11);

INSERT INTO valid_combinations (combination_id, record_id)
VALUES (2, 2);
INSERT INTO valid_combinations (combination_id, record_id)
VALUES (2,10);

... VALUES (5,12); ...值(5,12);

Then.... 然后....

SELECT 1
FROM valid_combinations a
WHERE a.record_id IN ($combination)
AND NOT EXISTS (SELECT 1 FROM valid_combinations b
   WHERE b.record_id NOT IN ($combination)
   AND b.combination_id=a.combination_id);

However I don't really understand why you need to work out every possible combination. 但是我真的不明白为什么你需要找出所有可能的组合。

My solution will probably be faster, however the order of the algorithm is no better than yours - just that the loop is moved to the database. 我的解决方案可能会更快,但是算法的顺序并不比您的顺序更好-只是将循环移到了数据库中。

C. C。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM