查询需要很长时间

Posted

技术标签:

【中文标题】查询需要很长时间【英文标题】:query is taking long time 【发布时间】:2021-01-04 07:52:09 【问题描述】:
SELECT  a.sectionid as sectionid, views.timestamp as timestamp,
        views.time_on_page as time_on_page, views.video as video,
        views.access as access, masterid
    FROM (
        SELECT  m.timestamp as timestamp, m.time_on_page as time_on_page,
                AES_DECRYPT(m.IP_Blob, UNHEX(SHA2('Jove Is Cool',512))) as ip_bin,
                m.page as page, m.language as language, m.access as access,
                m.referrer as referrer, m.searchterms as searchterms,
                m.user_id as user_id,
                case when m.page regexp '/pdf(-materials)?/'
                     then replace(replace(replace(RIGHT(m.page, 5), '/', ''), 'd', ''), 'f', '')
                     else m.video end as video,
                m.id as masterid
            FROM  masterstats_innodb as m
            join  stats_to_institution as sti  ON m.id = sti.statid
            JOIN  institutions as ins  ON sti.institutionid = ins.institutionid
            WHERE  m.timestamp BETWEEN '2019-12-30' AND '2020-12-29'
              and  ( ins.institutionid = '2'
                  or ins.parentinstitutionid = '2' ) 
        ) as views
    LEFT JOIN  articles as a  ON views.video = a.productid
       `enter code here`
    order by masterid

masterstats_innodb -- 115759655 stats_to_institution -- 45317238 机构 --45038

所有可能的索引都已创建,并且还为 masterstats_innodb 表创建了分区。

【问题讨论】:

masterstats_innodb 中有多少行在这两个日期之间?我想这将是一个很大的数额?有多少匹配 institutionidparentinstitutionid 过滤器?当你组合所有过滤器时,你希望有多少?你用什么对表进行分区? 请为每张桌子提供SHOW CREATE TABLE 【参考方案1】:

OR没有优化好;切换到UNION 是一个常见的修复方法。但目前尚不清楚这是否会有所帮助。哪个更有选择性? m.timestamp(ins.institutionid = '2' or ins.parentinstitutionid = '2')?

对于stats_to_institution 上的最佳索引:http://mysql.rjweb.org/doc.php/index_cookbook_mysql#many_to_many_mapping_table

enter code here 在哪里没有意义;请填写示例。

暂定索引:

a:  (productid, sectionid)
ins:  (parentinstitutionid, institutionid)
m:  (timestamp)
sti:  (statid, institutionid)
sti:  (institutionid, statid)

【讨论】:

以上是关于查询需要很长时间的主要内容,如果未能解决你的问题,请参考以下文章

查询需要很长时间

MySQL 查询返回 JSON 数据需要很长时间

Postgresql 选择计数查询需要很长时间

选择查询需要很长时间执行 - 查询优化

UPDATE 查询需要很长时间 PostgreSQL

MySql 查询需要很长时间 PHP