mysql - HQL/MySQL 用于列出不同项和重复项

标签 mysql performance grails hql

我有 20.000 多个对象的列表。这些对象有一个名为 title 的表的 fk。如果两个tipp链接到相同的标题,并且它们属于同一个包(tipp_pkg_fk,这是一个参数),则它们被认为是重复的。 我需要所有对象的列表,并将重复项列在一起。例如:

tippA.title.name = "One"
tippB.title.name = "Two"
tippC.title.name = "Two"

理想情况下,从上面我会得到这样的列表结果:[[tippA],[tippB,tippC]]

我不知道如何做到这一点,我已经尝试过(首先在Mysql中,这样我可以测试它,然后将其更改为HQL):

select tipp.tipp_id, 1 as sortOrder
    from (select distinct a.tipp_id as id
            from title_instance_package_platform a, title_instance_package_platform b 
            where a.tipp_pkg_fk= 1 and b.tipp_pkg_fk = 1 and a.tipp_ti_fk = b.tipp_ti_fk) duplicates,
    title_instance_package_platform tipp
where tipp.tipp_id != duplicates.id
union all
select duplicates.id, 2 as sortOrder
    from (select distinct a.tipp_id as id
            from title_instance_package_platform a , title_instance_package_platform b 
            where a.tipp_pkg_fk = 1 and b.tipp_pkg_fk=1 and a.tipp_ti_fk = b.tipp_ti_fk) duplicates
order by sortOrder, id;

这个执行了 330 秒,然后我在 MySQL Workbench 中收到消息fetching,此时计算机开始死机。所以我的想法是,首先我选择所有不重复的 ID,然后选择所有重复的 ID,然后将它们合并并排序,以便它们出现在一起。我正在寻找最有效的方法来执行此操作,因为我将在通宵工作期间多次执行此查询。

对于我的 TIPP 模型,以下是映射的一部分:

static mapping = {
     pkg column:'tipp_pkg_fk', index: 'tipp_idx'
     title column:'tipp_ti_fk', index: 'tipp_idx'
}


+-----------------------------+--------------+------+-----+---------+----------------+
| Field                       | Type         | Null | Key | Default | Extra          |
+-----------------------------+--------------+------+-----+---------+----------------+
| tipp_id                     | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| tipp_version                | bigint(20)   | NO   |     | NULL    |                |
| tipp_pkg_fk                 | bigint(20)   | NO   | MUL | NULL    |                |
| tipp_plat_fk                | bigint(20)   | NO   | MUL | NULL    |                |
| tipp_ti_fk                  | bigint(20)   | NO   | MUL | NULL    |                |
| date_created                | datetime     | NO   |     | NULL    |                |
| last_updated                | datetime     | NO   |     | NULL    |                |



+-----------------+---------------+------+-----+---------+----------------+
| Field           | Type          | Null | Key | Default | Extra          |
+-----------------+---------------+------+-----+---------+----------------+
| ti_id           | bigint(20)    | NO   | PRI | NULL    | auto_increment |
| ti_version      | bigint(20)    | NO   |     | NULL    |                |
| date_created    | datetime      | NO   |     | NULL    |                |
| ti_imp_id       | varchar(255)  | NO   | MUL | NULL    |                |
| last_updated    | datetime      | NO   |     | NULL    |                |
| ti_title        | varchar(1024) | YES  |     | NULL    |                |
| ti_key_title    | varchar(1024) | YES  |     | NULL    |                |
| ti_norm_title   | varchar(1024) | YES  |     | NULL    |                |
| sort_title      | varchar(1024) | YES  |     | NULL    |                |
+-----------------+---------------+------+-----+---------+----------------+

更新

经过一些更改后,它正在工作:

select tipp.tipp_id as id, 1 as sortOrder
from
    title_instance_package_platform tipp
where tipp.tipp_id not in (select distinct a.tipp_id as id
            from title_instance_package_platform a, title_instance_package_platform b 
            where a.tipp_pkg_fk= 1 and b.tipp_pkg_fk = 1 and a.tipp_ti_fk = b.tipp_ti_fk)
union all
select duplicates.id as id, 2 as sortOrder
    from (select distinct a.tipp_id as id
            from title_instance_package_platform a , title_instance_package_platform b 
            where a.tipp_pkg_fk = 1 and b.tipp_pkg_fk=1 and a.tipp_ti_fk = b.tipp_ti_fk) duplicates
order by sortOrder, id;

尽管如此,我仍然没有将重复项分组在一起,而是所有内容都以列表的形式出现,这意味着我仍然需要将它们分组。

最佳答案

你能从另一边做选择吗? 选择所有标题和包并列出所有提示,仅当存在提示时(计数> 0)并将它们捆绑在一起以获得您显示的数组?

关于mysql - HQL/MySQL 用于列出不同项和重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31921596/

相关文章:

mysql - 如何使用 MySQL 查询在月份中获取两个日期之间的差异?

mysql - 如何在一个查询中每天包含多个 COUNT?

php mysql 数据库连接非常慢

css - LESS、媒体查询和性能

python - 加速 Python 脚本的 for 循环

grails - 如何使用 Grails 和数据库迁移插件执行插件迁移?

grails - Grails exceptionHandler是否可以支持以下错误处理流程

mysql - 需要有关 MySQL 的帮助,我认为该 MySQL 涉及自连接

javascript - 执行此操作的最快方法?三元运算符?转变?大批?

grails - 通过字符串将对象转换为特定类