【问题标题】:高级搜索“Mysql-Laravel”中有这么多子查询
【发布时间】:2022-01-20 16:06:34
【问题描述】:

我正在构建一个基于 Laravel 作为 API 的“广告”网站,其中包含动态高级搜索。

广告可以有多个属性,用户可以进行高级搜索,但是如果用户检查了这么多要过滤的属性并且查询变得很慢,那么它会导致这么多的子查询,任何关于比这个更好的查询的建议。

select
 *
from
  `ads`
where
  `ads`.`id` in (
    select
      `ad_attributes`.`ad_id`
    from
      `ad_attributes`
    where
      `attribute_id` = 32
      and `value` in ('odio', 'quos', 'dignissimos', 'dolorem', 'qui')
  )
  and `ads`.`id` in (
    select
      `ad_attributes`.`ad_id`
    from
      `ad_attributes`
    where
      `attribute_id` = 171
      and `value` in ('itaque', 'non', 'dolor', 'laborum')
  )
  and `ads`.`id` in (
    select
      `ad_attributes`.`ad_id`
    from
      `ad_attributes`
    where
      `attribute_id` = 111
      and `value` in ('quia', 'non', 'nam', 'molestias')
  )
  and `ads`.`id` in (
    select
      `ad_attributes`.`ad_id`
    from
      `ad_attributes`
    where
      `attribute_id` = 144
      and `value` in ('delectus', 'nam', 'exercitationem', 'sit')
  )
  and `ads`.`id` in (
    select
      `ad_attributes`.`ad_id`
    from
      `ad_attributes`
    where
      `attribute_id` = 160
      and `value` in ('repellat', 'fugit', 'quaerat', 'vero')
  )
  and `ads`.`id` in (
    select
      `ad_attributes`.`ad_id`
    from
      `ad_attributes`
    where
      `attribute_id` = 176
      and `value` in ('mollitia', 'voluptates', 'maxime', 'culpa')
  )
  and `ads`.`id` in (
    select
      `ad_attributes`.`ad_id`
    from
      `ad_attributes`
    where
      `attribute_id` = 177
      and `value` in ('necessitatibus', 'id')
  )

这是搜索的代码,PS。我正在使用 mpyw/eloquent-has-by-non-dependent-subquery 而不是使用 whereHas,因为它更慢......

->when(!empty($search->attrs), function (Builder $query) use ($search) {
            foreach ($search->attrs as $key => $value) {
                if (!is_null($value)) {
                    $query->hasByNonDependentSubquery('adAttributes', function (Builder $q) use ($value, $key, $search) {
                        $q->where('attribute_id', $key)
                            ->when(is_array($value), fn($q) => $q->whereIn('value', $value))
                            ->when(!is_array($value), fn($q) => $q->where('value', $value));
                    });
                }
            }
            return $query;
        });

我在我的数据库中添加了一些数据,我无法共享所有这些数据,因为它在 Fiddle 上的虚拟种子数据约为 60k [https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=6f00cc71fff716837906c22d46b0c899][1]

【问题讨论】:

  • 你说话有多慢?
  • @Savlon 我还没有投入生产,我在 ad_attributes 和 50k 广告中播种了大约 10k 行。没有子查询的查询大约需要 30 毫秒,并且随着我添加更多过滤器和更多子查询,它会不断增长....我发布的查询大约需要 0.5 秒
  • 请将问题缩小到 2 个(而不是 7 个)IN,每个只有 2 个选项。然后提供数据样本数据,其中包含您想要的一些行和一些您不想要的行。特别是,提供一些 nnichols 的公式会错误地包含的行。让我们专注于 SQL(假设您可以向后工作到 Elequent)。

标签: mysql laravel eloquent subquery advanced-search


【解决方案1】:

这可能完全没用并且性能非常糟糕,但我隐约记得多年前在与 EAV 性能问题作斗争时使用过这些方法。我没有合适的测试数据集来测试这个,所以我提出这个建议可能是在自欺欺人。

select
 *
from
  `ads`
where
  `ads`.`id` in (
    select
      `ad_attributes`.`ad_id`
    from
      `ad_attributes`
    where
      (`attribute_id` = 32  and `value` in ('odio', 'quos', 'dignissimos', 'dolorem', 'qui')) OR
      (`attribute_id` = 171 and `value` in ('itaque', 'non', 'dolor', 'laborum')) OR
      (`attribute_id` = 111 and `value` in ('quia', 'non', 'nam', 'molestias')) OR
      (`attribute_id` = 144 and `value` in ('delectus', 'nam', 'exercitationem', 'sit')) OR
      (`attribute_id` = 160 and `value` in ('repellat', 'fugit', 'quaerat', 'vero')) OR
      (`attribute_id` = 176 and `value` in ('mollitia', 'voluptates', 'maxime', 'culpa')) OR
      (`attribute_id` = 177 and `value` in ('necessitatibus', 'id'))
    group by `ad_attributes`.`ad_id`
    having count(`attribute_id`) = 7
  )

