草庐IT

php - 在同一个表上的多个连接似乎影响性能的情况下,如何改进此查询

coder 2023-10-24 原文

以下查询用于在电子商务网站上搜索产品数据库时应用多个过滤值。

When you use more than 7 filters, MySQL server goes down.

  • 有机会加入positions_categories_links一次吗?
  • 我应该考虑更改数据库结构吗?

Each product has one eshop_cats category (many2one) and up to 20 categories with multiple relations (many2many).

eshop_pos 有 40,000 条记录并包含产品。

eshop_cats 有 340 条记录并包含主要类别。

categories 有 6000 条记录并包含 Duplex Categories。

positions_categories_links 有 360,000 条记录,包含 Products 和 Categories 之间的键。


这是我的查询:

SELECT COUNT(DISTINCT eshop_pos.id)
FROM eshop_pos
INNER JOIN eshop_cats t1 ON eshop_pos.eshopcatid = t1.id
    AND t1.active = 1
INNER JOIN positions_categories_links t2 ON t2.pos_id = eshop_pos.id
INNER JOIN categories t3 ON t3.id = t2.cat_id
    AND t3.active = 1
    AND t3.section_id = 62021
INNER JOIN positions_categories_links t4 ON t4.pos_id = eshop_pos.id
INNER JOIN categories t5 ON t5.id = t4.cat_id
    AND t5.active = 1
    AND t5.section_id = 62023
INNER JOIN positions_categories_links AS duplex_links_51 ON duplex_links_51.pos_id = eshop_pos.id
    AND duplex_links_51.cat_id = 51
    AND duplex_links_51.value IN (2984)
INNER JOIN positions_categories_links AS duplex_links_52 ON duplex_links_52.pos_id = eshop_pos.id
    AND duplex_links_52.cat_id = 52
    AND duplex_links_52.value IN (3003)
INNER JOIN positions_categories_links AS duplex_links_3904 ON duplex_links_3904.pos_id = eshop_pos.id
    AND duplex_links_3904.cat_id = 3904
    AND duplex_links_3904.value IN (3941)
INNER JOIN positions_categories_links AS duplex_links_4462 ON duplex_links_4462.pos_id = eshop_pos.id
    AND duplex_links_4462.cat_id = 4462
    AND duplex_links_4462.value IN (4465)
INNER JOIN positions_categories_links AS duplex_links_4466 ON duplex_links_4466.pos_id = eshop_pos.id
    AND duplex_links_4466.cat_id = 4466
    AND duplex_links_4466.value IN (4468)
INNER JOIN positions_categories_links AS duplex_links_4472 ON duplex_links_4472.pos_id = eshop_pos.id
    AND duplex_links_4472.cat_id = 4472
    AND duplex_links_4472.value IN (4473)
INNER JOIN positions_categories_links AS duplex_links_4974 ON duplex_links_4974.pos_id = eshop_pos.id
    AND duplex_links_4974.cat_id = 4974
    AND duplex_links_4974.value IN (4978)
INNER JOIN positions_categories_links AS duplex_links_4979 ON duplex_links_4979.pos_id = eshop_pos.id
    AND duplex_links_4979.cat_id = 4979
    AND duplex_links_4979.value IN (4982)
INNER JOIN positions_categories_links AS duplex_links_4984 ON duplex_links_4984.pos_id = eshop_pos.id
    AND duplex_links_4984.cat_id = 4984
    AND duplex_links_4984.value IN (4986)

我无法在服务器上运行查询的 EXPLAIN。但是,它在我的本地笔记本电脑上运行良好:

最佳答案

您的查询格式略有改变。我还将别名引用从您的“duplex_links”引用更改为缩写别名“PCL”(来自您的 Position_categories_links 表)。更短且相关的帮助表引用(至少对我和其他人而言)。

至于您的表/索引,如果它们尚不存在,我建议使用以下表/索引。在这种情况下,我为您的查询提供了所有覆盖索引,这意味着由于用于满足所有连接条件的列是索引的一部分,sql 数据库不必转到底层实际数据页来确认其他详细信息,因此帮助提高性能。

Table                       Index
eshop_pos                   (id, eshopcatid)
eshop_cats                  (id, active)
positions_categories_links  (pos_id, cat_id, value)
categories                  (id, active, section_id)

我还喜欢显示连接位置之间的缩进相关性,这样您就知道如何从一个表/别名进入下一个级别。您可以直接看到事物的来源层次结构。

SELECT 
      COUNT(DISTINCT eshop_pos.id) 
   FROM 
      eshop_pos 
         inner join eshop_cats t1 
            on eshop_pos.eshopcatid = t1.id 
           AND t1.active = 1 

         inner join positions_categories_links t2 
            on eshop_pos.id = t2.pos_id

            inner join categories t3 
               on t2.cat_id = t3.id
              and t3.active = 1 
              and t3.section_id = 62021 

         inner join positions_categories_links t4 
            on eshop_pos.id = t4.pos_id

            inner join categories t5 
               on t4.cat_id = t5.id
              and t5.active = 1 
              and t5.section_id = 62023 

         INNER JOIN positions_categories_links AS PCL51 
            ON eshop_pos.id = PCL51.pos_id
           AND PCL51.cat_id = 51 
           and PCL51.value in (2984) 

         INNER JOIN positions_categories_links AS PCL52 
            ON eshop_pos.id = PCL52.pos_id
           AND PCL52.cat_id = 52 
           and PCL52.value in (3003) 

         INNER JOIN positions_categories_links AS PCL3904 
            ON eshop_pos.id = PCL3904.pos_id
           AND PCL3904.cat_id = 3904 
           and PCL3904.value in (3941) 

         INNER JOIN positions_categories_links AS PCL4462
            ON eshop_pos.id = PCL4462.pos_id
           AND PCL4462.cat_id = 4462 
           and PCL4462.value in (4465) 

         INNER JOIN positions_categories_links AS PCL4466 
            ON eshop_pos.id = PCL4466.pos_id
           AND PCL4466.cat_id = 4466 
           and PCL4466.value in (4468) 

现在,拥有适当的索引来帮助优化查询是一回事,但不断地对这样的多个条件进行计数可能有点矫枉过正。如果您有一个已知的特定详细级别,例如您在此处所做的特定类别,那肯定会有所帮助。

(删除了不保证所有标准的“或”版本)

关于php - 在同一个表上的多个连接似乎影响性能的情况下,如何改进此查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48717396/

有关php - 在同一个表上的多个连接似乎影响性能的情况下,如何改进此查询的更多相关文章

  1. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  2. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  3. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  4. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  5. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  6. ruby - Highline 询问方法不会使用同一行 - 2

    设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案

  7. ruby - 默认情况下使选项为 false - 2

    这是在Ruby中设置默认值的常用方法:classQuietByDefaultdefinitialize(opts={})@verbose=opts[:verbose]endend这是一个容易落入的陷阱:classVerboseNoMatterWhatdefinitialize(opts={})@verbose=opts[:verbose]||trueendend正确的做法是:classVerboseByDefaultdefinitialize(opts={})@verbose=opts.include?(:verbose)?opts[:verbose]:trueendend编写Verb

  8. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  9. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  10. ruby - 多个属性的 update_column 方法 - 2

    我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2

随机推荐