我有一个object表,其中存储与照片中识别的对象相关的数据。这意味着每一张照片可以有多行。
object表示例,显示在1张照片中识别的3个对象。
+----+--------+---------+-----------+
| id | name | photoId | companyId |
+----+--------+---------+-----------+
| 1 | Person | 33 | 50 |
+----+--------+---------+-----------+
| 2 | Cat | 33 | 50 |
+----+--------+---------+-----------+
| 3 | Dog | 33 | 50 |
+----+--------+---------+-----------+最终,这张桌子可能会变得很大。如果上传了500,000张照片,并且在每个文件中识别了20个对象,则为10,000,000行。
问题
object.companyId有效地查询这个表,以便为自己提供每个公司已识别的前5位对象的列表?下面显示了我的尝试,但由于我存储的数据很少,它看起来已经很慢了。我的尝试:
从MySql检索结果:
select * from object where companyId=50
在PHP中迭代结果以获得每个标签的计数:
foreach($res as $row){
//get how many times this label shows up in the results
$labelCnt = array_count_values(array_column($res, 'name'))[$row['name']];
//create an array of all label counts (label -> cnt)
$allLabelCntArr[$row['name']] = $labelCnt;
}
arsort($allLabelCnts); //sort from highest to lowest
$topFive = array_slice($allLabelCnts, 0, 5, true); //get top 5发布于 2020-01-15 21:59:24
您可以在MySQL查询中直接获取给定公司的前5个对象:
SELECT name, COUNT(*) AS cnt
FROM object
WHERE companyId = 50
GROUP BY name
ORDER BY cnt DESC
LIMIT 5这将返回具有最高计数的LIMIT值的5行(或您在name子句中放入的行数)。只要您有name和companyId上的索引,不管表大小如何,这个查询都应该执行得很好。
https://stackoverflow.com/questions/59759945
复制相似问题