编辑 正如我在下面的第一条评论中所述 - You may need to COUNT(DISTINCT attribute_id), depending on whether ads can have multiple rows for the same attribute_id。由于您的测试数据集确实有多个行对应同一个 ad_id、attribute_id 对,因此您需要添加 DISTINCT。

我已添加到您原来的SQL Fiddle 和您的db<>fiddle。不幸的是 dbfiddle 不会返回每个查询的执行时间。

根据您的两个小提琴中的表格,这些表格目前没有索引。您的 ad_attributes 表上还有一个不必要的代理主键,而不是 (ad_id, attribute_id) 或 (ad_id, attribute_id, value) 上的自然键。如果您允许/需要每个 ad_id 具有相同 attribute_id 的多行,则需要第二个版本,就像您的测试数据中目前的情况一样。这是故意的还是只是您的测试数据创建方式中的错误?

在你的 ad_attributes 中的 776 个测试行中,有 106 组 (ad_id,attribute_id,attribute_option_id,value) 有两个或多个副本,最差的有六个副本。我建议您的测试数据没有用,因为它不遵循生产数据集中所需的基本规则和约束。使用完全随机数据测试查询并不是特别有用。您的测试数据应尝试对您可能在生产数据中看到的内容进行建模。

我并不完全确信我理解您的数据结构的意图。拥有 attribute_option_id 和 value 列背后的想法是什么?文本和选项属性背后的想法是什么?你真的需要一个文本属性吗?仔细考虑最后一个,因为随着数据集的增长,它有可能对数据质量和性能产生非常重大的影响。

【讨论】:

  • 感谢您花时间提供帮助,OR 将无济于事,因为客户希望结果具有例如 odio 和 non 和 quia ...等:(
  • 您使用ORs 来检索所有潜在的匹配项,而无需多个连接或子查询。然后使用检索到的属性的GROUP BY 和COUNT 来查看有多少属性满足要求,在本例中是全部七个。您可能需要COUNT(DISTINCT attribute_id),具体取决于广告是否可以为同一attribute_id 包含多行。它确实有效,但可能比您当前的子查询还要慢。
  • @MahmoudBenJabir - 尽管您发表了评论,但它得到的结果是否与您原来的公式相同?
  • @RickJames 不,我发布的查询返回了 91 行,评论中的查询返回了 2 行
  • @MahmoudBenJabir - 抱歉,我还是没看到;您能否在 nnichols 的查询中显示 91 行中的两行,这些行显示为(更正确地)您的查询的 2 行之一。
【解决方案2】:

是否有理由不直接加入表格并使用where()?

$query = Ads::select('ads.*')->join('ads_attributes', 'ads.id', 'ads_attributes.ad_id');

if (!empty($search->attrs) {
    foreach ($search->attrs as $k=>$v) {
        $q->orWhere(function (Builder $q) (use $k, $v) {
            $q->where('ads_attributes.id', $k)
                ->when(
                    is_array($v),
                    fn ($q) => $q->whereIn('value', $v),
                    fn ($q) => $q->where('value', $v),
                );
        });
    }
}

return $query;

【讨论】:

  • where 子句变为 and (ad_attributes.attribute_id = 111 and value in ('quia', 'non', 'nam', 'molestias') ) 和 (@987654326 @.attribute_id = 144 和 value in ('delectus', 'nam', 'exercitationem', 'sit') 和 (ad_attributes.attribute_id = 160 和 value in ('repellat' , 'fugit', 'quaerat', 'vero') ).... 等永远是假的!!
  • 您的原始查询也是and。把外面的where改成orWhere()?
  • 所有ands 都是子查询,所以可以是and ads.id in (select ...),但不能是and attribute_id = 1 and attribute_id = 2!
  • 是的,我意识到了这一点。编辑有帮助吗?
  • 实际上它已更改为OR,这几乎总是正确的:") ....
【解决方案3】:

众所周知,实体-属性-值模式模式冗长且效率低下。但它很灵活。

可能有助于将这个“复合”索引(也就是“覆盖”)添加到表 ad_attributes:

INDEX(attribute_id, value, ad_attributes)

【讨论】:

    猜你喜欢
    • 2018-10-21
    • 2015-06-09
    • 1970-01-01
    • 2015-01-14
    • 2018-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多