目前正在使用视图,我应该使用硬表吗?

发布于 2024-08-31 21:50:11 字数 3018 浏览 8 评论 0原文

我目前正在争论我的表mapping_uGroups_uProducts 是否是由下表形成的视图:

CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` 
    SQL SECURITY DEFINER VIEW `db`.`mapping_uGroups_uProducts` 
    AS select distinct `X`.`upID` AS `upID`,`Z`.`ugID` AS `ugID` from 
    ((`db`.`mapping_uProducts_Products` `X` join `db`.`productsInfo` `Y` 
            on((`X`.`pID` = `Y`.`pID`))) join `db`.`mapping_uGroups_Groups` `Z` 
            on((`Y`.`gID` = `Z`.`gID`)));

我当前的查询是:

    SELECT upID FROM uProductsInfo \
        JOIN fs_uProducts USING (upID) column \
        JOIN mapping_uGroups_uProducts USING (upID) -- could be faster if we use hard table and index \
        JOIN mapping_fs_key USING (fsKeyID)  \
    WHERE fsName="OVERALL"  \
        AND ugID=1          \
    ORDER BY score DESC     \
    LIMIT 0,30;

这非常慢。 (对于 30 个结果,大约需要 10 秒)。我认为我的查询如此慢的原因肯定是由于该特定查询依赖于没有索引来加快速度的视图。

+----+-------------+----------------+--------+----------------+---------+---------+---------------------------------------+-------+---------------------------------+
        | id | select_type | table          | type   | possible_keys  | key     | key_len | ref                                   | rows  | Extra                           |
        +----+-------------+----------------+--------+----------------+---------+---------+---------------------------------------+-------+---------------------------------+
        |  1 | PRIMARY     | mapping_fs_key | const  | PRIMARY,fsName | fsName  | 386     | const                                 |     1 | Using temporary; Using filesort | 
        |  1 | PRIMARY     | <derived2>     | ALL    | NULL           | NULL    | NULL    | NULL                                  | 19706 | Using where                     | 
        |  1 | PRIMARY     | uProductsInfo  | eq_ref | PRIMARY        | PRIMARY | 4       | mapping_uGroups_uProducts.upID        |     1 | Using index                     | 
        |  1 | PRIMARY     | fs_uProducts   | ref    | upID           | upID    | 4       | db.uProductsInfo.upID                 |   221 | Using where                     | 
        |  2 | DERIVED     | X              | ALL    | PRIMARY        | NULL    | NULL    | NULL                                  | 40772 | Using temporary                 | 
        |  2 | DERIVED     | Y              | eq_ref | PRIMARY        | PRIMARY | 4       | db.X.pID                              |     1 | Distinct                        | 
        |  2 | DERIVED     | Z              | ref    | PRIMARY        | PRIMARY | 4       | db.Y.gID                              |     2 | Using index; Distinct           | 
        +----+-------------+----------------+--------+----------------+---------+---------+---------------------------------------+-------+---------------------------------+
        7 rows in set (0.48 sec)

这里的解释看起来非常神秘,我不知道是否应该删除视图并编写一个脚本以将视图中的所有内容插入到硬表中。 (显然,由于映射变化非常频繁,它将失去视图的灵活性)。

有谁知道如何更好地优化我的架构?

I am currently debating whether my table, mapping_uGroups_uProducts, which is a view formed by the following table:

CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` 
    SQL SECURITY DEFINER VIEW `db`.`mapping_uGroups_uProducts` 
    AS select distinct `X`.`upID` AS `upID`,`Z`.`ugID` AS `ugID` from 
    ((`db`.`mapping_uProducts_Products` `X` join `db`.`productsInfo` `Y` 
            on((`X`.`pID` = `Y`.`pID`))) join `db`.`mapping_uGroups_Groups` `Z` 
            on((`Y`.`gID` = `Z`.`gID`)));

