简体   繁体   English

在一对多关系查询中使用LIMIT

[英]Using LIMIT's during one-to-many relationship Queries

I have the two tables tabA and tabB , and there is a one-to-many relationship from tabA to tabB . 我有两个表tabAtabB ,并且从tabAtabB一对多的关系。 I have the query: 我有查询:

SELECT * FROM `tabA` LEFT JOIN `tabB` ON `tabA`.`aID` = `tabB`.`aID`

and the rows that are returned is a large set with multiple duplicates from tabA for each tabB reference to tabA . 并且返回的行是一个大集合,其中每个tabBtabB引用都来自tabA多个重复tabA

I am aware that I can use GROUP BY to limit the tabA rows to unique elements, unless I use custom field(s) using the GROUP_CONCAT function, combined with two REPLACE functions for escaping (which seriously impacts performance), I loose all bar one of the rows contained in tabB . 我知道我可以使用GROUP BYtabA行限制为唯一元素,除非我使用通过GROUP_CONCAT函数与两个REPLACE函数结合使用的自定义字段来转义(这严重影响了性能),所以我放开了所有小节tabB包含的行tabB An example query looks like: 查询示例如下:

   SELECT `tabA`.*,
          GROUP_CONCAT(REPLACE(REPLACE(`tabB`.`tabBCol1`, '/', '//'), ',', '/,')) AS `tabBCol`,
          GROUP_CONCAT(REPLACE(REPLACE(`tabB`.`tabBCol2`, '/', '//'), ',', '/,')) AS `tabBCo2` 
     FROM `tabA` 
LEFT JOIN `tabB` ON `tabA`.`aID` = `tabB`.`aID` 
 GROUP BY `tabA`.`aID`

That query will allow me to use the LIMIT syntax so I can (for example) only show 5 entries, starting after 5 (ie LIMIT 5,5 ). 该查询将允许我使用LIMIT语法,因此(例如)我只能显示5个条目(从5开始)(即LIMIT 5,5 )。 And when I apply that to the former query, then I won't get the next 5 queries, but a random set of data based on the numbers of associations. 当我将其应用于前一个查询时,将不会获得接下来的5个查询,而是基于关联数的随机数据集。

So, apart from the second query, is there any way that I can fetch the rows, with there associations, but allow the use of the LIMIT syntax, and without the performance hit of excessive REPLACE functions? 因此,除了第二个查询之外,还有什么方法可以获取具有关联的行,但是允许使用LIMIT语法,而不会导致过多的REPLACE函数降低性能?

ADDITIONAL 额外

Although I can use multiple subqueries for each row, using the first query with GROUP BY syntax (which would allow me to apply any WHERE conditions for the associations), I am trying to find a way to avoid the N+1 Selects Problem (although in this example, my LIMIT syntax is LIMIT 5,5 , I will be applying this to much larger LIMIT s (upto 1000 rows at a time)). 尽管我可以为每行使用多个子查询,但是使用第一个查询使用GROUP BY语法(这将允许我将任何WHERE条件应用于关联),但我仍在尝试寻找一种避免N + 1选择问题的方法(尽管在此示例中,我的LIMIT语法为LIMIT 5,5 ,我将其应用到更大的LIMIT (一次最多1000行)。

Try two queries: 尝试两个查询:

// get those 5 records
SELECT * FROM Cars   WHERE some_conditon = blabla LIMIT 5;

// get all associated records from related table
SELECT * FROM Wheels WHERE car_id IN (1, 3, 5, 123, 16);

In the result there will not be any N problem as you will always have two queries. 结果将不会有任何N问题,因为您将始终有两个查询。 Even if you will have 1000 records in 1st query it will always be better to use this simple method, than joins/groups by/concats/etc. 即使您在第一个查询中有1000条记录,使用这种简单方法总是比通过/ concats / etc加入/分组更好。

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

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