My current query is:

    SELECT upID FROM uProductsInfo \
        JOIN fs_uProducts USING (upID) column \
        JOIN mapping_uGroups_uProducts USING (upID) -- could be faster if we use hard table and index \
        JOIN mapping_fs_key USING (fsKeyID)  \
    WHERE fsName="OVERALL"  \
        AND ugID=1          \
    ORDER BY score DESC     \
    LIMIT 0,30;

which is pretty slow. (for 30 results, it requires about 10 secondes). I think the reason for my query being so slow is definitely due to the fact that that particular query relies on a VIEW which has no index to speed things up.

+----+-------------+----------------+--------+----------------+---------+---------+---------------------------------------+-------+---------------------------------+
        | id | select_type | table          | type   | possible_keys  | key     | key_len | ref                                   | rows  | Extra                           |
        +----+-------------+----------------+--------+----------------+---------+---------+---------------------------------------+-------+---------------------------------+
        |  1 | PRIMARY     | mapping_fs_key | const  | PRIMARY,fsName | fsName  | 386     | const                                 |     1 | Using temporary; Using filesort | 
        |  1 | PRIMARY     | <derived2>     | ALL    | NULL           | NULL    | NULL    | NULL                                  | 19706 | Using where                     | 
        |  1 | PRIMARY     | uProductsInfo  | eq_ref | PRIMARY        | PRIMARY | 4       | mapping_uGroups_uProducts.upID        |     1 | Using index                     | 
        |  1 | PRIMARY     | fs_uProducts   | ref    | upID           | upID    | 4       | db.uProductsInfo.upID                 |   221 | Using where                     | 
        |  2 | DERIVED     | X              | ALL    | PRIMARY        | NULL    | NULL    | NULL                                  | 40772 | Using temporary                 | 
        |  2 | DERIVED     | Y              | eq_ref | PRIMARY        | PRIMARY | 4       | db.X.pID                              |     1 | Distinct                        | 
        |  2 | DERIVED     | Z              | ref    | PRIMARY        | PRIMARY | 4       | db.Y.gID                              |     2 | Using index; Distinct           | 
        +----+-------------+----------------+--------+----------------+---------+---------+---------------------------------------+-------+---------------------------------+
        7 rows in set (0.48 sec)

The explain here looks pretty cryptic, and I don't know whether I should drop view and write a script to just insert everything in the view to a hard table. ( obviously, it will lose the flexibility of the view since the mapping changes quite frequently).

Does anyone have any idea to how I can optimize my schema better?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

年少掌心 2024-09-07 21:50:11

您当前的计划使用视图作为驱动表:使用 fsName = 'OVERALL' 扫描 mapping_fs_key 中的每条记录

您可以使用以下函数替换视图:

SELECT  upID FROM uProductsInfo
JOIN    fs_uProducts USING (upID)
JOIN    mapping_fs_key USING (fsKeyID)
WHERE   fsName='OVERALL'
        AND upID IN
        (
        SELECT  upID
        FROM    mapping_uGroups_Groups Z
        JOIN    productsInfo Y
        ON      y.gID = z.gID
        JOIN    mapping_uProducts_Products X
        ON      x.pID = y.pID
        WHERE   z.ugID = 1
        )
ORDER BY
        score DESC
LIMIT 0,30

You current plan uses the view as a driven table: it is scanned for each record in mapping_fs_key with fsName = 'OVERALL'

You could replace the view with this function:

SELECT  upID FROM uProductsInfo
JOIN    fs_uProducts USING (upID)
JOIN    mapping_fs_key USING (fsKeyID)
WHERE   fsName='OVERALL'
        AND upID IN
        (
        SELECT  upID
        FROM    mapping_uGroups_Groups Z
        JOIN    productsInfo Y
        ON      y.gID = z.gID
        JOIN    mapping_uProducts_Products X
        ON      x.pID = y.pID
        WHERE   z.ugID = 1
        )
ORDER BY
        score DESC
LIMIT 0,30
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文